com.gc.iotools.stream.reader
Class ReaderFromWriter<T>

java.lang.Object
  extended by java.io.Reader
      extended by com.gc.iotools.stream.reader.ReaderFromWriter<T>
Type Parameters:
T - Optional result returned by the function produce(Writer) after the data has been written. It can be obtained calling the getResult()
All Implemented Interfaces:
Closeable, Readable

public abstract class ReaderFromWriter<T>
extends Reader

This class allow to read the data written to an Writer from an Reader.

To use this class you must subclass it and implement the abstract method produce(Writer). The data who is produced inside this function can be written to the sink Writer passed as a parameter. Later it can be read back from from the ReaderFromWriter class (whose ancestor is java.io.Reader ).

 final String dataId=//id of some data.
 final ReaderFromWriter<String> rfw
                          = new ReaderFromWriter<String>() {
   @Override
   public String produce(final Writer dataSink) throws Exception {
      //call your application function who produces the data here
      //WARNING: we're in another thread here, so this method shouldn't
      //write any class field or make assumptions on the state of the class.
      return produceMydata(dataId,dataSink)
   }
 };
  try {
    //now you can read from the Reader the data that was written to the
    //dataSink Writer
     char[] read=IOUtils.toCharArray(rfw);
     //Use data here
   } catch (final IOException e) {
     //Handle exception here
   } finally {
   rfw.close();
 }
 //You can get the result of produceMyData after the stream has been closed.
 String resultOfProduction = isos.getResult();
 

This class encapsulates a pipe and a Thread, hiding the complexity of using them. It is possible to select different strategies for allocating the internal thread or even specify the ExecutorService for thread execution.

Since:
1.2.7
Version:
$Id: ReaderFromWriter.java 527 2014-02-24 19:29:50Z gabriele.contini@gmail.com $
Author:
dvd.smnt
See Also:
ExecutionModel

Field Summary
protected  ExecutorService executorService
           
 
Fields inherited from class java.io.Reader
lock
 
Constructor Summary
ReaderFromWriter()
           It creates a ReaderFromWriter with a THREAD_PER_INSTANCE thread strategy.
ReaderFromWriter(boolean joinOnClose, ExecutionModel executionModel)
           It creates a ReaderFromWriter and let the user choose the thread allocation strategy he likes.
ReaderFromWriter(boolean joinOnClose, ExecutorService executor)
           It creates a ReaderFromWriter and let the user specify the ExecutorService that will execute the produce(Writer) method.
ReaderFromWriter(boolean joinOnClose, ExecutorService executor, int pipeBufferSize)
           It creates a ReaderFromWriter and let the user specify the ExecutorService that will execute the produce(Writer) method and the pipe buffer size.
ReaderFromWriter(ExecutionModel executionModel)
           It creates a ReaderFromWriter and let the user choose the thread allocation strategy he likes.
ReaderFromWriter(ExecutorService executor)
           It creates a ReaderFromWriter and let the user specify the ExecutorService that will execute the produce(Writer) method.
 
Method Summary
 void close()
          
static String[] getActiveThreadNames()
          This method can be used for debugging purposes to get a list of the currently active threads.
 T getResult()
           Returns the object that was previously returned by the produce(Writer) method.
protected abstract  T produce(Writer sink)
           This method must be implemented by the user of this class to produce the data that must be read from the external Reader.
 int read()
          
 int read(char[] b, int off, int len)
          
static void setDefaultPipeSize(int defaultPipeSize)
          Set the size for the pipe circular buffer for the newly created ReaderFromWriter.
 long skip(long n)
          
 
Methods inherited from class java.io.Reader
mark, markSupported, read, read, ready, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

executorService

protected final ExecutorService executorService
Constructor Detail

ReaderFromWriter

public ReaderFromWriter()

It creates a ReaderFromWriter with a THREAD_PER_INSTANCE thread strategy.

See Also:
ExecutionModel.THREAD_PER_INSTANCE

ReaderFromWriter

public ReaderFromWriter(boolean joinOnClose,
                        ExecutionModel executionModel)

