forked from suitmedia/kirk
/
RewindableInputStream.java
373 lines (279 loc) · 7.46 KB
/
RewindableInputStream.java
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package com.strobecorp.kirk;
import java.io.File;
import java.io.FilterInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
// Special jruby specific methods
import org.jruby.RubyString;
public class RewindableInputStream extends FilterInputStream {
// The default buffer size
public static final int DEFAULT_BUFFER_SIZE = 8192;
// The tmp files prefix
public static final String TMPFILE_PREFIX = "kirk-rewindable-input";
// The tmp file's suffix
public static final String TMPFILE_SUFFIX = "";
// The in memory buffer, the wrapped stream will be buffered
// in memory until this buffer is full, then it will be written
// to a temp file.
private ByteBuffer buf;
// The wrapped stream converted to a Channel
private ReadableByteChannel io;
// The total number of bytes buffered
private long buffered;
// The current position within the stream
private long position;
// The last remembered position
private long mark;
// The on disk stream buffer
private FileChannel tmpFile;
public RewindableInputStream(InputStream io) {
this(io, DEFAULT_BUFFER_SIZE);
}
public RewindableInputStream(InputStream io, int bufSize) {
this(io, ByteBuffer.allocate(bufSize));
}
public RewindableInputStream(InputStream io, ByteBuffer buf) {
super(io);
this.buffered = 0;
this.position = 0;
this.mark = -1;
this.io = Channels.newChannel(io);
this.buf = buf;
}
public long getPosition() {
return position;
}
public InputStream getUnbufferedInputStream() {
return in;
}
@Override
public synchronized int available() throws IOException {
long available = buffered - position;
ensureOpen();
if ( available > Integer.MAX_VALUE ) {
available = Integer.MAX_VALUE;
}
else if ( available < 0 ) {
throw new IOException("Somehow the stream travelled to the future :(");
}
return (int) available;
}
@Override
public synchronized void close() throws IOException {
if ( buf == null ) {
return;
}
if ( tmpFile != null ) {
tmpFile.close();
}
io.close();
buf = null;
}
@Override
public synchronized void mark(int readlimit) {
this.mark = this.position;
}
@Override
public boolean markSupported() {
return true;
}
@Override
public synchronized int read() throws IOException {
long len;
ensureOpen();
while ( true ) {
len = fillBuf(1);
if ( len == -1 ) {
return -1;
}
else if ( len == 1 ) {
return buf.get();
}
throw new IOException("WTF mate");
}
}
@Override
public synchronized int read(byte[] buffer, int offset, int length) throws IOException {
int count = 0;
int len;
ensureOpen();
while ( count < length ) {
// Fill the buffer
len = (int) fillBuf(length - count);
// Handle EOFs
if ( len == -1 ) {
if ( count == 0 ) {
return -1;
}
return count;
}
buf.get(buffer, offset + count, len);
position += len;
count += len;
}
return count;
}
public byte[] read(int length) throws IOException {
byte[] bytes = new byte[length];
int len = read(bytes, 0, length);
if ( len == -1 ) {
return null;
}
else if ( len != length ) {
byte[] sized = new byte[len];
System.arraycopy(bytes, 0, sized, 0, len);
bytes = sized;
}
return bytes;
}
// public synchronized int readAndAppendTo(RubyString str, int length) throws IOException {
// byte[] bytes = new byte[length];
// int len = read(bytes, 0, length);
// if ( len > 0 ) {
// str.getByteList().append(bytes, 0, len);
// }
// return len;
// }
@Override
public synchronized void reset() throws IOException {
if ( mark < 0 ) {
throw new IOException("The marked position is invalid");
}
position = mark;
}
// XXX This is completely busted ;-)
@Override
public synchronized long skip(long amount) throws IOException {
long count = 0;
long len;
ensureOpen();
while ( amount > count ) {
len = fillBuf(amount - count);
if ( len == -1 ) {
break;
}
count += len;
}
return count;
}
public synchronized void seek(long newPosition) throws IOException {
ensureOpen();
if ( newPosition < 0 ) {
throw new IOException("Cannot seek to a negative position");
}
this.position = newPosition;
}
public synchronized void rewind() throws IOException {
seek(0);
}
private void ensureOpen() throws IOException {
if ( buf == null ) {
throw new IOException("IO is closed");
}
}
private long fillBuf(long length) throws IOException {
if ( isOnDisk() || position + length > buf.capacity() ) {
// Rotate the buffer to disk if it hasn't been done yet
if ( !isOnDisk() ) {
rotateToTmpFile();
}
return fillBufFromTmpFile(length);
}
else {
return fillBufFromMem(length);
}
}
private long fillBufFromMem(long length) throws IOException {
long limit;
int len;
limit = position + length;
if ( buffered < limit ) {
buf.limit((int) limit).position((int) buffered);
len = io.read(buf);
if ( len == -1 ) {
if ( buffered <= position ) {
return -1;
}
}
else {
buffered += len;
if ( buffered <= position ) {
return 0;
}
}
}
buf.limit((int) limit).position((int) position);
return Math.min(buffered - position, length);
}
private long fillBufFromTmpFile(long length) throws IOException {
long count = 0;
int len;
// If we haven't buffered far enough, then do it
if ( buffered < position ) {
// Bail with an EOF if there isn't enough data to buffer
// to the requested position.
if ( !bufferTo(position) ) {
return -1;
}
}
buf.clear().limit(Math.min((int) length, buf.capacity()));
if ( buffered > position ) {
tmpFile.position(position);
len = tmpFile.read(buf);
buf.flip();
return len;
}
else {
// Read from the network
len = io.read(buf);
if ( len == -1 ) {
return -1;
}
buf.flip();
tmpFile.position(buffered);
tmpFile.write(buf);
buffered += len;
buf.position(0);
return len;
}
}
private boolean bufferTo(long pos) throws IOException {
long limit;
int len;
while ( buffered < pos ) {
limit = Math.min(pos - buffered, buf.capacity());
buf.clear().limit((int) limit);
len = io.read(buf);
if ( len == -1 ) {
return false;
}
buf.flip();
tmpFile.position(buffered);
tmpFile.write(buf);
buffered += len;
}
return true;
}
private boolean isInMemory() {
return tmpFile == null;
}
private boolean isOnDisk() {
return tmpFile != null;
}
private void rotateToTmpFile() throws IOException {
File file;
RandomAccessFile fileStream;
file = File.createTempFile(TMPFILE_PREFIX, TMPFILE_SUFFIX);
file.deleteOnExit();
fileStream = new RandomAccessFile(file, "rw");
tmpFile = fileStream.getChannel();
buf.clear().position(0).limit((int) buffered);
tmpFile.write(buf);
buf.clear();
}
}