1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.dx.io;
18
19import com.android.dx.util.ByteArrayByteInput;
20import com.android.dx.util.ByteInput;
21
22/**
23 * An encoded value or array.
24 */
25public final class EncodedValue implements Comparable<EncodedValue> {
26    private final byte[] data;
27
28    public EncodedValue(byte[] data) {
29        this.data = data;
30    }
31
32    public ByteInput asByteInput() {
33        return new ByteArrayByteInput(data);
34    }
35
36    public byte[] getBytes() {
37        return data;
38    }
39
40    public void writeTo(DexBuffer.Section out) {
41        out.write(data);
42    }
43
44    @Override public int compareTo(EncodedValue other) {
45        int size = Math.min(data.length, other.data.length);
46        for (int i = 0; i < size; i++) {
47            if (data[i] != other.data[i]) {
48                return (data[i] & 0xff) - (other.data[i] & 0xff);
49            }
50        }
51        return data.length - other.data.length;
52    }
53
54    @Override public String toString() {
55        return Integer.toHexString(data[0] & 0xff) + "...(" + data.length + ")";
56    }
57}
58