jlibs.core.io
Class PumpedReader
java.lang.Object
java.io.Reader
java.io.PipedReader
jlibs.core.io.PumpedReader
- All Implemented Interfaces:
- Closeable, Readable, Runnable
public abstract class PumpedReader
- extends PipedReader
- implements Runnable
This class simplifies usage of PipedReader and PipedWriter
Using PipedReader and PipedWriter looks cumbersome.
PipedReader pipedReader = new PipedReader();
final PipedWriter pipedWriter = new PipedWriter(pipedReader);
final IOException ioEx[] = { null };
new Thread(){
@Override
public void run(){
try{
writeDataTo(pipedWriter);
pipedWriter.close();
}catch(IOException ex){
ioEx[0] = ex;
}
}
}.start();
readDataFrom(pipedReader);
pipedReader.close();
if(ioEx[0]!=null)
throw new RuntimeException("something gone wrong", ioEx[0]);
The same can be achieved using PumpedReader as follows:
PumpedReader reader = new PumpedReader(){
@Override
protected void pump(PipedWriter writer) throws Exception{
writeDataTo(writer);
}
}.start(); // start() will spawn new thread
readDataFrom(reader);
reader.close(); // any exceptions occurred in pump(...) are thrown by close()
PumpedReader is an abstract class with following abstract method:
protected abstract void pump(PipedWriter writer) throws Exception;
This method implementation should write data into writer which is passed as argument and close it.
Any exception thrown by pump(...) are wrapped in IOException and rethrown by PumpedReader.close().
PumpedReader implements Runnable which is supposed to be run in thread.
You can use PumpedReader.start() method to start thread or spawn thread implicitly.
start() method returns self reference.
public PumpedReader start();
The advantage of PumpedReader over PipedReader/PipedWriter/Thread is:
- it doesn't clutter the exising flow of code
- exception handling is better
- Author:
- Santhosh Kumar T
- See Also:
PumpedInputStream
|
Method Summary |
void |
close()
Closes this stream and releases any system resources
associated with the stream. |
protected abstract void |
pump(PipedWriter writer)
Subclasse implementation should write data into writer. |
void |
run()
|
PumpedReader |
start()
Starts a thread with this instance as runnable. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
PumpedReader
public PumpedReader()
run
public void run()
- Specified by:
run in interface Runnable
start
public PumpedReader 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.PipedWriter)
are cached and rethrown by this method
- Specified by:
close in interface Closeable- Overrides:
close in class PipedReader
- Throws:
IOException - if an I/O error occurs.
pump
protected abstract void pump(PipedWriter writer)
throws Exception
- Subclasse implementation should write data into
writer.
- Parameters:
writer - writer into which data should be written
- Throws:
Exception
Copyright © 2018. All rights reserved.