jlibs.core.io
Class PumpedInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.PipedInputStream
          extended by jlibs.core.io.PumpedInputStream
All Implemented Interfaces:
Closeable, Runnable

public abstract class PumpedInputStream
extends PipedInputStream
implements Runnable

This class simplifies usage of PipedInputStream and PipedOutputStream

Using PipedInputStream and PipedOutputStream looks cumbersome.

 PipedInputStream pipedIn = new PipedInputStream();
 final PipedOutputStream pipedOut = new PipedOutputStream(pipedIn);
 final IOException ioEx[] = { null };
 new Thread(){
     @Override
     public void run(){
         try{
             writeDataTo(pipedOut);
             pipedOut.close();
         }catch(IOException ex){
             ioEx[0] = ex;
         }
     }
 }.start();
 readDataFrom(pipedIn);
 pipedIn.close();
 if(ioEx[0]!=null)
     throw new RuntimeException("something gone wrong", ioEx[0]);
 
The same can be achieved using PumpedInputStream as follows:
 PumpedInputStream in = new PumpedInputStream(){
     @Override
     protected void pump(PipedOutputStream out) throws Exception{
         writeDataTo(out);
     }
 }.start(); // start() will spawn new thread
 readDataFrom(in);
 in.close(); // any exceptions occurred in pump(...) are thrown by close()
 
PumpedInputStream is an abstract class with following abstract method:
 protected abstract void pump(PipedOutputStream out) throws Exception;
 
This method implementation should write data into out which is passed as argument and close it.
Any exception thrown by pump(...) are wrapped in IOException and rethrown by PumpedReader.close().

PumpedInputStream implements Runnable which is supposed to be run in thread.
You can use PumpedInputStream.start() method to start thread or spawn thread implicitly.
start() method returns self reference.

 public PumpedInputStream start();
 
The advantage of PumpedInputStream over PipedInputStream/PipedOutputStream/Thread is:

Author:
Santhosh Kumar T
See Also:
PumpedReader

Field Summary
 
Fields inherited from class java.io.PipedInputStream
buffer, in, PIPE_SIZE
 
Constructor Summary
PumpedInputStream()
           
 
Method Summary
 void close()
          Closes this stream and releases any system resources associated with the stream.
protected abstract  void pump(PipedOutputStream out)
          Subclasse implementation should write data into writer.
 void run()
           
 PumpedInputStream start()
          Starts a thread with this instance as runnable.
 
Methods inherited from class java.io.PipedInputStream
available, connect, read, read, receive
 
Methods inherited from class java.io.InputStream
mark, markSupported, read, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PumpedInputStream

public PumpedInputStream()
Method Detail

run

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

start

public PumpedInputStream start()
Starts a thread with this instance as runnable.

Returns:
self reference

close

public void close()
           throws IOException
Closes this stream and releases any system resources associated with the stream.

Any exception thrown by pump(java.io.PipedOutputStream) are cached and rethrown by this method

Specified by:
close in interface Closeable
Overrides:
close in class PipedInputStream
Throws:
IOException - if an I/O error occurs.

pump

protected abstract void pump(PipedOutputStream out)
                      throws Exception
Subclasse implementation should write data into writer.

Parameters:
out - outputstream into which data should be written
Throws:
Exception


Copyright © 2018. All rights reserved.