IO.java
6.26 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
/*
* Decompiled with CFR 0_118.
*/
package com.adobe.internal.io.stream;
import com.adobe.internal.io.stream.BitInputStream;
import com.adobe.internal.io.stream.InputByteStream;
import com.adobe.internal.io.stream.OutputByteStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public final class IO {
private static final int DEFAULT_CHUNK_SIZE = 32768;
private static final boolean USE_THREADLOCAL_BUFFER = true;
private static ThreadLocal<byte[]> tlBuffer = new ThreadLocal<byte[]>(){
@Override
protected synchronized byte[] initialValue() {
return new byte[32768];
}
};
private static byte[] getBuffer(boolean useThreadLocalBuffer) {
if (useThreadLocalBuffer) {
return tlBuffer.get();
}
return new byte[32768];
}
private IO() {
}
public static final long copy(InputStream is, OutputStream os) throws IOException {
byte[] buf = IO.getBuffer(true);
long bytesCopied = 0;
int bytes = 0;
while ((bytes = is.read(buf)) != -1) {
os.write(buf, 0, bytes);
bytesCopied += (long)bytes;
}
return bytesCopied;
}
public static final long copy(InputStream is, long length, OutputStream os) throws IOException {
return IO.copyInternal(is, length, os, true);
}
static long copyInternal(InputStream is, long length, OutputStream os, boolean useThreadLocalBuffer) throws IOException {
byte[] buf = IO.getBuffer(useThreadLocalBuffer);
long bytesToCopy = length;
int bytesThisRead = 0;
long bytesCopied = 0;
while (bytesToCopy > 0 && (bytesThisRead = is.read(buf, 0, bytesToCopy > (long)buf.length ? buf.length : (int)bytesToCopy)) != -1) {
os.write(buf, 0, bytesThisRead);
bytesToCopy -= (long)bytesThisRead;
bytesCopied += (long)bytesThisRead;
}
return bytesCopied;
}
public static final long copy(BitInputStream is, OutputStream os, int bitsPerValue) throws IOException {
byte[] buf = IO.getBuffer(true);
long bytesCopied = 0;
int bytes = 0;
while ((bytes = is.read(buf, bitsPerValue, buf.length)) != 0) {
os.write(buf, 0, bytes);
bytesCopied += (long)bytes;
}
return bytesCopied;
}
public static final long copy(BitInputStream is, long length, OutputStream os, int bitsPerValue) throws IOException {
byte[] buf = IO.getBuffer(true);
long bytesToCopy = length;
int bytesThisRead = 0;
long bytesCopied = 0;
while (bytesToCopy > 0 && (bytesThisRead = is.read(buf, bitsPerValue, bytesToCopy > (long)buf.length ? buf.length : (int)bytesToCopy)) != 0) {
os.write(buf, 0, bytesThisRead);
bytesToCopy -= (long)bytesThisRead;
bytesCopied += (long)bytesThisRead;
}
return bytesCopied;
}
public static final long copy(InputByteStream is, OutputByteStream os) throws IOException {
return IO.copy(is, 0, is.length(), os);
}
public static final long copy(InputByteStream is, OutputStream os) throws IOException {
return IO.copy(is, 0, is.length(), os);
}
public static final long copy(InputByteStream is, long offset, long length, OutputByteStream os) throws IOException {
byte[] buf = IO.getBuffer(true);
is.seek(offset);
long bytesToCopy = length;
int bytesThisRead = 0;
long bytesCopied = 0;
while (bytesToCopy > 0 && (bytesThisRead = is.read(buf)) != -1) {
if ((long)bytesThisRead < bytesToCopy) {
os.write(buf, 0, bytesThisRead);
bytesToCopy -= (long)bytesThisRead;
bytesCopied += (long)bytesThisRead;
continue;
}
os.write(buf, 0, (int)bytesToCopy);
bytesToCopy = 0;
bytesCopied += (long)bytesThisRead;
}
return bytesCopied;
}
public static final long copy(InputByteStream is, long offset, long length, OutputStream os, int blockSize) throws IOException {
if (blockSize < 1) {
throw new IOException("Block size can't be smaller than 1 byte.");
}
byte[] buf = null;
buf = blockSize <= 32768 ? IO.getBuffer(true) : new byte[blockSize];
is.seek(offset);
long bytesToCopy = length;
int bytesThisRead = 0;
long bytesCopied = 0;
while ((bytesThisRead = is.read(buf, 0, blockSize)) != -1 && bytesToCopy > 0) {
if ((long)bytesThisRead < bytesToCopy) {
os.write(buf, 0, bytesThisRead);
bytesToCopy -= (long)bytesThisRead;
} else {
os.write(buf, 0, (int)bytesToCopy);
bytesToCopy = 0;
}
bytesCopied += (long)bytesThisRead;
}
return bytesCopied;
}
public static final long copy(InputByteStream is, long offset, long length, OutputStream os) throws IOException {
return IO.copy(is, offset, length, os, 32768);
}
public static final byte[] longToByteArray(long value, int width) {
byte[] bytes = new byte[width];
while (--width >= 0) {
bytes[width] = (byte)value;
value >>= 8;
}
return bytes;
}
public static final byte[] inputByteStreamToArray(InputByteStream byteStream) throws IOException {
int length = (int)byteStream.length();
byte[] bytes = new byte[length];
long len = byteStream.read(bytes);
return len > 0 ? bytes : null;
}
public static long copy(InputStream is, OutputByteStream obs) throws IOException {
byte[] buf = IO.getBuffer(true);
int bytes = 0;
long bytesCopied = 0;
while (is.available() > 0 && (bytes = is.read(buf)) != -1) {
obs.write(buf, 0, bytes);
bytesCopied += (long)bytes;
}
return bytesCopied;
}
public static int compareInputByteStreams(InputByteStream stream1, InputByteStream stream2) throws IOException {
int b1;
int b2;
do {
if ((b1 = stream1.read()) == (b2 = stream2.read())) continue;
return b1 - b2;
} while (b1 != -1);
return b1 - b2;
}
}