messif.objects.nio
Class BufferInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by messif.objects.nio.BufferInputStream
All Implemented Interfaces:
java.io.Closeable, BinaryInput
Direct Known Subclasses:
ChannelInputStream, MappedFileChannelInputStream

public class BufferInputStream
extends java.io.InputStream
implements BinaryInput

Input stream that implements the BinaryInput using an internal buffer.

If multiple threads use the same instance of this class, the access to the instance must be synchronized.

See Also:
BufferOutputStream

Constructor Summary
BufferInputStream(byte[] array)
          Creates a new instance of BufferInputStream.
BufferInputStream(java.nio.ByteBuffer buffer)
          Creates a new instance of BufferInputStream.
BufferInputStream(int bufferSize, boolean bufferDirect)
          Creates a new instance of BufferInputStream.
 
Method Summary
 int available()
           
 int bufferedSize()
          Returns the number of bytes currently in the buffer.
 long getPosition()
          Returns the current position in this input stream.
 int read()
          Reads the next byte of data from the input stream.
 int read(byte[] buf, int off, int len)
          Reads up to len bytes of data from the input stream into an array of bytes.
protected  void read(java.nio.ByteBuffer buffer)
          Reads some data into the buffer.
 int read(java.nio.channels.FileChannel channel, long position)
          Reads data from the specified file channel.
 int read(java.nio.channels.ReadableByteChannel channel)
          Reads data from the specified channel.
 java.nio.ByteBuffer readInput(int minBytes)
          Returns a buffer that contains at least minBytes bytes.
 void setPosition(long position)
          Sets the current position in this input stream.
 long skip(long n)
          Skips over and discards n bytes of data from this input.
 
Methods inherited from class java.io.InputStream
close, mark, markSupported, read, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BufferInputStream

public BufferInputStream(int bufferSize,
                         boolean bufferDirect)
                  throws java.lang.IllegalArgumentException
Creates a new instance of BufferInputStream. The input operates on a newly created buffer with the specified size.

Parameters:
bufferSize - the size of the internal buffer
bufferDirect - allocate the internal buffer as direct
Throws:
java.lang.IllegalArgumentException - if there specified buffer size is not valid

BufferInputStream

public BufferInputStream(java.nio.ByteBuffer buffer)
Creates a new instance of BufferInputStream. The input operates on the given buffer.

Parameters:
buffer - the internal buffer for this stream

BufferInputStream

public BufferInputStream(byte[] array)
Creates a new instance of BufferInputStream. The input operates on a buffer that wraps the given data array.

Parameters:
array - the data array for this stream
Method Detail

getPosition

public long getPosition()
Returns the current position in this input stream.

Returns:
the current position in this input stream

setPosition

public void setPosition(long position)
                 throws java.io.IOException
Sets the current position in this input stream.

Parameters:
position - the new position in this input stream
Throws:
java.io.IOException - if the position is invalid or there was another I/O error

bufferedSize

public int bufferedSize()
Returns the number of bytes currently in the buffer.

Returns:
the number of bytes currently in the buffer

read

public int read()
         throws java.io.IOException
Reads the next byte of data from the 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 method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

Specified by:
read in class java.io.InputStream
Returns:
the next byte of data, or -1 if the end of the stream is reached
Throws:
java.io.IOException - if there was an error using readChannel

read

public int read(byte[] buf,
                int off,
                int len)
         throws java.io.IOException,
                java.lang.IndexOutOfBoundsException
Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read. The number of bytes actually read is returned as an integer.

Overrides:
read in class java.io.InputStream
Parameters:
buf - the buffer into which the data is read
off - the start offset in array buf at which the data is written
len - the maximum number of bytes to 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:
java.io.IOException - if there was an error reading from the input stream
java.lang.IndexOutOfBoundsException - if the buf.length - off < len

skip

public long skip(long n)
          throws java.io.IOException
Description copied from interface: BinaryInput
Skips over and discards n bytes of data from this input. The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0. This may result from any of a number of conditions; reaching end of file before n bytes have been skipped is only one possibility. The actual number of bytes skipped is returned.

Specified by:
skip in interface BinaryInput
Overrides:
skip in class java.io.InputStream
Parameters:
n - the number of bytes to be skipped
Returns:
the actual number of bytes skipped
Throws:
java.io.IOException - if the input does not support seek or some other I/O error occurs

available

public int available()
Overrides:
available in class java.io.InputStream

readInput

public java.nio.ByteBuffer readInput(int minBytes)
                              throws java.io.IOException
Description copied from interface: BinaryInput
Returns a buffer that contains at least minBytes bytes. If the buffer with the required number of bytes cannot be provided, an IOException is thrown. Note that the returned buffer can provide more than minBytes.

Specified by:
readInput in interface BinaryInput
Parameters:
minBytes - the minimal number of bytes that must be available for reading from the buffer
Returns:
the buffer prepared for reading
Throws:
java.io.IOException - if there was an error while preparing a buffer with minBytes bytes

read

protected void read(java.nio.ByteBuffer buffer)
             throws java.io.EOFException,
                    java.io.IOException
Reads some data into the buffer. This method blocks until at least one byte is read or, if there are no more data, EOFException is thrown.

Parameters:
buffer - the buffer into which to read additional data
Throws:
java.io.EOFException - if there are no more data available
java.io.IOException - if there was an error reading data

read

public int read(java.nio.channels.ReadableByteChannel channel)
         throws java.io.IOException
Reads data from the specified channel. The read method is called on the channel.

Parameters:
channel - the readable channel from which the data is read
Returns:
number of bytes actually read
Throws:
java.io.IOException - if there was an I/O error reading from the channel

read

public int read(java.nio.channels.FileChannel channel,
                long position)
         throws java.io.IOException
Reads data from the specified file channel. The read method is called on the channel.

Parameters:
channel - the file channel from which the data is read
position - the position in the file where to read the data
Returns:
number of bytes actually read
Throws:
java.io.IOException - if there was an I/O error reading from the channel