com.healthmarketscience.rmiio
Class PacketInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by com.healthmarketscience.rmiio.PacketInputStream
All Implemented Interfaces:
Closeable
Direct Known Subclasses:
EncodingInputStream, PipeBuffer.InputStreamAdapter

public abstract class PacketInputStream
extends InputStream

Adds support for packet based access to data from an InputStream. Can be more efficient for certain stream implementations, especially for remote stream usage where data is coming across the wire in byte[] packets.

Author:
James Ahlborn

Field Summary
static int DEFAULT_PACKET_SIZE
          target size of packets returned from packet related methods
protected static byte[] EMPTY_PACKET
          empty packet.
 
Constructor Summary
PacketInputStream()
           
PacketInputStream(int packetSize)
           
PacketInputStream(int packetSize, boolean noDelay)
           
 
Method Summary
 boolean getNoDelay()
           
 int getPacketSize()
           
abstract  int packetsAvailable()
          Returns the number of full packets which can be read without blocking.
 byte[] readPacket()
          Gets the next "packet" from the internal buffer and returns it (if any).
abstract  byte[] readPacket(boolean readPartial)
          Gets the next "packet" from the internal buffer and returns it (if any).
static byte[] readPacket(InputStream in, byte[] packet)
          Reads a packet of data from the given input stream.
 
Methods inherited from class java.io.InputStream
available, close, mark, markSupported, read, read, read, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_PACKET_SIZE

public static final int DEFAULT_PACKET_SIZE
target size of packets returned from packet related methods

See Also:
Constant Field Values

EMPTY_PACKET

protected static final byte[] EMPTY_PACKET
empty packet. useful for returns from partial reads where no data is currently available, but it's not EOF.

Constructor Detail

PacketInputStream

public PacketInputStream()

PacketInputStream

public PacketInputStream(int packetSize)

PacketInputStream

public PacketInputStream(int packetSize,
                         boolean noDelay)
Method Detail

getPacketSize

public int getPacketSize()

getNoDelay

public boolean getNoDelay()

readPacket

public byte[] readPacket()
                  throws IOException
Gets the next "packet" from the internal buffer and returns it (if any). By default, this method will block until a fully filled packet is created (equivalent to calling readPacket(false)). If noDelay is enabled, this method will allow partial packet reads (equivalent to calling readPacket(true)).

Returns:
a fully filled array of byte's or null if the end of stream has been reached
Throws:
IOException

readPacket

public abstract byte[] readPacket(boolean readPartial)
                           throws IOException
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.

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 abstract int packetsAvailable()
                              throws IOException
Returns the number of full packets which can be read without blocking.

Throws:
IOException

readPacket

public static byte[] readPacket(InputStream in,
                                byte[] packet)
                         throws IOException
Reads a packet of data from the given input stream. The given packet is filled and returned if possible. If not enough bytes are available, a new packet will be created and returned. If the stream is empty, null will be returned.

Parameters:
in - the InputStream from which to read data
packet - the potential output packet (if enough data is available)
Returns:
a filled packet of data if any available, null if the stream is empty
Throws:
IOException


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