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 
17 package com.android.dex;
18 
19 import com.android.dex.util.ByteArrayByteInput;
20 import com.android.dex.util.ByteInput;
21 
22 /**
23  * An encoded value or array.
24  */
25 public final class EncodedValue implements Comparable<EncodedValue> {
26     private final byte[] data;
27 
EncodedValue(byte[] data)28     public EncodedValue(byte[] data) {
29         this.data = data;
30     }
31 
asByteInput()32     public ByteInput asByteInput() {
33         return new ByteArrayByteInput(data);
34     }
35 
getBytes()36     public byte[] getBytes() {
37         return data;
38     }
39 
writeTo(Dex.Section out)40     public void writeTo(Dex.Section out) {
41         out.write(data);
42     }
43 
44     @Override
compareTo(EncodedValue other)45     public int compareTo(EncodedValue other) {
46         int size = Math.min(data.length, other.data.length);
47         for (int i = 0; i < size; i++) {
48             if (data[i] != other.data[i]) {
49                 return (data[i] & 0xff) - (other.data[i] & 0xff);
50             }
51         }
52         return data.length - other.data.length;
53     }
54 
55     @Override
toString()56     public String toString() {
57         return Integer.toHexString(data[0] & 0xff) + "...(" + data.length + ")";
58     }
59 }
60