public abstract class RandomAccessReader
extends java.lang.Object
By default, the reader operates with Motorola byte order (big endianness). This can be changed by calling
setMotorolaByteOrder(boolean)
.
Concrete implementations include:
Modifier and Type | Field and Description |
---|---|
private boolean |
_isMotorolaByteOrder |
Constructor and Description |
---|
RandomAccessReader() |
Modifier and Type | Method and Description |
---|---|
boolean |
getBit(int index)
Gets whether a bit at a specific index is set or not.
|
abstract byte |
getByte(int index)
Gets the byte value at the specified byte
index . |
abstract byte[] |
getBytes(int index,
int count)
Returns the required number of bytes from the specified index from the underlying source.
|
double |
getDouble64(int index) |
float |
getFloat32(int index) |
short |
getInt16(int index)
Returns a signed 16-bit int calculated from two bytes of data at the specified index (MSB, LSB).
|
int |
getInt24(int index)
Get a 24-bit unsigned integer from the buffer, returning it as an int.
|
int |
getInt32(int index)
Returns a signed 32-bit integer from four bytes of data at the specified index the buffer.
|
long |
getInt64(int index)
Get a signed 64-bit integer from the buffer.
|
byte |
getInt8(int index)
Returns a signed 8-bit int calculated from one byte of data at the specified index.
|
abstract long |
getLength()
Returns the length of the data source in bytes.
|
byte[] |
getNullTerminatedBytes(int index,
int maxLengthBytes)
Returns the sequence of bytes punctuated by a
\0 value. |
java.lang.String |
getNullTerminatedString(int index,
int maxLengthBytes,
java.nio.charset.Charset charset)
Creates a String from the _data buffer starting at the specified index,
and ending where
byte=='\0' or where length==maxLength . |
StringValue |
getNullTerminatedStringValue(int index,
int maxLengthBytes,
java.nio.charset.Charset charset) |
float |
getS15Fixed16(int index)
Gets a s15.16 fixed point float from the buffer.
|
java.lang.String |
getString(int index,
int bytesRequested,
java.nio.charset.Charset charset) |
java.lang.String |
getString(int index,
int bytesRequested,
java.lang.String charset) |
StringValue |
getStringValue(int index,
int bytesRequested,
java.nio.charset.Charset charset) |
int |
getUInt16(int index)
Returns an unsigned 16-bit int calculated from two bytes of data at the specified index.
|
long |
getUInt32(int index)
Get a 32-bit unsigned integer from the buffer, returning it as a long.
|
short |
getUInt8(int index)
Returns an unsigned 8-bit int calculated from one byte of data at the specified index.
|
boolean |
isMotorolaByteOrder()
Gets the endianness of this reader.
|
protected abstract boolean |
isValidIndex(int index,
int bytesRequested) |
void |
setMotorolaByteOrder(boolean motorolaByteOrder)
Sets the endianness of this reader.
|
abstract int |
toUnshiftedOffset(int localOffset) |
protected abstract void |
validateIndex(int index,
int bytesRequested)
Ensures that the buffered bytes extend to cover the specified index.
|
public abstract int toUnshiftedOffset(int localOffset)
public abstract byte getByte(int index) throws java.io.IOException
index
.
Implementations should not perform any bounds checking in this method. That should be performed
in validateIndex
and isValidIndex
.
index
- The index from which to read the bytejava.lang.IllegalArgumentException
- index
is negativeBufferBoundsException
- if the requested byte is beyond the end of the underlying data sourcejava.io.IOException
- if the byte is unable to be readpublic abstract byte[] getBytes(int index, int count) throws java.io.IOException
index
- The index from which the bytes begins in the underlying sourcecount
- The number of bytes to be returnedjava.lang.IllegalArgumentException
- index
or count
are negativeBufferBoundsException
- if the requested bytes extend beyond the end of the underlying data sourcejava.io.IOException
- if the byte is unable to be readprotected abstract void validateIndex(int index, int bytesRequested) throws java.io.IOException
If the stream ends before the point is reached, a BufferBoundsException
is raised.
index
- the index from which the required bytes startbytesRequested
- the number of bytes which are requiredjava.io.IOException
- if the stream ends before the required number of bytes are acquiredprotected abstract boolean isValidIndex(int index, int bytesRequested) throws java.io.IOException
java.io.IOException
public abstract long getLength() throws java.io.IOException
This is a simple operation for implementations (such as RandomAccessFileReader
and
ByteArrayReader
) that have the entire data source available.
Users of this method must be aware that sequentially accessed implementations such as
RandomAccessStreamReader
will have to read and buffer the entire data source in
order to determine the length.
java.io.IOException
public void setMotorolaByteOrder(boolean motorolaByteOrder)
true
for Motorola (or big) endianness (also known as network byte order), with MSB before LSB.false
for Intel (or little) endianness, with LSB before MSB.motorolaByteOrder
- true
for Motorola/big endian, false
for Intel/little endianpublic boolean isMotorolaByteOrder()
true
for Motorola (or big) endianness (also known as network byte order), with MSB before LSB.false
for Intel (or little) endianness, with LSB before MSB.public boolean getBit(int index) throws java.io.IOException
index
- the number of bits at which to testjava.io.IOException
- the buffer does not contain enough bytes to service the request, or index is negativepublic short getUInt8(int index) throws java.io.IOException
index
- position within the data buffer to read bytejava.io.IOException
- the buffer does not contain enough bytes to service the request, or index is negativepublic byte getInt8(int index) throws java.io.IOException
index
- position within the data buffer to read bytejava.io.IOException
- the buffer does not contain enough bytes to service the request, or index is negativepublic int getUInt16(int index) throws java.io.IOException
index
- position within the data buffer to read first bytejava.io.IOException
- the buffer does not contain enough bytes to service the request, or index is negativepublic short getInt16(int index) throws java.io.IOException
index
- position within the data buffer to read first bytejava.io.IOException
- the buffer does not contain enough bytes to service the request, or index is negativepublic int getInt24(int index) throws java.io.IOException
index
- position within the data buffer to read first bytejava.io.IOException
- the buffer does not contain enough bytes to service the request, or index is negativepublic long getUInt32(int index) throws java.io.IOException
index
- position within the data buffer to read first bytejava.io.IOException
- the buffer does not contain enough bytes to service the request, or index is negativepublic int getInt32(int index) throws java.io.IOException
index
- position within the data buffer to read first bytejava.io.IOException
- the buffer does not contain enough bytes to service the request, or index is negativepublic long getInt64(int index) throws java.io.IOException
index
- position within the data buffer to read first bytejava.io.IOException
- the buffer does not contain enough bytes to service the request, or index is negativepublic float getS15Fixed16(int index) throws java.io.IOException
This particular fixed point encoding has one sign bit, 15 numerator bits and 16 denominator bits.
java.io.IOException
- the buffer does not contain enough bytes to service the request, or index is negativepublic float getFloat32(int index) throws java.io.IOException
java.io.IOException
public double getDouble64(int index) throws java.io.IOException
java.io.IOException
public StringValue getStringValue(int index, int bytesRequested, java.nio.charset.Charset charset) throws java.io.IOException
java.io.IOException
public java.lang.String getString(int index, int bytesRequested, java.nio.charset.Charset charset) throws java.io.IOException
java.io.IOException
public java.lang.String getString(int index, int bytesRequested, java.lang.String charset) throws java.io.IOException
java.io.IOException
public java.lang.String getNullTerminatedString(int index, int maxLengthBytes, java.nio.charset.Charset charset) throws java.io.IOException
byte=='\0'
or where length==maxLength
.index
- The index within the buffer at which to start reading the string.maxLengthBytes
- The maximum number of bytes to read. If a zero-byte is not reached within this limit,
reading will stop and the string will be truncated to this length.java.io.IOException
- The buffer does not contain enough bytes to satisfy this request.public StringValue getNullTerminatedStringValue(int index, int maxLengthBytes, java.nio.charset.Charset charset) throws java.io.IOException
java.io.IOException
public byte[] getNullTerminatedBytes(int index, int maxLengthBytes) throws java.io.IOException
\0
value.index
- The index within the buffer at which to start reading the string.maxLengthBytes
- The maximum number of bytes to read. If a \0
byte is not reached within this limit,
the returned array will be maxLengthBytes
long.java.io.IOException
- The buffer does not contain enough bytes to satisfy this request.