seda.sandStorm.lib.util
Class MultiByteArrayInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by seda.nbio.NonblockingInputStream
          extended by seda.sandStorm.lib.util.MultiByteArrayInputStream
All Implemented Interfaces:
java.io.Closeable
Direct Known Subclasses:
aSocketInputStream

public class MultiByteArrayInputStream
extends seda.nbio.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.


Field Summary
private  byte[][] arrays
           
private  boolean closed
           
private  int cur_array
           
private  int cur_offset
           
private static boolean DEBUG
           
private  int mark_array
           
private  int mark_offset
           
private static int NUMARRAYS
           
private  int push_array
           
 
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.
private  void expandArrays()
           
 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
 

Field Detail

DEBUG

private static final boolean DEBUG
See Also:
Constant Field Values

NUMARRAYS

private static final int NUMARRAYS
See Also:
Constant Field Values

closed

private boolean closed

cur_offset

private int cur_offset

cur_array

private int cur_array

arrays

private byte[][] arrays

push_array

private int push_array

mark_array

private int mark_array

mark_offset

private int mark_offset
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.


expandArrays

private void expandArrays()

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 seda.nbio.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 seda.nbio.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 seda.nbio.NonblockingInputStream
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 seda.nbio.NonblockingInputStream
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 seda.nbio.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 seda.nbio.NonblockingInputStream
Throws:
java.io.IOException

close

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

Specified by:
close in interface java.io.Closeable
Specified by:
close in class seda.nbio.NonblockingInputStream
Throws:
java.io.IOException

markSupported

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

Overrides:
markSupported in class java.io.InputStream

reset

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

Overrides:
reset in class java.io.InputStream
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.

Overrides:
mark in class java.io.InputStream

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.