public class IsolatedOutputStream
extends java.io.OutputStream
Wraps an OutputStream to prevent interrupts during writes from being made visible to that stream instance. This works around buggy or difficult OutputStream implementations like JSch that cannot gracefully handle an interrupt during write.
Every write (or flush) requires a context switch to another thread. Callers
should wrap this stream with BufferedOutputStream
using a suitable
buffer size to amortize the cost of context switches.
Modifier and Type | Class and Description |
---|---|
private static class |
IsolatedOutputStream.NamedThreadFactory |
Modifier and Type | Field and Description |
---|---|
private java.util.concurrent.ExecutorService |
copier |
private java.io.OutputStream |
dst |
private java.util.concurrent.Future<java.lang.Void> |
pending |
Constructor and Description |
---|
IsolatedOutputStream(java.io.OutputStream out)
Wraps an OutputStream.
|
Modifier and Type | Method and Description |
---|---|
private void |
checkClosed() |
private static void |
checkedGet(java.util.concurrent.Future<java.lang.Void> future) |
private void |
cleanClose() |
void |
close() |
private void |
dirtyClose() |
private void |
execute(java.util.concurrent.Callable<java.lang.Void> task) |
void |
flush() |
private static java.io.InterruptedIOException |
interrupted(java.lang.InterruptedException c) |
private boolean |
tryCleanClose() |
void |
write(byte[] buf,
int pos,
int cnt) |
void |
write(int ch) |
private final java.io.OutputStream dst
private final java.util.concurrent.ExecutorService copier
private java.util.concurrent.Future<java.lang.Void> pending
public IsolatedOutputStream(java.io.OutputStream out)
out
- stream to send all writes to.public void write(int ch) throws java.io.IOException
write
in class java.io.OutputStream
java.io.IOException
public void write(byte[] buf, int pos, int cnt) throws java.io.IOException
write
in class java.io.OutputStream
java.io.IOException
public void flush() throws java.io.IOException
flush
in interface java.io.Flushable
flush
in class java.io.OutputStream
java.io.IOException
public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.OutputStream
java.io.IOException
private boolean tryCleanClose()
private void cleanClose() throws java.io.IOException
java.io.IOException
private void dirtyClose() throws java.io.IOException
java.io.IOException
private void checkClosed() throws java.io.IOException
java.io.IOException
private void execute(java.util.concurrent.Callable<java.lang.Void> task) throws java.io.IOException
java.io.IOException
private static void checkedGet(java.util.concurrent.Future<java.lang.Void> future) throws java.io.IOException
java.io.IOException
private static java.io.InterruptedIOException interrupted(java.lang.InterruptedException c)