Package com.drew.lang

Class RandomAccessReader

java.lang.Object
com.drew.lang.RandomAccessReader
Direct Known Subclasses:
ByteArrayReader, RandomAccessFileReader, RandomAccessStreamReader

public abstract class RandomAccessReader extends Object
Base class for random access data reading operations of common data types.

By default, the reader operates with Motorola byte order (big endianness). This can be changed by calling setMotorolaByteOrder(boolean).

Concrete implementations include:

Author:
Drew Noakes https://drewnoakes.com
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    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
    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.
    getNullTerminatedString(int index, int maxLengthBytes, Charset charset)
    Creates a String from the _data buffer starting at the specified index, and ending where byte=='\0' or where length==maxLength.
    getNullTerminatedStringValue(int index, int maxLengthBytes, Charset charset)
     
    float
    getS15Fixed16(int index)
    Gets a s15.16 fixed point float from the buffer.
    getString(int index, int bytesRequested, String charset)
     
    getString(int index, int bytesRequested, Charset charset)
     
    getStringValue(int index, int bytesRequested, 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
    Gets the endianness of this reader.
    void
    setMotorolaByteOrder(boolean motorolaByteOrder)
    Sets the endianness of this reader.
    abstract int
    toUnshiftedOffset(int localOffset)
     

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • RandomAccessReader

      public RandomAccessReader()
  • Method Details

    • toUnshiftedOffset

      public abstract int toUnshiftedOffset(int localOffset)
    • getByte

      public abstract byte getByte(int index) throws IOException
      Gets the byte value at the specified byte index.

      Implementations should not perform any bounds checking in this method. That should be performed in validateIndex and isValidIndex.

      Parameters:
      index - The index from which to read the byte
      Returns:
      The read byte value
      Throws:
      IllegalArgumentException - index is negative
      BufferBoundsException - if the requested byte is beyond the end of the underlying data source
      IOException - if the byte is unable to be read
    • getBytes

      public abstract byte[] getBytes(int index, int count) throws IOException
      Returns the required number of bytes from the specified index from the underlying source.
      Parameters:
      index - The index from which the bytes begins in the underlying source
      count - The number of bytes to be returned
      Returns:
      The requested bytes
      Throws:
      IllegalArgumentException - index or count are negative
      BufferBoundsException - if the requested bytes extend beyond the end of the underlying data source
      IOException - if the byte is unable to be read
    • getLength

      public abstract long getLength() throws IOException
      Returns the length of the data source in bytes.

      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.

      Returns:
      the length of the data source, in bytes.
      Throws:
      IOException
    • setMotorolaByteOrder

      public void setMotorolaByteOrder(boolean motorolaByteOrder)
      Sets the endianness of this reader.
      • 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.
      Parameters:
      motorolaByteOrder - true for Motorola/big endian, false for Intel/little endian
    • isMotorolaByteOrder

      public boolean isMotorolaByteOrder()
      Gets the endianness of this reader.
      • 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.
    • getBit

      public boolean getBit(int index) throws IOException
      Gets whether a bit at a specific index is set or not.
      Parameters:
      index - the number of bits at which to test
      Returns:
      true if the bit is set, otherwise false
      Throws:
      IOException - the buffer does not contain enough bytes to service the request, or index is negative
    • getUInt8

      public short getUInt8(int index) throws IOException
      Returns an unsigned 8-bit int calculated from one byte of data at the specified index.
      Parameters:
      index - position within the data buffer to read byte
      Returns:
      the 8 bit int value, between 0 and 255
      Throws:
      IOException - the buffer does not contain enough bytes to service the request, or index is negative
    • getInt8

      public byte getInt8(int index) throws IOException
      Returns a signed 8-bit int calculated from one byte of data at the specified index.
      Parameters:
      index - position within the data buffer to read byte
      Returns:
      the 8 bit int value, between 0x00 and 0xFF
      Throws:
      IOException - the buffer does not contain enough bytes to service the request, or index is negative
    • getUInt16

      public int getUInt16(int index) throws IOException
      Returns an unsigned 16-bit int calculated from two bytes of data at the specified index.
      Parameters:
      index - position within the data buffer to read first byte
      Returns:
      the 16 bit int value, between 0x0000 and 0xFFFF
      Throws:
      IOException - the buffer does not contain enough bytes to service the request, or index is negative
    • getInt16

      public short getInt16(int index) throws IOException
      Returns a signed 16-bit int calculated from two bytes of data at the specified index (MSB, LSB).
      Parameters:
      index - position within the data buffer to read first byte
      Returns:
      the 16 bit int value, between 0x0000 and 0xFFFF
      Throws:
      IOException - the buffer does not contain enough bytes to service the request, or index is negative
    • getInt24

      public int getInt24(int index) throws IOException
      Get a 24-bit unsigned integer from the buffer, returning it as an int.
      Parameters:
      index - position within the data buffer to read first byte
      Returns:
      the unsigned 24-bit int value as a long, between 0x00000000 and 0x00FFFFFF
      Throws:
      IOException - the buffer does not contain enough bytes to service the request, or index is negative
    • getUInt32

      public long getUInt32(int index) throws IOException
      Get a 32-bit unsigned integer from the buffer, returning it as a long.
      Parameters:
      index - position within the data buffer to read first byte
      Returns:
      the unsigned 32-bit int value as a long, between 0x00000000 and 0xFFFFFFFF
      Throws:
      IOException - the buffer does not contain enough bytes to service the request, or index is negative
    • getInt32

      public int getInt32(int index) throws IOException
      Returns a signed 32-bit integer from four bytes of data at the specified index the buffer.
      Parameters:
      index - position within the data buffer to read first byte
      Returns:
      the signed 32 bit int value, between 0x00000000 and 0xFFFFFFFF
      Throws:
      IOException - the buffer does not contain enough bytes to service the request, or index is negative
    • getInt64

      public long getInt64(int index) throws IOException
      Get a signed 64-bit integer from the buffer.
      Parameters:
      index - position within the data buffer to read first byte
      Returns:
      the 64 bit int value, between 0x0000000000000000 and 0xFFFFFFFFFFFFFFFF
      Throws:
      IOException - the buffer does not contain enough bytes to service the request, or index is negative
    • getS15Fixed16

      public float getS15Fixed16(int index) throws IOException
      Gets a s15.16 fixed point float from the buffer.

      This particular fixed point encoding has one sign bit, 15 numerator bits and 16 denominator bits.

      Returns:
      the floating point value
      Throws:
      IOException - the buffer does not contain enough bytes to service the request, or index is negative
    • getFloat32

      public float getFloat32(int index) throws IOException
      Throws:
      IOException
    • getDouble64

      public double getDouble64(int index) throws IOException
      Throws:
      IOException
    • getStringValue

      public StringValue getStringValue(int index, int bytesRequested, Charset charset) throws IOException
      Throws:
      IOException
    • getString

      public String getString(int index, int bytesRequested, Charset charset) throws IOException
      Throws:
      IOException
    • getString

      public String getString(int index, int bytesRequested, String charset) throws IOException
      Throws:
      IOException
    • getNullTerminatedString

      public String getNullTerminatedString(int index, int maxLengthBytes, Charset charset) throws IOException
      Creates a String from the _data buffer starting at the specified index, and ending where byte=='\0' or where length==maxLength.
      Parameters:
      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.
      Returns:
      The read string.
      Throws:
      IOException - The buffer does not contain enough bytes to satisfy this request.
    • getNullTerminatedStringValue

      public StringValue getNullTerminatedStringValue(int index, int maxLengthBytes, Charset charset) throws IOException
      Throws:
      IOException
    • getNullTerminatedBytes

      public byte[] getNullTerminatedBytes(int index, int maxLengthBytes) throws IOException
      Returns the sequence of bytes punctuated by a \0 value.
      Parameters:
      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.
      Returns:
      The read byte array, excluding the null terminator.
      Throws:
      IOException - The buffer does not contain enough bytes to satisfy this request.