Package com.drew.lang

Class SequentialReader

java.lang.Object
com.drew.lang.SequentialReader
Direct Known Subclasses:
SequentialByteArrayReader, StreamReader

public abstract class SequentialReader extends Object
Author:
Drew Noakes https://drewnoakes.com
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    abstract int
    Returns an estimate of the number of bytes that can be read (or skipped over) from this SequentialReader without blocking by the next invocation of a method for this input stream.
    abstract byte
    Gets the next byte in the sequence.
    abstract void
    getBytes(byte[] buffer, int offset, int count)
    Retrieves bytes, writing them into a caller-provided buffer.
    abstract byte[]
    getBytes(int count)
    Returns the required number of bytes from the sequence.
    double
     
    float
     
    short
    Returns a signed 16-bit int calculated from two bytes of data (MSB, LSB).
    int
    Returns a signed 32-bit integer from four bytes of data.
    long
    Get a signed 64-bit integer from the buffer.
    byte
    Returns a signed 8-bit int calculated from the next byte the sequence.
    byte[]
    getNullTerminatedBytes(int maxLengthBytes)
    Returns the sequence of bytes punctuated by a \0 value.
    getNullTerminatedString(int maxLengthBytes, Charset charset)
    Creates a String from the stream, ending where byte=='\0' or where length==maxLength.
    getNullTerminatedStringValue(int maxLengthBytes, Charset charset)
    Creates a String from the stream, ending where byte=='\0' or where length==maxLength.
    abstract long
     
    float
    Gets a s15.16 fixed point float from the buffer.
    getString(int bytesRequested)
     
    getString(int bytesRequested, String charset)
     
    getString(int bytesRequested, Charset charset)
     
    getStringValue(int bytesRequested, Charset charset)
     
    int
    Returns an unsigned 16-bit int calculated from the next two bytes of the sequence.
    long
    Get a 32-bit unsigned integer from the buffer, returning it as a long.
    short
    Returns an unsigned 8-bit int calculated from the next byte of the sequence.
    boolean
    Gets the endianness of this reader.
    void
    setMotorolaByteOrder(boolean motorolaByteOrder)
    Sets the endianness of this reader.
    abstract void
    skip(long n)
    Skips forward in the sequence.
    abstract boolean
    trySkip(long n)
    Skips forward in the sequence, returning a boolean indicating whether the skip succeeded, or whether the sequence ended.

    Methods inherited from class java.lang.Object

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

    • SequentialReader

      public SequentialReader()
  • Method Details

    • getPosition

      public abstract long getPosition() throws IOException
      Throws:
      IOException
    • getByte

      public abstract byte getByte() throws IOException
      Gets the next byte in the sequence.
      Returns:
      The read byte value
      Throws:
      IOException
    • getBytes

      public abstract byte[] getBytes(int count) throws IOException
      Returns the required number of bytes from the sequence.
      Parameters:
      count - The number of bytes to be returned
      Returns:
      The requested bytes
      Throws:
      IOException
    • getBytes

      public abstract void getBytes(byte[] buffer, int offset, int count) throws IOException
      Retrieves bytes, writing them into a caller-provided buffer.
      Parameters:
      buffer - The array to write bytes to.
      offset - The starting position within buffer to write to.
      count - The number of bytes to be written.
      Throws:
      IOException
    • skip

      public abstract void skip(long n) throws IOException
      Skips forward in the sequence. If the sequence ends, an EOFException is thrown.
      Parameters:
      n - the number of byte to skip. Must be zero or greater.
      Throws:
      EOFException - the end of the sequence is reached.
      IOException - an error occurred reading from the underlying source.
    • trySkip

      public abstract boolean trySkip(long n) throws IOException
      Skips forward in the sequence, returning a boolean indicating whether the skip succeeded, or whether the sequence ended.
      Parameters:
      n - the number of byte to skip. Must be zero or greater.
      Returns:
      a boolean indicating whether the skip succeeded, or whether the sequence ended.
      Throws:
      IOException - an error occurred reading from the underlying source.
    • available

      public abstract int available()
      Returns an estimate of the number of bytes that can be read (or skipped over) from this SequentialReader without blocking by the next invocation of a method for this input stream. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.

      Note that while some implementations of SequentialReader like SequentialByteArrayReader will return the total remaining number of bytes in the stream, others will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.

      Returns:
      an estimate of the number of bytes that can be read (or skipped over) from this SequentialReader without blocking or 0 when it reaches the end of the input stream.
    • 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.
    • getUInt8

      public short getUInt8() throws IOException
      Returns an unsigned 8-bit int calculated from the next byte of the sequence.
      Returns:
      the 8 bit int value, between 0 and 255
      Throws:
      IOException
    • getInt8

      public byte getInt8() throws IOException
      Returns a signed 8-bit int calculated from the next byte the sequence.
      Returns:
      the 8 bit int value, between 0x00 and 0xFF
      Throws:
      IOException
    • getUInt16

      public int getUInt16() throws IOException
      Returns an unsigned 16-bit int calculated from the next two bytes of the sequence.
      Returns:
      the 16 bit int value, between 0x0000 and 0xFFFF
      Throws:
      IOException
    • getInt16

      public short getInt16() throws IOException
      Returns a signed 16-bit int calculated from two bytes of data (MSB, LSB).
      Returns:
      the 16 bit int value, between 0x0000 and 0xFFFF
      Throws:
      IOException - the buffer does not contain enough bytes to service the request
    • getUInt32

      public long getUInt32() throws IOException
      Get a 32-bit unsigned integer from the buffer, returning it as a long.
      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
    • getInt32

      public int getInt32() throws IOException
      Returns a signed 32-bit integer from four bytes of data.
      Returns:
      the signed 32 bit int value, between 0x00000000 and 0xFFFFFFFF
      Throws:
      IOException - the buffer does not contain enough bytes to service the request
    • getInt64

      public long getInt64() throws IOException
      Get a signed 64-bit integer from the buffer.
      Returns:
      the 64 bit int value, between 0x0000000000000000 and 0xFFFFFFFFFFFFFFFF
      Throws:
      IOException - the buffer does not contain enough bytes to service the request
    • getS15Fixed16

      public float getS15Fixed16() 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
    • getFloat32

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

      public double getDouble64() throws IOException
      Throws:
      IOException
    • getString

      public String getString(int bytesRequested) throws IOException
      Throws:
      IOException
    • getString

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

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

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

      public String getNullTerminatedString(int maxLengthBytes, Charset charset) throws IOException
      Creates a String from the stream, ending where byte=='\0' or where length==maxLength.
      Parameters:
      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 maxLengthBytes, Charset charset) throws IOException
      Creates a String from the stream, ending where byte=='\0' or where length==maxLength.
      Parameters:
      maxLengthBytes - The maximum number of bytes to read. If a \0 byte is not reached within this limit, reading will stop and the string will be truncated to this length.
      charset - The Charset to register with the returned StringValue, or null if the encoding is unknown
      Returns:
      The read string.
      Throws:
      IOException - The buffer does not contain enough bytes to satisfy this request.
    • getNullTerminatedBytes

      public byte[] getNullTerminatedBytes(int maxLengthBytes) throws IOException
      Returns the sequence of bytes punctuated by a \0 value.
      Parameters:
      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.