net.wimpi.modbus.io
Class FastByteArrayInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by net.wimpi.modbus.io.FastByteArrayInputStream
All Implemented Interfaces:
Closeable
Direct Known Subclasses:
BytesInputStream

public class FastByteArrayInputStream
extends InputStream

This class is a clean room implementation of the ByteArrayInputStream, with enhancements for speed (no synchronization for example).

The idea for such an implementation was originally obtained from Berkeley DB JE, however, this represents a clean-room implementation that is NOT derived from their implementation for license reasons and differs in implementation considerably. For compatibility reasons we have tried to conserve the interface as much as possible.

Version:
@version@ (@date@)
Author:
Dieter Wimberger (wimpi)

Constructor Summary
FastByteArrayInputStream(byte[] buffer)
          Creates a new FastByteArrayInputStream instance that allows to read from the given byte array.
FastByteArrayInputStream(byte[] buffer, int offset, int length)
          Creates a new FastByteArrayInputStream instance that allows to read from the given byte array.
 
Method Summary
 int available()
          Returns the number of bytes that can be read (or skipped over) from this FastByteArrayInputStream.
 void close()
          The close method for this FastByteArrayInputStream does nothing.
 byte[] getBuffer()
          Returns the underlying data being read.
 int getPosition()
          Returns the offset at which data is being read from the buffer.
 void mark(int limit)
          Marks the current position in this FastByteArrayInputStream.
 boolean markSupported()
          Tests if this FastByteArrayInputStream supports the mark and reset methods.
 int read()
          Reads the next byte of data from this input stream.
 int read(byte[] toBuf)
           
 int read(byte[] toBuf, int offset, int length)
          Reads up to len bytes of data into an array of bytes from this input stream.
 void reset()
          Re-positions this stream to the position at the time the mark method was last called this FastByteArrayInputStream.
 int size()
          Returns the size of the buffer being read.
 long skip(long n)
          Skips over and discards n bytes of data from this input stream.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FastByteArrayInputStream

public FastByteArrayInputStream(byte[] buffer)
Creates a new FastByteArrayInputStream instance that allows to read from the given byte array.

Parameters:
buffer - the data to be read.

FastByteArrayInputStream

public FastByteArrayInputStream(byte[] buffer,
                                int offset,
                                int length)
Creates a new FastByteArrayInputStream instance that allows to read from the given byte array.

Parameters:
buffer - the data to read.
offset - the byte offset at which to begin reading.
length - the number of bytes to read.
Method Detail

read

public int read()
         throws IOException
Reads the next byte of data from this input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned.

This read method cannot block.

Specified by:
read in class InputStream
Returns:
the next byte of data, or -1 if the end of the stream has been reached.
Throws:
IOException

read

public int read(byte[] toBuf,
                int offset,
                int length)
         throws IOException
Reads up to len bytes of data into an array of bytes from this input stream. If pos equals count, then -1 is returned to indicate end of file. Otherwise, the number k of bytes read is equal to the smaller of len and count-pos. If k is positive, then bytes buf[pos] through buf[pos+k-1] are copied into b[off] through b[off+k-1] in the manner performed by System.arraycopy. The value k is added into pos and k is returned.

Overrides:
read in class InputStream
Parameters:
toBuf - the buffer into which the data is read.
offset - the start offset of the data.
length - the max number of bytes read.
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
Throws:
IOException

read

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

skip

public long skip(long n)
Skips over and discards n bytes of data from this input stream. The skip method may skip over some smaller number of bytes. The actual number of bytes skipped is returned, or a number <=0 if none was skipped.

The maximum number of bytes that can be skipped is defined by Integer.MAX_VALUE.

Overrides:
skip in class InputStream
Parameters:
n - the number of bytes to be skipped.
Returns:
the actual number of bytes skipped.

close

public void close()
The close method for this FastByteArrayInputStream does nothing.

Specified by:
close in interface Closeable
Overrides:
close in class InputStream

available

public int available()
Returns the number of bytes that can be read (or skipped over) from this FastByteArrayInputStream.

Overrides:
available in class InputStream
Returns:
the number of bytes that can be skipped.

mark

public void mark(int limit)
Marks the current position in this FastByteArrayInputStream. A subsequent call toreset() will re-postition this FastByteArrayInputStream at the last marked position so that subsequent reads re-read the same bytes.

Overrides:
mark in class InputStream
Parameters:
limit - a read limit that invalidates the mark if passed.

markSupported

public boolean markSupported()
Tests if this FastByteArrayInputStream supports the mark and reset methods.

Overrides:
markSupported in class InputStream
Returns:
true if supported, false otherwise.

reset

public void reset()
           throws IOException
Re-positions this stream to the position at the time the mark method was last called this FastByteArrayInputStream.

Overrides:
reset in class InputStream
Throws:
IOException - if the readlimit was exceeded.

getBuffer

public byte[] getBuffer()
Returns the underlying data being read.

Returns:
the underlying data.

getPosition

public int getPosition()
Returns the offset at which data is being read from the buffer.

Returns:
the offset at which data is being read.

size

public int size()
Returns the size of the buffer being read.

Returns:
the size of the buffer.


Copyright © 2010. All Rights Reserved.