mediautil.gen.directio
Class OutStreamToIterativeReader

java.lang.Object
  extended by java.io.OutputStream
      extended by mediautil.gen.directio.OutStreamToIterativeReader
All Implemented Interfaces:
Closeable, Flushable

public class OutStreamToIterativeReader
extends OutputStream

This class enables writing directly to an IterativeReader which will read the data written as an InputStream. The write calls to this class which is to be used as an OutputStream translate into nextRead() calls on the underlying IterativeReader after buffering. The only catch is that the IterativeReader in this case should not exceed the read request beyond the readCushion as this will result in an empty buffer which will be flagged as an IOException on the IterativeReader's InputStream.

See Also:
IterativeReader

Constructor Summary
OutStreamToIterativeReader()
          Creates an OutStreamToIterativeReader Object.
OutStreamToIterativeReader(int bufSize, int minReadSize, int readCushion)
          Creates an OutStreamToIterativeReader Object.
 
Method Summary
 void close()
          Closes the OutStreamToIterativeReader.
 InputStream getReaderInputStream()
          Gets the InputStream for use by the IterativeReader.
 boolean isReaderDetached()
          Returns if the IterativeReader attached to this OutStreamToIterativeReader has detached.
 void setIterativeReader(IterativeReader iterativeReader)
          Sets the IterativeReader for this OutStreamToIterativeReader.
 void write(byte[] b, int off, int len)
          Write method of the OutStreamToIterativeReader.
 void write(int b)
          Write method of the OutStreamToIterativeReader.
 
Methods inherited from class java.io.OutputStream
flush, write
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

OutStreamToIterativeReader

public OutStreamToIterativeReader(int bufSize,
                                  int minReadSize,
                                  int readCushion)
Creates an OutStreamToIterativeReader Object. This Object can be used as an OutputStream. However an IterativeReader must be set before using the stream.

Parameters:
bufSize - Buffer Size
minReadSize - Minimum Read request size for the IterativeRead's nextRead() call
readCushion - Bytes by which the actual bytes read may exceed the requested number of bytes for the ItertativeReader's nextRead() call. If the nextRead() call overshoots this limit also then an empty buffer results which is flagged as an IOException for the reader's InputStream.

OutStreamToIterativeReader

public OutStreamToIterativeReader()
Creates an OutStreamToIterativeReader Object. This Object can be used as an OutputStream. However an IterativeReader must be set before using the stream. Uses a a buffer size of 3072, and a minReadSize and readCushion of 1024 bytes.

See Also:
OutStreamToIterativeReader(int,int,int)
Method Detail

getReaderInputStream

public InputStream getReaderInputStream()
Gets the InputStream for use by the IterativeReader. This class's write method fills the buffer for use by this InputStream's read. Closing this stream is equivalent to the nextRead of the iterativeReader returning IterativeReader.STOP, in which case IterativeReader is detached and subsequent writes to the OutStreamToIterativeReader Object are ignored.

Returns:
Stream for use by IterativeReader. Note that the returned Object implements ByteCounter and can be cast to a ByteCounter for the IterativeReader to keep track of the number of bytes read.
See Also:
setIterativeReader(IterativeReader)

setIterativeReader

public void setIterativeReader(IterativeReader iterativeReader)
Sets the IterativeReader for this OutStreamToIterativeReader. This must be called before writing data to this stream.

Parameters:
iterativeReader - IterativeReader to call to read data. The iterativeReader must write to the InputStream got by the getReaderInputStream() call.
See Also:
getReaderInputStream()

write

public void write(byte[] b,
                  int off,
                  int len)
           throws IOException
Write method of the OutStreamToIterativeReader. See the documentation of OutputStream.write(..) for more information. This writes data to fill the internal buffer and the calls the iterativeReader's nextRead metho to read the buffered data.

Overrides:
write in class OutputStream
Throws:
IOException - If the nextRead() call of the iterativeReader throws an IOException
See Also:
IterativeReader.nextRead(int)

write

public void write(int b)
           throws IOException
Write method of the OutStreamToIterativeReader. See the documentation of OutputStream.write(..) for more information. This writes data to fill the internal buffer and the calls the iterativeReader's nextRead method to read the buffered data.

Specified by:
write in class OutputStream
Throws:
IOException - If the nextRead() call of the iterativeReader throws an IOException
See Also:
IterativeReader.nextRead(int)

close

public void close()
           throws IOException
Closes the OutStreamToIterativeReader. See the documentation of OutputStream.write(..) for more information. This flags the end of the internal buffer data as end of file. Then it calls the iterativeReader's nextRead method till it returns IterativeReader.STOP.

Specified by:
close in interface Closeable
Overrides:
close in class OutputStream
Throws:
IOException - If the nextRead() call of the iterativeReader throws an IOException
See Also:
IterativeReader.nextRead(int)

isReaderDetached

public boolean isReaderDetached()
Returns if the IterativeReader attached to this OutStreamToIterativeReader has detached.

Returns:
True if the reader has detached, false otherwise.