public abstract class RandomAccessReader extends Object
By default, the reader operates with Motorola byte order (big endianness). This can be changed by calling
setMotorolaByteOrder(boolean)
.
Concrete implementations include:
Constructor and Description |
---|
RandomAccessReader() |
Modifier and Type | Method and Description |
---|---|
protected 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 |
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.
|
String |
getNullTerminatedString(int index,
int maxLengthBytes)
Creates a String from the _data buffer starting at the specified index,
and ending where
byte=='\0' or where length==maxLength . |
float |
getS15Fixed16(int index)
Gets a s15.16 fixed point float from the buffer.
|
String |
getString(int index,
int bytesRequested) |
String |
getString(int index,
int bytesRequested,
String 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.
|
protected abstract void |
validateIndex(int index,
int bytesRequested)
Ensures that the buffered bytes extend to cover the specified index.
|
protected abstract byte getByte(int index) throws 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 byteIllegalArgumentException
- index
or count
are negativeBufferBoundsException
- if the requested byte is beyond the end of the underlying data sourceIOException
- if the byte is unable to be readpublic abstract byte[] getBytes(int index, int count) throws IOException
index
- The index from which the bytes begins in the underlying sourcecount
- The number of bytes to be returnedIllegalArgumentException
- index
or count
are negativeBufferBoundsException
- if the requested bytes extend beyond the end of the underlying data sourceIOException
- if the byte is unable to be readprotected abstract void validateIndex(int index, int bytesRequested) throws 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 requiredIOException
- if the stream ends before the required number of bytes are acquiredprotected abstract boolean isValidIndex(int index, int bytesRequested) throws IOException
IOException
public abstract long getLength() throws 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.
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 short getUInt8(int index) throws IOException
index
- position within the data buffer to read byteIOException
- the buffer does not contain enough bytes to service the request, or index is negativepublic byte getInt8(int index) throws IOException
index
- position within the data buffer to read byteIOException
- the buffer does not contain enough bytes to service the request, or index is negativepublic int getUInt16(int index) throws IOException
index
- position within the data buffer to read first byteIOException
- the buffer does not contain enough bytes to service the request, or index is negativepublic short getInt16(int index) throws IOException
index
- position within the data buffer to read first byteIOException
- the buffer does not contain enough bytes to service the request, or index is negativepublic long getUInt32(int index) throws IOException
index
- position within the data buffer to read first byteIOException
- the buffer does not contain enough bytes to service the request, or index is negativepublic int getInt32(int index) throws IOException
index
- position within the data buffer to read first byteIOException
- the buffer does not contain enough bytes to service the request, or index is negativepublic long getInt64(int index) throws IOException
index
- position within the data buffer to read first byteIOException
- the buffer does not contain enough bytes to service the request, or index is negativepublic float getS15Fixed16(int index) throws IOException
This particular fixed point encoding has one sign bit, 15 numerator bits and 16 denominator bits.
IOException
- the buffer does not contain enough bytes to service the request, or index is negativepublic float getFloat32(int index) throws IOException
IOException
public double getDouble64(int index) throws IOException
IOException
public String getString(int index, int bytesRequested) throws IOException
IOException
public String getString(int index, int bytesRequested, String charset) throws IOException
IOException
public String getNullTerminatedString(int index, int maxLengthBytes) throws 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.IOException
- The buffer does not contain enough bytes to satisfy this request.Copyright © 2002-2015 Drew Noakes. All Rights Reserved.