com.gc.iotools.stream.reader
Class TeeReaderWriter

java.lang.Object
  extended by java.io.Reader
      extended by com.gc.iotools.stream.reader.TeeReaderWriter
All Implemented Interfaces:
Closeable, Readable

public class TeeReaderWriter
extends Reader

Copies the data from the underlying Reader to the Writer(s) passed in the constructor. The data copied are similar to the underlying Reader.

When the method close() is invoked all the bytes remaining in the underlying Reader are copied to the Writer(s). This behavior is different from this class and TeeInputStream in Apache commons-io.

Bytes skipped are in any case copied to the Writer. Mark and reset of the outer Reader doesn't affect the data copied to the Writer(s), that remain similar to the Reader passed in the constructor.

It also calculate some statistics on the read/write operations. getWriteTime() returns the time spent writing to the Writers, getReadTime() returns the time spent reading from the Reader and getWriteSize() returns the amount of data written to a single Writer until now.

Sample usage:

         Reader source=... //some data to be read.
   StringWriter destination1= new StringWriter();
   StringWriter destination2= new StringWriter();

   TeeReaderWriter tee = new TeeReaderWriter(source,destination1);
   org.apache.commons.io.IOUtils.copy(tee,destination2);
   tee.close();
   //at this point both destination1 and destination2 contains the same bytes
   //in destination1 were put by TeeReaderWriter while in
   //destination2 they were copied by IOUtils.
   StringBuffer buffer=destination1.getBuffer();
 

Since:
1.2.7
Version:
$Id: TeeReaderWriter.java 463 2011-01-21 23:54:17Z dvd.smnt@gmail.com $
Author:
dvd.smnt
See Also:
TeeReader

Field Summary
protected  boolean closeStreams
          If true source and destination streams are closed when close() is invoked.
protected  boolean[] copyEnabled
           
protected  Writer[] destinations
          The destination Writers where data is written.
protected  Reader source
          The source Reader where the data comes from.
 
Fields inherited from class java.io.Reader
lock
 
Constructor Summary
TeeReaderWriter(Reader source, boolean closeStreams, Writer... destinations)
           Creates a TeeInputStreamWriter and saves its argument, the input stream source and the output stream destination for later use.
TeeReaderWriter(Reader source, Writer destination)
           Creates a TeeReaderWriter and saves its argument, the input stream source and the Writer destination for later use.
TeeReaderWriter(Reader source, Writer destination, boolean closeStreams)
          Creates a TeeReaderWriter and saves its argument, the input stream source and the output stream destination for later use.
 
Method Summary
 void close()
          
 void enableCopy(boolean enable)
           Allow to switch off the copy to the underlying OutputStreams.
 void enableCopy(boolean[] enable)
           Allow to switch off the copy to the underlying OutputStreams, selectively enabling or disabling copy to some specific stream.
 long getReadTime()
           Returns the number of milliseconds spent reading from the source Reader.
 long getWriteSize()
           Returns the number of bytes written until now to a single destination Writer.
 long[] getWriteTime()
           Return the time spent writing on the destination Writer(s) in milliseconds.
 void mark(int readLimit)
          
 boolean markSupported()
          
 int read()
          
 int read(char[] b, int off, int len)
          
 void reset()
          
 
Methods inherited from class java.io.Reader
read, read, ready, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

closeStreams

protected final boolean closeStreams
If true source and destination streams are closed when close() is invoked.


copyEnabled

protected final boolean[] copyEnabled

destinations

protected final Writer[] destinations
The destination Writers where data is written.


source

protected final Reader source
The source Reader where the data comes from.

Constructor Detail

TeeReaderWriter

public TeeReaderWriter(Reader source,
                       boolean closeStreams,
                       Writer... destinations)

Creates a TeeInputStreamWriter and saves its argument, the input stream source and the output stream destination for later use.

This constructor allow to specify multiple Writer to which the data will be copied.

