mediautil.gen.directio
Class InStreamFromIterativeWriter

java.lang.Object
  extended by java.io.InputStream
      extended by mediautil.gen.directio.InStreamFromIterativeWriter
All Implemented Interfaces:
Closeable

public class InStreamFromIterativeWriter
extends InputStream

This class enables reading off directly from an IterativeWriter. The read/skip calls to this class which is to be used as an InputStream translate into nextWrite() calls on the underlying IterativeWriter. Excess bytes written are buffered for the next read call.

Another possible use of this class is in converting an IterativeWriter processing data from an input into a FilterInputStream.

See Also:
IterativeWriter

Constructor Summary
InStreamFromIterativeWriter()
          Creates an InStreamFromIterativeWriter Object.
InStreamFromIterativeWriter(int bufSize)
          Creates an InStreamFromIterativeWriter Object.
InStreamFromIterativeWriter(int bufSize, int incSize, int minWriteSize, int writeCushion)
          Creates an InStreamFromIterativeWriter Object.
 
Method Summary
 int available()
          Available method of the SplitInputStream.
 int getMaxBufSize()
          Returns the max bufSize.
 OutputStream getWriterOutputStream()
          Gets the OutputStream for use by the IterativeWriter.
 int read()
          Read method of the InStreamFromIterativeWriter.
 int read(byte[] b, int off, int len)
          Read method of the InStreamFromIterativeWriter.
 void setIterativeWriter(IterativeWriter iterativeWriter)
          Sets the IterativeWriter for this InStreamFromIterativeWriter.
 long skip(long n)
          Skip method of the InStreamFromIterativeWriter.
 
Methods inherited from class java.io.InputStream
close, mark, markSupported, read, reset
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

InStreamFromIterativeWriter

public InStreamFromIterativeWriter(int bufSize,
                                   int incSize,
                                   int minWriteSize,
                                   int writeCushion)
Creates an InStreamFromIterativeWriter Object. This Object can be used as an InputStream. However an IterativeWriter must be set before using the stream.

Parameters:
bufSize - Buffer Size
incSize - Bytes by which to increment Buffer Size. Expanding the buffer will be required when the IterativeWriter's nextWrite method writes too many bytes beyond the requested size.
minWriteSize - Minimum Write request size for the IterativeWrite's nextWrite() call
writeCushion - Bytes by which the actual bytes written may exceed the requested number of bytes for the ItertativeWriter's nextWrite() call. If the nextWrite() call overshoots this limit also then a buffer expansion may be required which affects performance.

InStreamFromIterativeWriter

public InStreamFromIterativeWriter(int bufSize)
Creates an InStreamFromIterativeWriter Object. This Object can be used as an InputStream. However an IterativeWriter must be set before using the stream. Uses a buffer increment size of 512 and minWriteSize and writeCushion of 750 bytes.

Parameters:
bufSize - Buffer Size

InStreamFromIterativeWriter

public InStreamFromIterativeWriter()
Creates an InStreamFromIterativeWriter Object. This Object can be used as an InputStream. However an IterativeWriter must be set before using the stream. Uses a a buffer size of 4096, buffer increment size of 512 and minWriteSize and writeCushion of 750 bytes.

Method Detail

getWriterOutputStream

public OutputStream getWriterOutputStream()
Gets the OutputStream for use by the IterativeWriter. The data written to this stream is buffered for use by this class's read/skip method. Closing this stream is equivalent to the nextRead of the iterativeWriter returning IterativeReader.STOP, in which case the InStreamFromIterativeWriter Object shows end of file after its buffer is read.

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

setIterativeWriter

public void setIterativeWriter(IterativeWriter iterativeWriter)
Sets the IterativeWriter for this InStreamFromIterativeWriter. This must be called before reading data from this stream.

Parameters:
iterativeWriter - IterativeWriter to call to write data. The iterativeWriter must write to the OutputStream got by the getWriterOutputStream() call.
See Also:
getWriterOutputStream()

read

public int read(byte[] b,
                int off,
                int len)
         throws IOException
Read method of the InStreamFromIterativeWriter. See the documentation of InputStream.read(..) for more information. This reads data from an internal buffer calling the iterativeWriter's nextWrite method to fill the buffer.

Overrides:
read in class InputStream
Throws:
IOException - If the nextWrite() call of the iterativeWriter throws an IOException
See Also:
IterativeWriter.nextWrite(int)

read

public int read()
         throws IOException
Read method of the InStreamFromIterativeWriter. See the documentation of InputStream.read(..) for more information. This reads data from an internal buffer calling the iterativeWriter's nextWrite method to fill the buffer.

Specified by:
read in class InputStream
Throws:
IOException - If the nextWrite() call of the iterativeWriter throws an IOException
See Also:
IterativeWriter.nextWrite(int)

skip

public long skip(long n)
          throws IOException
Skip method of the InStreamFromIterativeWriter. See the documentation of InputStream.skip(..) for more information. This skips data from an internal buffer calling the iterativeWriter's nextWrite method to fill the buffer.

Overrides:
skip in class InputStream
Throws:
IOException - If the nextWrite() call of the iterativeWriter throws an IOException
See Also:
IterativeWriter.nextWrite(int)

available

public int available()
              throws IOException
Available method of the SplitInputStream. See the documentation of InputStream.available(..) for more information. This method does not strictly conform to the InputStreams spec since it always returns atleast 1 unless the IterativeWriter's OutputStream is closed.

Overrides:
available in class InputStream
Throws:
IOException

getMaxBufSize

public int getMaxBufSize()
Returns the max bufSize. For debugging and informational purposes.

Returns:
Max Buf Size