, fooobar.com/questions/1615690/..., CountDownLatch complete.await(), , InputStream , .
, PipedOutpuStream, PipedOutputStream , try-finally-resource, , , 2 .
Supplier<InputStream> streamSupplier = new Supplier<InputStream>() {
@Override
public InputStream get() {
final AtomicReference<IOException> osException = new AtomicReference<>();
final CountDownLatch piped = new CountDownLatch(1);
final PipedInputStream is = new PipedInputStream();
FilterInputStream fis = new FilterInputStream(is) {
@Override
public void close() throws IOException {
try {
IOException e = osException.get();
if (e != null) {
throw new IOException("IOException in writer", e);
}
} finally {
super.close();
}
};
};
Thread t = new Thread(() -> {
try (PipedOutputStream os = new PipedOutputStream(is)) {
piped.countDown();
writeIozToStream(os, projectFile, dataFolder);
} catch (final IOException e) {
osException.set(e);
}
});
t.start();
try {
piped.await();
} catch (InterruptedException e) {
t.cancel();
Thread.currentThread().interrupt();
}
return fis;
}
};
- -
try (InputStream is = streamSupplier.getInputStream()) {
//Read stream in full
}
, InputStream , PipedOutputStream, "Pipe closed" IOException.
complete.await() FilterInputStream close() complete.await()), (PipedInputStream , complete.await(), PipedOutputStream PipedInputStream awaitSpace)