Parameters:
source - The underlying Reader
closeStreams - if true the destination will be closed when the close() method is invoked. If false the close method on the underlying streams will not be called (it must be invoked externally).
destinations - Data read from source are also written to this Writer.
Since:
1.2.7

TeeReaderWriter

public TeeReaderWriter(Reader source,
                       Writer destination)

Creates a TeeReaderWriter and saves its argument, the input stream source and the Writer destination for later use.

When the method close() it is invoked the remaining content of the source stream is copied to the destination and the source and destination streams are closed.

Parameters:
source - The underlying Reader
destination - Data read from source are also written to this Writer.

TeeReaderWriter

public TeeReaderWriter(Reader source,
                       Writer destination,
                       boolean closeStreams)
Creates a TeeReaderWriter and saves its argument, the input stream source and the output stream destination for later use.

Parameters:
source - The underlying Reader
destination - Data read from source are also written to this Writer.
closeStreams - if true the destination will be closed when the close() method is invoked. If false the close method on the underlying streams will not be called (it must be invoked externally).
Since:
1.2.7
Method Detail

close

public void close()
           throws IOException

This method is called when the method close() is invoked. It copies all the data eventually remaining in the source Reader passed in the constructor to the destination Writer.

The standard behavior is to close both the underlying Reader and Writer(s). When the class was constructed with the parameter closeStreams set to false the underlying streams must be closed externally.

Specified by:
close in interface Closeable
Specified by:
close in class Reader
Throws:
IOException
See Also:
close()

enableCopy

public final void enableCopy(boolean enable)

Allow to switch off the copy to the underlying OutputStreams. Setting the parameter to false will disable the copy to all the underlying streams at once.

If you need more fine grained control you should use enableCopy(boolean[]) .

Parameters:
enable - whether to copy or not the bytes to the underlying stream.
Since:
1.2.9

enableCopy

public final void enableCopy(boolean[] enable)

Allow to switch off the copy to the underlying OutputStreams, selectively enabling or disabling copy to some specific stream.

The copy is enabled by default. Each element in the array correspond to an OutputStream passed in the constructor. If the correspondent element in the array passed as a parameter is set to true the copy will be enabled. It can be invoked multiple times.

Parameters:
enable - whether to copy or not the bytes to the underlying OutputStreams.
Since:
1.2.9

getReadTime

public long getReadTime()

Returns the number of milliseconds spent reading from the source Reader.

Returns:
number of milliseconds spent reading from the source .
Since:
1.2.7

getWriteSize

public long getWriteSize()

Returns the number of bytes written until now to a single destination Writer.

This number is not affected by any of the mark and reset that are made on this TeeReaderWriter and reflects only the number of bytes written.

Returns:
number of bytes written until now to a single destination.
Since:
1.2.7

getWriteTime

public long[] getWriteTime()

Return the time spent writing on the destination Writer(s) in milliseconds.

The returned array has one element for each Writer passed in the constructor.

Returns:
time spent writing on the destination Writers.

mark

public void mark(int readLimit)
          throws IOException

Marks the current position in this input stream. A subsequent call to the reset method repositions this stream at the last marked position so that subsequent reads re-read the same bytes.

Overrides:
mark in class Reader
Throws:
IOException
Since:
1.2.7
See Also:
reset(), Reader.mark(int)

markSupported

public boolean markSupported()

Overrides:
markSupported in class Reader

read

public int read()
         throws IOException

Overrides:
read in class Reader
Throws:
IOException

read

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

Specified by:
read in class Reader
Throws:
IOException

reset

public void reset()
           throws IOException

Repositions this stream to the position at the time the mark method was last called on this input stream.

After reset() method is called the data is not copied anymore to the destination Writer until the position where reset was issued is reached again. This ensures the data copied to the destination Writer reflects the data contained in the source Reader (the one passed in the constructor).

Overrides:
reset in class Reader
Throws:
IOException - If the source stream has an exception in calling reset().
Since:
1.2.7
See Also:
mark(int), Reader.reset()


Copyright © 2008-2011. All Rights Reserved.