-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPipelinedOutputStream.java
355 lines (326 loc) · 13 KB
/
PipelinedOutputStream.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
/*
* PipelinedOutputStream.java
*
* Author: Luke Hutchison
*
* Hosted at: https://github.com/lukehutch/PipelinedOutputStream
*
* --
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Luke Hutchison
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without
* limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
* EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
* OR OTHER DEALINGS IN THE SOFTWARE.
*/
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayDeque;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
// TODO: Auto-generated Javadoc
/**
* Buffer an {@link OutputStream} in a separate process, to separate data generation from data compression or
* writing to disk.
*/
public class PipelinedOutputStream extends OutputStream {
/** An executor service for the consumer thread. */
private ExecutorService executor;
/** The consumer stream. */
private ConsumerStream consumerStream;
/** The {@link Future} used to await termination of the consumer thread. */
private Future<Void> consumerStreamFuture;
/** The buffered output stream inserted between the producer and consumer. */
private final BufferedOutputStream bufferedOutputStream;
/** Tracks the number of bytes written. */
private final AtomicLong bytesWrittenTracker;
/** Used to generate unique thread names. */
private static final AtomicInteger streamIndex = new AtomicInteger();
/** True when the stream has been closed. */
private final AtomicBoolean closed = new AtomicBoolean(false);
/**
* The Class BufferChunk.
*/
private static class BufferChunk {
/** The buffer. */
byte[] buffer = new byte[CHUNK_SIZE];
/** The number of bytes used in the buffer. */
int len;
/** The poison pill to use to mark end of stream. */
static final BufferChunk POISON_PILL = new BufferChunk();
/**
* The buffer chunk size to use. Should probably be 8192, since that is the default used by Java buffers.
*/
private static final int CHUNK_SIZE = 8192;
/**
* Write the buffer chunk to an {@link OutputStream}.
*
* @param out the out
* @throws IOException Signals that an I/O exception has occurred.
*/
void writeTo(OutputStream out) throws IOException {
out.write(buffer, 0, len);
}
}
/**
* The {@link OutputStream} for the consumer thread.
*/
private static class ConsumerStream extends OutputStream implements Callable<Void> {
/** The output stream to write consumed bytes to. */
private OutputStream out;
/** The consumer stream {@link Thread} reference. */
private AtomicReference<Thread> consumerStreamThread = new AtomicReference<>();
/** True when the consumer stream has been closed. */
private final AtomicBoolean consumerStreamClosed = new AtomicBoolean(false);
/** The buffer chunks that have not yet been consumed. */
private final ArrayBlockingQueue<BufferChunk> bufferChunks;
/** Recycler for buffer chunks. */
private final ArrayDeque<BufferChunk> bufferChunkRecycler;
/**
* Instantiate a new consumer stream.
*
* @param bufSize the size of the buffer to insert between producer and consumer.
* @param outputStream the stream to send consumed output to.
*/
public ConsumerStream(int bufSize, OutputStream outputStream) {
if (bufSize <= 0) {
throw new IllegalArgumentException("bufSize must be greater than 0");
}
out = outputStream;
int numBufferChunks = (bufSize + BufferChunk.CHUNK_SIZE - 1) / BufferChunk.CHUNK_SIZE;
bufferChunks = new ArrayBlockingQueue<>(numBufferChunks);
bufferChunkRecycler = new ArrayDeque<>(numBufferChunks);
}
/**
* Executed in consumer thread.
*
* @return null
* @throws Exception if anything goes wrong while writing.
*/
@Override
public Void call() throws Exception {
consumerStreamThread.set(Thread.currentThread());
try {
while (true) {
try {
BufferChunk chunk = bufferChunks.take();
if (chunk == BufferChunk.POISON_PILL) {
break;
}
chunk.writeTo(out);
bufferChunkRecycler.add(chunk);
} catch (Exception e) {
throw e;
}
}
} finally {
// Close the underlying OutputStream once the buffer is flushed, or if exception is thrown
consumerStreamClosed.set(true);
out.close();
}
return null;
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
if (len > BufferChunk.CHUNK_SIZE) {
// If b is too big for buf, break b into chunks of size buf.length
int remaining = len;
for (int i = 0; i < len; i += BufferChunk.CHUNK_SIZE) {
int bytesToWrite = Math.min(BufferChunk.CHUNK_SIZE, remaining);
write(b, off + i, bytesToWrite);
remaining -= bytesToWrite;
}
} else {
if (consumerStreamClosed.get()) {
throw new IOException("Tried to write to closed stream");
}
BufferChunk bufferChunk = bufferChunkRecycler.poll();
if (bufferChunk == null) {
bufferChunk = new BufferChunk();
}
for (int o = 0, i = off; o < len;) {
bufferChunk.buffer[o++] = b[i++];
}
bufferChunk.len = len;
try {
bufferChunks.put(bufferChunk);
} catch (InterruptedException e) {
throw new IOException(e);
}
}
}
@Override
public synchronized void write(int b) throws IOException {
if (consumerStreamClosed.get()) {
throw new IOException("Tried to write to closed stream");
}
BufferChunk bufferChunk = bufferChunkRecycler.poll();
if (bufferChunk == null) {
bufferChunk = new BufferChunk();
}
bufferChunk.buffer[0] = (byte) b;
bufferChunk.len = 1;
try {
bufferChunks.put(bufferChunk);
} catch (InterruptedException e) {
throw new IOException(e);
}
}
@Override
public void close() throws IOException {
if (!consumerStreamClosed.getAndSet(true)) {
try {
bufferChunks.put(BufferChunk.POISON_PILL);
} catch (InterruptedException e) {
throw new IOException(e);
}
}
}
}
/**
* Instantiate a new pipelined output stream.
*
* @param bufSize the size of the buffer to insert between producer and consumer (mostly to collect
* together single-byte {@link OutputStream#write(int)} operations into chunks).
* @param outputStream the output stream to write to
* @param bytesWrittenTracker an {@link AtomicLong} to use to track the number of bytes written.
* @throws IOException Signals that an I/O exception has occurred.
*/
public PipelinedOutputStream(int bufSize, OutputStream outputStream, AtomicLong bytesWrittenTracker)
throws IOException {
this.bytesWrittenTracker = bytesWrittenTracker;
executor = Executors.newFixedThreadPool(1, new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
final Thread thread = new Thread(r,
PipelinedOutputStream.class.getSimpleName() + "-" + streamIndex.getAndIncrement());
thread.setDaemon(true);
return thread;
}
});
consumerStream = new ConsumerStream(bufSize, outputStream);
bufferedOutputStream = new BufferedOutputStream(consumerStream);
consumerStreamFuture = executor.submit(consumerStream);
}
/**
* Instantiate a new pipelined output stream.
*
* @param bufSize the size of the buffer to insert between producer and consumer (mostly to collect
* together single-byte {@link OutputStream#write(int)} operations into chunks).
* @param outputStream the output stream to write to
* @throws IOException Signals that an I/O exception has occurred.
*/
public PipelinedOutputStream(int bufSize, OutputStream out) throws IOException {
this(bufSize, out, new AtomicLong());
}
/**
* Write an array of bytes.
*
* @param b the array of bytes to write.
* @throws IOException Signals that an I/O exception has occurred.
*/
@Override
public void write(byte[] b) throws IOException {
bufferedOutputStream.write(b);
bytesWrittenTracker.addAndGet(b.length);
}
/**
* Write a range of bytes from an array.
*
* @param b the array of bytes.
* @param off the start offset within the array.
* @param len the number of bytes to write from the array.
* @throws IOException Signals that an I/O exception has occurred.
*/
@Override
public void write(byte[] b, int off, int len) throws IOException {
bufferedOutputStream.write(b, off, len);
bytesWrittenTracker.addAndGet(len);
}
/**
* Write a single byte.
*
* @param b the byte.
* @throws IOException Signals that an I/O exception has occurred.
*/
@Override
public synchronized void write(int b) throws IOException {
bufferedOutputStream.write(b);
bytesWrittenTracker.incrementAndGet();
}
/**
* Get the number of bytes written so far.
*
* @return the number of bytes written so far.
*/
public long getBytesWritten() {
return bytesWrittenTracker.get();
}
/**
* Close the stream.
*
* @throws IOException Signals that an I/O exception has occurred.
*/
@Override
public void close() throws IOException {
if (!closed.getAndSet(true)) {
bufferedOutputStream.close();
try {
// Block on consumer thread completion
consumerStreamFuture.get();
} catch (InterruptedException e) {
// Ignore
} catch (ExecutionException e) {
throw new IOException(e.getCause());
}
if (executor != null) {
// Try to shut down executor service cleanly
ExecutorService ex = executor;
executor = null;
try {
ex.shutdown();
} catch (final SecurityException e) {
// Ignore
}
boolean terminated = false;
try {
// Await termination
terminated = ex.awaitTermination(5000, TimeUnit.MILLISECONDS);
} catch (final InterruptedException e) {
// Ignore
}
if (!terminated) {
try {
ex.shutdownNow();
} catch (final SecurityException e) {
throw new RuntimeException(e);
}
}
}
}
}
}