Class QueueOutputStream

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

    public class QueueOutputStream
    extends java.io.OutputStream
    Simple alternative to JDK PipedOutputStream; queue input stream provides what's written in queue output stream.

    Example usage:

     QueueOutputStream outputStream = new QueueOutputStream();
     QueueInputStream inputStream = outputStream.newPipeInputStream();
    
     outputStream.write("hello world".getBytes(UTF_8));
     inputStream.read();
     
    Unlike JDK PipedInputStream and PipedOutputStream, queue input/output streams may be used safely in a single thread or multiple threads. Also, unlike JDK classes, no special meaning is attached to initial or current thread. Instances can be used longer after initial threads exited.

    Closing a QueueOutputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

    Since:
    2.9.0
    See Also:
    QueueInputStream
    • Constructor Summary

      Constructors 
      Constructor Description
      QueueOutputStream()
      Constructs a new instance with no limit to internal buffer size.
      QueueOutputStream​(java.util.concurrent.BlockingQueue<java.lang.Integer> blockingQueue)
      Constructs a new instance with given buffer.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      QueueInputStream newQueueInputStream()
      Creates a new QueueInputStream instance connected to this.
      void write​(int b)
      Writes a single byte.
      • Methods inherited from class java.io.OutputStream

        close, flush, nullOutputStream, write, write
      • Methods inherited from class java.lang.Object

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

      • QueueOutputStream

        public QueueOutputStream()
        Constructs a new instance with no limit to internal buffer size.
      • QueueOutputStream

        public QueueOutputStream​(java.util.concurrent.BlockingQueue<java.lang.Integer> blockingQueue)
        Constructs a new instance with given buffer.
        Parameters:
        blockingQueue - backing queue for the stream
    • Method Detail

      • newQueueInputStream

        public QueueInputStream newQueueInputStream()
        Creates a new QueueInputStream instance connected to this. Writes to this output stream will be visible to the input stream.
        Returns:
        QueueInputStream connected to this stream
      • write

        public void write​(int b)
                   throws java.io.InterruptedIOException
        Writes a single byte.
        Specified by:
        write in class java.io.OutputStream
        Throws:
        java.io.InterruptedIOException - if the thread is interrupted while writing to the queue.