org.jnetpcap.nio
Class JBufferInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by org.jnetpcap.nio.JBufferInputStream
All Implemented Interfaces:
java.io.Closeable

public class JBufferInputStream
extends java.io.InputStream

IO InputStream class that reads data out of a JBuffer. This implementation supports all methods efficiently, including bulk transfers and the optional mark operation.

Author:
Mark Bednarczyk, Sly Technologies, Inc.

Constructor Summary
JBufferInputStream(JBuffer in)
          Creates a new input stream initialized to read data out of the supplied buffer.
JBufferInputStream(JBuffer in, int offset, int length)
          Creates a new input stream initialized to read data out fo the supplied buffer.
 
Method Summary
 int available()
          Number of bytes available for reading out of the buffer.
 void close()
          Closes this input stream.
 void mark(int readlimit)
          Sets a mark within the buffer where subsequent reset operation will revert back the position to.
 boolean markSupported()
          Checks if mark method is supported on this stream.
 int read()
          Reads 1 byte out of the underlying source buffer.
 int read(byte[] b)
          Reads a block of data out of the source buffer.
 int read(byte[] b, int off, int len)
          Reads a block of data out of the source buffer.
 void reset()
          Resets the stream back to position where it was previously marked.
 long skip(long n)
          Advances the position with the source buffer effectively skipping over specified number of bytes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JBufferInputStream

public JBufferInputStream(JBuffer in)
Creates a new input stream initialized to read data out of the supplied buffer.

Parameters:
in - source buffer to read data out of

JBufferInputStream

public JBufferInputStream(JBuffer in,
                          int offset,
                          int length)
Creates a new input stream initialized to read data out fo the supplied buffer.

Parameters:
in - source buffer to read data out of
offset - offset into the source buffer where to start reading
length - number of byte to read out of the buffer before signalining end of stream
Method Detail

read

public int read()
         throws java.io.IOException
Reads 1 byte out of the underlying source buffer.

Specified by:
read in class java.io.InputStream
Returns:
byte value read out of the buffer
Throws:
java.io.IOException - Signals that an I/O exception has occurred.
See Also:
InputStream.read()

available

public int available()
              throws java.io.IOException
Number of bytes available for reading out of the buffer.

Overrides:
available in class java.io.InputStream
Returns:
number of byte available in the buffer which is usually the entire buffer
Throws:
java.io.IOException - Signals that an I/O exception has occurred.

close

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

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.InputStream
Throws:
java.io.IOException - Signals that an I/O exception has occurred.
See Also:
InputStream.close()

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Reads a block of data out of the source buffer.

Overrides:
read in class java.io.InputStream
Parameters:
b - buffer to store the block of data read
off - offset into the destination buffer where to store the block data
len - number of bytes to read as a block
Returns:
the int
Throws:
java.io.IOException - Signals that an I/O exception has occurred.

read

public int read(byte[] b)
         throws java.io.IOException
Reads a block of data out of the source buffer.

Overrides:
read in class java.io.InputStream
Parameters:
b - Buffer to store the block of data read. The length of read operation is the size of the byte array.
Returns:
the int
Throws:
java.io.IOException - Signals that an I/O exception has occurred.

skip

public long skip(long n)
          throws java.io.IOException
Advances the position with the source buffer effectively skipping over specified number of bytes.

Overrides:
skip in class java.io.InputStream
Parameters:
n - of bytes to skip over
Returns:
actual number of bytes skipped over. Can be less then requested if remaining number of bytes still to be read out of the source buffer was less then requested
Throws:
java.io.IOException - Signals that an I/O exception has occurred.

mark

public void mark(int readlimit)
Sets a mark within the buffer where subsequent reset operation will revert back the position to.

Overrides:
mark in class java.io.InputStream
Parameters:
readlimit - Maximum number of bytes expected to be read. This parameter is ignored for source buffer operations since data does not need to be buffered.

markSupported

public boolean markSupported()
Checks if mark method is supported on this stream.

Overrides:
markSupported in class java.io.InputStream
Returns:
this method always returns true for this object type

reset

public void reset()
           throws java.io.IOException
Resets the stream back to position where it was previously marked.

Overrides:
reset in class java.io.InputStream
Throws:
java.io.IOException - Signals that an I/O exception has occurred.