com.healthmarketscience.rmiio.util
Class EncodingInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by com.healthmarketscience.rmiio.PacketInputStream
          extended by com.healthmarketscience.rmiio.util.EncodingInputStream
All Implemented Interfaces:
Closeable

public abstract class EncodingInputStream
extends PacketInputStream

InputStream which facilitates generating a stream of data on demand through some programmatic action. Subclasses must implement the encode(int) method, which should write some reasonable amount of data to the OutputStream returned from createOutputStream() (which in turn will forward to data to the consumer of the InputStream). The OutputStream linked to this class must only be written during a call to encode.

Additionally, this class provides a "packet" based read method, which returns the underlying byte[]'s (which will be approximately the size of the chunk size configured for this class), which may be more efficient in some applications.

Note, this class has no synchronization except that the close method supports asynchronous closing.

Author:
James Ahlborn

Field Summary
static int DEFAULT_CHUNK_SIZE
          initial size of the overflow buffer used when moving data from the internal _localOStream to the _localIStream.
 
Fields inherited from class com.healthmarketscience.rmiio.PacketInputStream
DEFAULT_PACKET_SIZE, EMPTY_PACKET
 
Constructor Summary
protected EncodingInputStream()
           
protected EncodingInputStream(int chunkSize)
           
protected EncodingInputStream(int chunkSize, boolean noDelay)
           
 
Method Summary
 int available()
           
 void close()
           
protected  PacketOutputStream createOutputStream()
          Creates an OutputStream linked to this InputStream which can be used to write data as requested.
protected abstract  void encode(int suggestedLength)
          Writes some amount of data to the OutputStream linked to the EncodingInputStream calling this method.
protected  long encodeSkip(long len)
          Skips some amount of bytes in the encoding output.
 int packetsAvailable()
          Returns the number of full packets which can be read without blocking.
 int read()
           
 int read(byte[] b)
           
 int read(byte[] buf, int pos, int len)
           
 byte[] readPacket(boolean readPartial)
          Gets the next "packet" from the internal buffer and returns it (if any).
 long skip(long len)
           
protected  void throwIfClosed()
          Throws an IOException if the stream is closed, otherwise, does nothing.
 
Methods inherited from class com.healthmarketscience.rmiio.PacketInputStream
getNoDelay, getPacketSize, readPacket, readPacket
 
Methods inherited from class java.io.InputStream
mark, markSupported, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_CHUNK_SIZE

public static final int DEFAULT_CHUNK_SIZE
initial size of the overflow buffer used when moving data from the internal _localOStream to the _localIStream.

See Also:
Constant Field Values
Constructor Detail

EncodingInputStream

protected EncodingInputStream()

EncodingInputStream

protected EncodingInputStream(int chunkSize)

EncodingInputStream

protected EncodingInputStream(int chunkSize,
                              boolean noDelay)
Method Detail

createOutputStream

protected PacketOutputStream createOutputStream()
Creates an OutputStream linked to this InputStream which can be used to write data as requested. This method creates a new instance, and therefore should only be called once and the result should be cached by the caller.


close

public void close()
           throws IOException
Specified by:
close in interface Closeable
Overrides:
close in class InputStream
Throws:
IOException

throwIfClosed

protected void throwIfClosed()
                      throws IOException
Throws an IOException if the stream is closed, otherwise, does nothing.

Throws:
IOException

available

public int available()
              throws IOException
Overrides:
available in class InputStream
Throws:
IOException

read

public int read()
         throws IOException
Specified by:
read in class InputStream
Throws:
IOException

read

public int read(byte[] b)
         throws IOException
Overrides:
read in class InputStream
Throws:
IOException

read

public int read(byte[] buf,
                int pos,
                int len)
         throws IOException
Overrides:
read in class InputStream
Throws:
IOException

skip

public long skip(long len)
          throws IOException
Overrides:
skip in class InputStream
Throws:
IOException

readPacket

public byte[] readPacket(boolean readPartial)
                  throws IOException
Description copied from class: PacketInputStream
Gets the next "packet" from the internal buffer and returns it (if any). This method may block until a full packet is read, depending on the value of readPartial.

Specified by:
readPacket in class PacketInputStream
Parameters:
readPartial - iff false, may block until a full packet is read (or EOF), otherwise will return as much data as is currently available (which may be 0).
Returns:
a fully filled array of byte's or null if the end of stream has been reached. if no data is available but EOF has not been reached, the returned buffer will have length 0.
Throws:
IOException

packetsAvailable

public int packetsAvailable()
                     throws IOException
Description copied from class: PacketInputStream
Returns the number of full packets which can be read without blocking.

Specified by:
packetsAvailable in class PacketInputStream
Throws:
IOException

encodeSkip

protected long encodeSkip(long len)
                   throws IOException
Skips some amount of bytes in the encoding output. The default implementation just reads bytes via the normal encode process and discards them. Subclasses may override this method to provide a more efficient skip implementation.

Returns:
the actual number of bytes skipped by this call
Throws:
IOException

encode

protected abstract void encode(int suggestedLength)
                        throws IOException
Writes some amount of data to the OutputStream linked to the EncodingInputStream calling this method. The implementation may technically write as much data as desired during the call. However, since any data written over the given suggestedLength will be buffered locally, the implementation risks running the local vm out of memory if too much is written. If too little is written, the EncodingInputStream will simply repeat the call with adjusted parameters. Implementation should call close on the OutputStream when finished encoding.

Parameters:
suggestedLength - target amount of bytes to write
Throws:
IOException


Copyright © 2006–2016 Health Market Science. All rights reserved.