Class IsolatedOutputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable

    public class IsolatedOutputStream
    extends java.io.OutputStream
    OutputStream isolated from interrupts.

    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.

    Since:
    4.6
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.concurrent.ExecutorService copier  
      private java.io.OutputStream dst  
      private java.util.concurrent.Future<java.lang.Void> pending  
    • Constructor Summary

      Constructors 
      Constructor Description
      IsolatedOutputStream​(java.io.OutputStream out)
      Wraps an OutputStream.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method 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)
      • Methods inherited from class java.io.OutputStream

        write
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • dst

        private final java.io.OutputStream dst
      • copier

        private final java.util.concurrent.ExecutorService copier
      • pending

        private java.util.concurrent.Future<java.lang.Void> pending
    • Constructor Detail

      • IsolatedOutputStream

        public IsolatedOutputStream​(java.io.OutputStream out)
        Wraps an OutputStream.
        Parameters:
        out - stream to send all writes to.
    • Method Detail

      • write

        public void write​(int ch)
                   throws java.io.IOException
        Specified by:
        write in class java.io.OutputStream
        Throws:
        java.io.IOException
      • write

        public void write​(byte[] buf,
                          int pos,
                          int cnt)
                   throws java.io.IOException
        Overrides:
        write in class java.io.OutputStream
        Throws:
        java.io.IOException
      • flush

        public void flush()
                   throws java.io.IOException
        Specified by:
        flush in interface java.io.Flushable
        Overrides:
        flush in class java.io.OutputStream
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.OutputStream
        Throws:
        java.io.IOException
      • tryCleanClose

        private boolean tryCleanClose()
      • cleanClose

        private void cleanClose()
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • dirtyClose

        private void dirtyClose()
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • checkClosed

        private void checkClosed()
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • execute

        private void execute​(java.util.concurrent.Callable<java.lang.Void> task)
                      throws java.io.IOException
        Throws:
        java.io.IOException
      • checkedGet

        private static void checkedGet​(java.util.concurrent.Future<java.lang.Void> future)
                                throws java.io.IOException
        Throws:
        java.io.IOException
      • interrupted

        private static java.io.InterruptedIOException interrupted​(java.lang.InterruptedException c)