-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathByteArray.java
More file actions
executable file
·228 lines (224 loc) · 6.56 KB
/
ByteArray.java
File metadata and controls
executable file
·228 lines (224 loc) · 6.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* Copyright (C) 2022 github.com/REAndroid
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.reandroid.arsc.item;
import java.util.AbstractList;
import java.util.List;
public class ByteArray extends BlockItem {
public ByteArray(int bytesLength) {
super(bytesLength);
}
public ByteArray() {
this(0);
}
public final void clear(){
setSize(0);
}
public final void add(byte[] values){
if(values==null || values.length==0){
return;
}
int old=size();
int len=values.length;
setBytesLength(old+len, false);
byte[] bts = getBytesInternal();
System.arraycopy(values, 0, bts, old, len);
}
public final void set(byte[] values){
super.setBytesInternal(values);
}
public final byte[] toArray(){
return getBytes();
}
public final void fill(byte value){
byte[] bts=getBytesInternal();
int max=bts.length;
for(int i=0;i<max;i++){
bts[i]=value;
}
}
public final void ensureArraySize(int s){
int sz=size();
if(sz>=s){
return;
}
setSize(s);
}
public final void setSize(int s){
if(s<0){
s=0;
}
setBytesLength(s);
}
public final int size(){
return getBytesLength();
}
public byte get(int index){
return getBytesInternal()[index];
}
public int getByteUnsigned(int index){
return 0xff & get(index);
}
public final void putByte(int index, int byteValue){
put(index, (byte) byteValue);
}
public final void put(int index, byte value){
byte[] bts = getBytesInternal();
bts[index]=value;
}
public boolean getBit(int byteOffset, int bitIndex){
return getBit(getBytesInternal(), byteOffset, bitIndex);
}
public void putBit(int byteOffset, int bitIndex, boolean bit){
putBit(getBytesInternal(), byteOffset, bitIndex, bit);
}
public final void putShort(int offset, int value){
putShort(offset, (short) value);
}
public final void putShort(int offset, short val){
byte[] bts = getBytesInternal();
bts[offset+1]= (byte) (val >>> 8 & 0xff);
bts[offset]= (byte) (val & 0xff);
}
public final int getShortUnsigned(int offset){
return 0xffff & getShort(offset);
}
public final short getShort(int offset){
byte[] bts = getBytesInternal();
return (short) (bts[offset] & 0xff | (bts[offset+1] & 0xff) << 8);
}
public final void putInteger(int offset, int val){
byte[] bts = getBytesInternal();
if((offset+4)>bts.length){
return;
}
bts[offset+3]= (byte) (val >>> 24);
bts[offset+2]= (byte) (val >>> 16 & 0xff);
bts[offset+1]= (byte) (val >>> 8 & 0xff);
bts[offset]= (byte) (val & 0xff);
}
public final int getInteger(int offset){
byte[] bts = getBytesInternal();
if((offset+4)>bts.length){
return 0;
}
return bts[offset] & 0xff |
(bts[offset+1] & 0xff) << 8 |
(bts[offset+2] & 0xff) << 16 |
(bts[offset+3] & 0xff) << 24;
}
public final void putByteArray(int offset, byte[] val){
byte[] bts = getBytesInternal();
int avail = bts.length-offset;
if(avail<=0){
return;
}
int len = val.length;
if(len>avail){
len=avail;
}
System.arraycopy(val, 0, bts, offset, len);
}
public final byte[] getByteArray(int offset, int length){
byte[] bts = getBytesInternal();
byte[] result = new byte[length];
if (result.length >= 0) {
System.arraycopy(bts, offset, result, 0, result.length);
}
return result;
}
public final List<Byte> toByteList(){
return new AbstractList<Byte>() {
@Override
public Byte get(int i) {
return ByteArray.this.get(i);
}
@Override
public int size() {
return ByteArray.this.size();
}
};
}
public final List<Short> toShortList(){
return new AbstractList<Short>() {
@Override
public Short get(int i) {
return ByteArray.this.getShort(i);
}
@Override
public int size() {
return ByteArray.this.size()/2;
}
};
}
public final List<Integer> toIntegerList(){
return new AbstractList<Integer>() {
@Override
public Integer get(int i) {
return ByteArray.this.getInteger(i);
}
@Override
public int size() {
return ByteArray.this.size()/4;
}
};
}
@Override
public String toString(){
return "size="+size();
}
public static byte[] trimTrailZeros(byte[] bts){
if(bts==null){
return new byte[0];
}
int len=0;
for(int i=0;i<bts.length;i++){
if(bts[i]!=0){
len=i+1;
}
}
byte[] result=new byte[len];
if(result.length>0){
System.arraycopy(bts, 0, result, 0, result.length);
}
return result;
}
public static boolean equals(byte[] bts1, byte[] bts2){
if(bts1==bts2){
return true;
}
if(bts1==null || bts1.length==0){
return bts2==null || bts2.length==0;
}
if(bts2==null || bts2.length==0){
return false;
}
if(bts1.length!=bts2.length){
return false;
}
for(int i=0;i<bts1.length;i++){
if(bts1[i]!=bts2[i]){
return false;
}
}
return true;
}
public static boolean equalsIgnoreTrailZero(byte[] bts1, byte[] bts2){
if(bts1==bts2){
return true;
}
return equals(trimTrailZeros(bts1), trimTrailZeros(bts2));
}
}