Class PipeReader

java.lang.Object
de.tsl2.nano.util.PipeReader
All Implemented Interfaces:
Runnable

public class PipeReader extends Object implements Runnable
Piped Stream connector. Is able to connect one or two streams with a pipe. For further informations, see PipedInputStream and PipedOutputStream. Use readInThreads(InputStream, OutputStream) to let it do the whole work.

Their are two use cases:
The standard one will write to the given output-stream, reading input from a print-stream (see getPrintStream() - creating one new thread. You should connect the print-stream to the desired instance (like System.out).
The more complicated use case will create two threads. one to read from given input and writing to the pipe, the second reading the pipe and writing to the given output. usefull for en-decodings.

WARNING: be sure, your output stream doesn't write again to the printstream - this would be done inside the same thread with a deadlock.

Version:
$Revision$
Author:
Thomas Schneider
  • Constructor Details

    • PipeReader

      public PipeReader(InputStream in, OutputStream out)
      constructor
      Parameters:
      in - (optional) input stream (not yet an PipedInputStream!) to be connected to the output stream. if null, only a pipe from to the output-stream will be set.
      po - output stream (not yet an PipedOutputStream!) to be connected to the input stream.
  • Method Details

    • getPrintStream

      public PrintStream getPrintStream()
      getPrintStream
      Returns:
      printstream, connected to the pipe. if you didn't give an input stream you should connect this printstream to an instance (e.g. System.out).
    • getConnectionPipe

      protected PipedOutputStream getConnectionPipe()
    • run

      public void run()
      Specified by:
      run in interface Runnable
    • readInThreads

      public static PrintStream readInThreads(InputStream in, OutputStream out)
      creates a PipeReader starting one (if in is null) or two threads to connect the streams through a pipe.
      Parameters:
      in - (optional) input stream (not yet an PipedInputStream!) to be connected to the output stream. if null, only a pipe from to the output-stream will be set.
      po - output stream (not yet an PipedOutputStream!) to be connected to the input stream.
      Returns:
      a printstream that may be set to an output stream (e.g. System.out or/and System.err). makes sense if you don't give any input stream.