public class BufferInputStream extends java.io.InputStream implements BinaryInput
BinaryInput
using
an internal buffer
.
If multiple threads use the same instance of this class, the access to the instance must be synchronized.
BufferOutputStream
Constructor and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
int |
available() |
int |
bufferedSize()
Returns the number of bytes currently in the buffer.
|
long |
getPosition()
Returns the current position in this input stream.
|
java.nio.ByteOrder |
order()
Retrieves the byte order of the encapsulated buffer.
|
java.nio.ByteBuffer |
order(java.nio.ByteOrder bo)
Modifies the encapsulated buffer's byte order.
|
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. |
public BufferInputStream(int bufferSize, boolean bufferDirect) throws java.lang.IllegalArgumentException
bufferSize
- the size of the internal bufferbufferDirect
- allocate the internal buffer as direct
java.lang.IllegalArgumentException
- if there specified buffer size is not validpublic BufferInputStream(java.nio.ByteBuffer buffer)
buffer
- the internal buffer for this streampublic BufferInputStream(byte[] array)
wraps
the given data array.array
- the data array for this streampublic long getPosition()
public void setPosition(long position) throws java.io.IOException
position
- the new position in this input streamjava.io.IOException
- if the position is invalid or there was another I/O errorpublic int bufferedSize()
public final java.nio.ByteOrder order()
The byte order is used when reading or writing multibyte values, and
when creating buffers that are views of this byte buffer. The order of
a newly-created byte buffer is always BIG_ENDIAN
.
public final java.nio.ByteBuffer order(java.nio.ByteOrder bo)
bo
- The new byte order, either BIG_ENDIAN
or LITTLE_ENDIAN
public int read() throws java.io.IOException
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.read
in class java.io.InputStream
-1
if the end of the stream is reachedjava.io.IOException
- if there was an error using readChannelpublic int read(byte[] buf, int off, int len) throws java.io.IOException, java.lang.IndexOutOfBoundsException
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.read
in class java.io.InputStream
buf
- the buffer into which the data is readoff
- the start offset in array buf
at which the data is writtenlen
- the maximum number of bytes to read-1
if there is no more data because the end of
the stream has been reachedjava.io.IOException
- if there was an error reading from the input streamjava.lang.IndexOutOfBoundsException
- if the buf.length - off < len
public long skip(long n) throws java.io.IOException
BinaryInput
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.skip
in interface BinaryInput
skip
in class java.io.InputStream
n
- the number of bytes to be skippedjava.io.IOException
- if the input does not support seek or some other I/O error occurspublic int available()
available
in class java.io.InputStream
public java.nio.ByteBuffer readInput(int minBytes) throws java.io.IOException
BinaryInput
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
.readInput
in interface BinaryInput
minBytes
- the minimal number of bytes that must be available for reading from the bufferjava.io.IOException
- if there was an error while preparing a buffer with minBytes
bytesprotected void read(java.nio.ByteBuffer buffer) throws java.io.EOFException, java.io.IOException
buffer
.
This method blocks until at least one byte is read or,
if there are no more data, EOFException
is thrown.buffer
- the buffer into which to read additional datajava.io.EOFException
- if there are no more data availablejava.io.IOException
- if there was an error reading datapublic int read(java.nio.channels.ReadableByteChannel channel) throws java.io.IOException
read
method is called on the channel.channel
- the readable channel from which the data is readjava.io.IOException
- if there was an I/O error reading from the channelpublic int read(java.nio.channels.FileChannel channel, long position) throws java.io.IOException
read
method is called on the channel.channel
- the file channel from which the data is readposition
- the position in the file where to read the datajava.io.IOException
- if there was an I/O error reading from the channel