Class StreamPumper

  • All Implemented Interfaces:
    Runnable

    public class StreamPumper
    extends Object
    implements Runnable
    Copies all data from an input stream to an output stream.
    • Constructor Detail

      • StreamPumper

        public StreamPumper​(InputStream in,
                            OutputStream out,
                            boolean closeWhenExhausted)
        Create a new stream pumper.
        Parameters:
        in - The input stream to read data from.
        out - The output stream to write data to.
        closeWhenExhausted - If true, the output stream will be closed when the input is exhausted.
      • StreamPumper

        public StreamPumper​(InputStream in,
                            OutputStream out)
        Create a new stream pumper.
        Parameters:
        in - The input stream to read data from.
        out - The output stream to write data to.
    • Method Detail

      • setAutoflush

        public void setAutoflush​(boolean autoflush)
        Set whether data should be flushed through to the output stream.
        Parameters:
        autoflush - If true, push through data; if false, let it be buffered.
      • setNonBlocking

        public void setNonBlocking​(boolean nonBlocking)
        Set whether data should be read in a non blocking way.
        Parameters:
        nonBlocking - If true, data will be read in a non blocking mode.
      • run

        public void run()
        Copy data from the input stream to the output stream. Terminate as soon as the input stream is closed or an error occurs.
        Specified by:
        run in interface Runnable
      • isFinished

        public boolean isFinished()
        Tell whether the end of the stream has been reached.
        Returns:
        true if the stream has been exhausted.
      • setBufferSize

        public void setBufferSize​(int bufferSize)
        Set the size in bytes of the read buffer.
        Parameters:
        bufferSize - the buffer size to use.
      • getBufferSize

        public int getBufferSize()
        Get the size in bytes of the read buffer.
        Returns:
        The size of the read buffer.
      • getException

        public Throwable getException()
        Get the exception encountered, if any.
        Returns:
        The Exception encountered; or null if there was none.
      • stop

        public void stop()
        Stop the pumper as soon as possible. Note that it may continue to block on the input stream but it will really stop the thread as soon as it gets EOF or any byte, and it will be marked as finished.