ByteArrayByteWriter.java
9 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* Decompiled with CFR 0_118.
*/
package com.adobe.internal.io;
import com.adobe.internal.io.ByteWriter;
import java.io.IOException;
import java.util.ArrayList;
public final class ByteArrayByteWriter
implements ByteWriter {
private byte[] mCurBuffer;
private ArrayList mBufferArray;
private int mCurIndex;
private int mCurBase;
private int mCurTop;
private int mLength;
private static final int mMinBufSize = 32;
private int mMinBufBit;
private static final int mMaxBufSize = 262144;
private int mMaxBufBit;
private int mTotalSize;
public ByteArrayByteWriter() {
this(0);
}
public ByteArrayByteWriter(byte[] buffer) {
this(buffer, 0, buffer.length);
}
public ByteArrayByteWriter(int size) {
if (size < 0) {
throw new IllegalArgumentException("Negative initial size: " + size);
}
this.mCurBuffer = new byte[32];
this.mBufferArray = new ArrayList();
this.mBufferArray.add(this.mCurBuffer);
this.mCurIndex = 0;
this.mCurBase = 0;
this.mCurTop = 32;
this.mLength = 0;
int bits = 32;
int counter = 0;
while (bits != 0) {
bits >>= 1;
++counter;
}
this.mMinBufBit = counter - 1;
bits = 262144;
counter = 0;
while (bits != 0) {
bits >>= 1;
++counter;
}
this.mMaxBufBit = counter - 1;
this.mTotalSize = 32;
}
public ByteArrayByteWriter(byte[] buffer, int offset, int length) {
if (offset < 0 || length < 0 || offset + length > buffer.length) {
throw new IllegalArgumentException("Bad array parameters - offset = " + offset + ", length = " + length);
}
this.mCurBuffer = buffer;
this.mCurBase = - offset;
this.mCurTop = length;
this.mLength = length;
}
public ByteArrayByteWriter(ArrayList bufferArray, int length) {
this.mCurIndex = 0;
this.mCurBuffer = (byte[])bufferArray.get(this.mCurIndex);
this.mBufferArray = bufferArray;
this.mCurBase = 0;
this.mCurTop = Math.min(length, this.mCurBuffer.length);
this.mLength = length;
int bits = 32;
int counter = 0;
while (bits != 0) {
bits >>= 1;
++counter;
}
this.mMinBufBit = counter - 1;
bits = 262144;
counter = 0;
while (bits != 0) {
bits >>= 1;
++counter;
}
this.mMaxBufBit = counter - 1;
}
public byte[] toByteArray() {
byte[] bufferCopy = new byte[this.mLength];
int index = 0;
int totalCount = 0;
while (totalCount < this.mLength) {
byte[] thisBuffer = (byte[])this.mBufferArray.get(index);
int thisCount = Math.min(this.mLength - totalCount, thisBuffer.length);
System.arraycopy(thisBuffer, 0, bufferCopy, totalCount, thisCount);
totalCount += thisCount;
++index;
}
return bufferCopy;
}
@Override
public void write(long position, int b) throws IOException {
if (position < (long)this.mCurBase || position >= (long)this.mCurTop) {
if (position < 0 || position >= Integer.MAX_VALUE) {
throw new IOException("Attempt to position outside the buffer.");
}
if (position >= (long)this.mTotalSize) {
this.resizeBuffer(position + 1);
}
this.reloadBuffer(position);
}
if (position >= (long)this.mLength) {
this.mLength = (int)position + 1;
}
this.mCurBuffer[(int)position - this.mCurBase] = (byte)b;
}
@Override
public void write(long position, byte[] b, int offset, int length) throws IOException {
if (position < 0 || position + (long)length > Integer.MAX_VALUE) {
throw new IOException("Attempt to position outside the buffer.");
}
if (offset < 0 || length < 0 || offset + length > b.length) {
throw new IOException("Invalid offset/length on the array.");
}
if (length == 0) {
return;
}
if (position + (long)length > (long)this.mLength) {
this.mLength = (int)position + length;
}
if (position >= (long)this.mCurBase && position + (long)length <= (long)this.mCurTop) {
System.arraycopy(b, offset, this.mCurBuffer, (int)position - this.mCurBase, length);
} else {
if (position + (long)length > (long)this.mTotalSize) {
this.resizeBuffer(position + (long)length);
}
if (position < (long)this.mCurBase || position >= (long)this.mCurTop) {
this.reloadBuffer(position);
}
int totalWritten = 0;
while (totalWritten < length) {
int thisWrite = Math.min(length - totalWritten, this.mCurTop - (int)position);
System.arraycopy(b, offset, this.mCurBuffer, (int)position - this.mCurBase, thisWrite);
position += (long)thisWrite;
offset += thisWrite;
if ((totalWritten += thisWrite) >= length) continue;
++this.mCurIndex;
this.mCurBase = this.mCurTop;
this.mCurBuffer = (byte[])this.mBufferArray.get(this.mCurIndex);
this.mCurTop = this.mCurBase + this.mCurBuffer.length;
}
}
}
private void reloadBuffer(long position) {
if (position < 32) {
this.mCurIndex = 0;
this.mCurBase = 0;
} else if (position == (long)this.mCurTop) {
++this.mCurIndex;
this.mCurBase = this.mCurTop;
} else if (position >= 262144) {
this.mCurIndex = (int)position / 262144 + this.mMaxBufBit - this.mMinBufBit;
this.mCurBase = (int)position & -262144;
} else {
int index = this.mMaxBufBit - this.mMinBufBit + 1;
int bits = (int)position << 31 - this.mMaxBufBit;
while (--index != 0 && ((bits <<= 1) & Integer.MIN_VALUE) == 0) {
}
this.mCurIndex = index;
this.mCurBase = 1 << index + this.mMinBufBit - 1 & -32;
}
this.mCurBuffer = (byte[])this.mBufferArray.get(this.mCurIndex);
this.mCurTop = this.mCurBase + this.mCurBuffer.length;
}
private void resizeBuffer(long newSize) throws IOException {
while ((long)this.mTotalSize < newSize) {
int bufferSize = 0;
int arraySize = this.mBufferArray.size();
bufferSize = arraySize < 2 ? 32 : ((bufferSize = ((byte[])this.mBufferArray.get(arraySize - 1)).length) < 262144 ? (bufferSize *= 2) : 262144);
this.mBufferArray.add(new byte[bufferSize]);
this.mTotalSize += bufferSize;
}
}
@Override
public long length() throws IOException {
return this.mLength;
}
@Override
public void flush() throws IOException {
}
@Override
public void close() throws IOException {
}
public String toString() {
return super.toString();
}
@Override
public int read(long position) throws IOException {
if (position < 0 || position >= (long)this.mLength) {
return -1;
}
if (position < (long)this.mCurBase || position >= (long)this.mCurTop) {
this.reloadBuffer(position);
}
return this.mCurBuffer[(int)position - this.mCurBase] & 255;
}
@Override
public int read(long position, byte[] b, int offset, int length) throws IOException {
if (position < 0 || position >= (long)this.mLength) {
return -1;
}
if (offset < 0 || length < 0 || offset + length > b.length) {
throw new IOException("Invalid offset/length on the array.");
}
if (length == 0) {
return 0;
}
length = (int)Math.min((long)length, (long)this.mLength - position);
if (position >= (long)this.mCurBase && position + (long)length <= (long)this.mCurTop) {
System.arraycopy(this.mCurBuffer, (int)position - this.mCurBase, b, offset, length);
} else {
if (position < (long)this.mCurBase || position >= (long)this.mCurTop) {
this.reloadBuffer(position);
}
int totalRead = 0;
while (totalRead < length) {
int thisRead = Math.min(length - totalRead, this.mCurTop - (int)position);
System.arraycopy(this.mCurBuffer, (int)position - this.mCurBase, b, offset, thisRead);
position += (long)thisRead;
offset += thisRead;
if ((totalRead += thisRead) >= length) continue;
++this.mCurIndex;
this.mCurBase = this.mCurTop;
this.mCurBuffer = (byte[])this.mBufferArray.get(this.mCurIndex);
this.mCurTop = Math.min(this.mLength, this.mCurBase + this.mCurBuffer.length);
}
}
return length;
}
}