seda.sandStorm.lib.util
Class MultiByteArrayInputStream

java.lang.Object
  extended byjava.io.InputStream
      extended byseda.nbio.NonblockingInputStream
          extended byseda.sandStorm.lib.util.MultiByteArrayInputStream
Direct Known Subclasses:
aSocketInputStream

public class MultiByteArrayInputStream
extends NonblockingInputStream

This class allows you to treat a list of byte arrays as a single NonblockingInputStream. This is helpful for parsing data contained within network packets, where the payload for one ADU might be spread across multiple packets. This is a *nonblocking* interface; if you attempt to read data from it, and none is available, it will return immediately.

Author:
Matt Welsh

Constructor Summary
MultiByteArrayInputStream()
          Create an empty MultiByteArrayInputStream.
MultiByteArrayInputStream(byte[][] barr)
          Create a MultiByteArrayInputStream with the given array of byte arrays.
 
Method Summary
 void addArray(byte[] barr)
          Add an array to this MultiByteArrayInputStream.
 int available()
          Return the number of bytes available for reading.
 void clear()
          Reset this input stream - clear all internal data and pointers to a fresh initialized state.
 void close()
          Close this stream.
 void mark(int readlimit)
          Set the stream's mark to the current position.
 boolean markSupported()
          Returns true, since mark() and reset() are supported.
 int nbRead()
          Read the next byte from this stream.
 int numArrays()
          Return the number of bytes registered.
 int read()
          Read the next byte from this stream.
 int read(byte[] b)
          Read data from this input stream into the given byte array starting at offset 0 for b.length bytes.
 int read(byte[] b, int off, int len)
          Read data from this input stream into the given byte array starting at offset 'off' for 'len' bytes.
 void reset()
          Returns the stream to the position of the previous mark().
 long skip(long n)
          Skip n bytes in this stream; returns the number of bytes actually skipped (which may be less than the number requested).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MultiByteArrayInputStream

public MultiByteArrayInputStream(byte[][] barr)
Create a MultiByteArrayInputStream with the given array of byte arrays.


MultiByteArrayInputStream

public MultiByteArrayInputStream()
Create an empty MultiByteArrayInputStream.

Method Detail

addArray

public void addArray(byte[] barr)
Add an array to this MultiByteArrayInputStream.


read

public int read()
         throws java.io.IOException
Read the next byte from this stream. Returns -1 if no data is available.

Specified by:
read in class NonblockingInputStream
Throws:
java.io.IOException

nbRead

public int nbRead()
           throws java.io.IOException
Read the next byte from this stream. Returns -1 if no data is available.

Specified by:
nbRead in class NonblockingInputStream
Throws:
java.io.IOException

read

public int read(byte[] b)
         throws java.io.IOException
Read data from this input stream into the given byte array starting at offset 0 for b.length bytes. Returns the actual number of bytes read; returns -1 if no data is available.

Specified by:
read in class NonblockingInputStream
Returns:
The total number of bytes read into the buffer, 0 if no data was available, or -1 if the end of the stream has been reached.
Throws:
java.io.IOException

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Read data from this input stream into the given byte array starting at offset 'off' for 'len' bytes. Returns the actual number of bytes read; returns -1 if no data is available.

Specified by:
read in class NonblockingInputStream
Returns:
The total number of bytes read into the buffer, 0 if no data was available, or -1 if the end of the stream has been reached.
Throws:
java.io.IOException

skip

public long skip(long n)
          throws java.io.IOException
Skip n bytes in this stream; returns the number of bytes actually skipped (which may be less than the number requested).

Specified by:
skip in class NonblockingInputStream
Throws:
java.io.IOException

available

public int available()
              throws java.io.IOException
Return the number of bytes available for reading.

Specified by:
available in class NonblockingInputStream
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Close this stream.

Specified by:
close in class NonblockingInputStream
Throws:
java.io.IOException

markSupported

public boolean markSupported()
Returns true, since mark() and reset() are supported.


reset

public void reset()
           throws java.io.IOException
Returns the stream to the position of the previous mark().

Throws:
java.io.IOException

mark

public void mark(int readlimit)
Set the stream's mark to the current position. 'readlimit' is ignored, since there is no limit to how many bytes can be read before the mark is invalidated.


numArrays

public int numArrays()
Return the number of bytes registered.


clear

public void clear()
Reset this input stream - clear all internal data and pointers to a fresh initialized state.