It creates a ReaderFromWriter and let the user choose the thread allocation strategy he likes.

This class executes the produce method in a thread created internally.

Parameters:
executionModel - Defines how the internal thread is allocated.
joinOnClose - If true the close() method will also wait for the internal thread to finish.
Since:
1.2.7
See Also:
ExecutionModel

ReaderFromWriter

public ReaderFromWriter(boolean joinOnClose,
                        ExecutorService executor)

It creates a ReaderFromWriter and let the user specify the ExecutorService that will execute the produce(Writer) method.

Parameters:
executor - Defines the ExecutorService that will allocate the the internal thread.
joinOnClose - If true the close() method will also wait for the internal thread to finish.
Since:
1.2.7
See Also:
ExecutorService

ReaderFromWriter

public ReaderFromWriter(boolean joinOnClose,
                        ExecutorService executor,
                        int pipeBufferSize)

It creates a ReaderFromWriter and let the user specify the ExecutorService that will execute the produce(Writer) method and the pipe buffer size.

Using this method the default size is ignored.

Parameters:
executor - Defines the ExecutorService that will allocate the the internal thread.
joinOnClose - If true the close() method will also wait for the internal thread to finish.
pipeBufferSize - The size of the pipe buffer to allocate.
Since:
1.2.7
See Also:
ExecutorService

ReaderFromWriter

public ReaderFromWriter(ExecutionModel executionModel)

It creates a ReaderFromWriter and let the user choose the thread allocation strategy he likes.

This class executes the produce method in a thread created internally.

Parameters:
executionModel - Defines how the internal thread is allocated.
See Also:
ExecutionModel

ReaderFromWriter

public ReaderFromWriter(ExecutorService executor)

It creates a ReaderFromWriter and let the user specify the ExecutorService that will execute the produce(Writer) method.

Parameters:
executor - Defines the ExecutorService that will allocate the the internal thread.
See Also:
ExecutorService
Method Detail

getActiveThreadNames

public static final String[] getActiveThreadNames()
This method can be used for debugging purposes to get a list of the currently active threads.

Returns:
Array containing names of the threads currently active.

setDefaultPipeSize

public static void setDefaultPipeSize(int defaultPipeSize)
Set the size for the pipe circular buffer for the newly created ReaderFromWriter. Default is 4096 bytes.

Parameters:
defaultPipeSize - the default pipe buffer size in bytes.
Since:
1.2.7

close

public final void close()
                 throws IOException

Specified by:
close in interface Closeable
Specified by:
close in class Reader
Throws:
IOException

getResult

public T getResult()
            throws Exception

Returns the object that was previously returned by the produce(Writer) method. It performs all the synchronization operations needed to read the result and waits for the internal thread to terminate.

This method must be called after the method close(), otherwise an IllegalStateException is thrown.

Returns:
Object that was returned by the produce(Writer) method.
Throws:
Exception - If the produce(Writer) method threw an java.lang.Exception this method will throw again the same exception.
IllegalStateException - If the close() method hasn't been called yet.
Since:
1.2.7

produce

protected abstract T produce(Writer sink)
                      throws Exception

This method must be implemented by the user of this class to produce the data that must be read from the external Reader.

Special care must be paid passing arguments to this method or setting global fields because it is executed in another thread.

The right way to set a field variable is to return a value in the produceand retrieve it in the getResult().

Parameters:
sink - the implementing class should write its data to this stream.
Returns:
The implementing class can use this to return a result of data production. The result will be available through the method getResult().
Throws:
Exception - the exception eventually thrown by the implementing class is returned by the read() methods.
See Also:
getResult()

read

public final int read()
               throws IOException

Overrides:
read in class Reader
Throws:
IOException

read

public final int read(char[] b,
                      int off,
                      int len)
               throws IOException

Specified by:
read in class Reader
Throws:
IOException

skip

public long skip(long n)
          throws IOException

Overrides:
skip in class Reader
Throws:
IOException


Copyright © 2008–2014. All rights reserved.