Class BinaryReader

  • All Implemented Interfaces:
    Closeable, AutoCloseable
    Direct Known Subclasses:
    SplitInputStreamImpl

    public class BinaryReader
    extends InputStream
    Provides extended data access methods on binary data flows. Beyond supporting a byte-oriented view of the concatenated tokens, a number of methods for reading primitive type binary formats are provided. BinaryInputStream is also extended to provide support for reading from other "chunked" byte sources.
    See Also:
    BinaryBuilder
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected long absolutePosition
      The total number of bytes read
      protected byte[] buffer
      The current token value
      protected int position
      The offset of the next byte to read in the token
      protected int size
      The size of the current token
      protected InputStream source
      The underlying byte stream
    • Constructor Summary

      Constructors 
      Constructor Description
      BinaryReader​(byte[] bytes)
      Wraps a byte array with a byte-oriented reader.
      BinaryReader​(InputStream source, int bufferSize)
      Wraps an InputStream with a byte-oriented reader having extended function.
    • Field Detail

      • source

        protected InputStream source
        The underlying byte stream
      • buffer

        protected byte[] buffer
        The current token value
      • position

        protected int position
        The offset of the next byte to read in the token
      • size

        protected int size
        The size of the current token
      • absolutePosition

        protected long absolutePosition
        The total number of bytes read
    • Constructor Detail

      • BinaryReader

        public BinaryReader​(InputStream source,
                            int bufferSize)
        Wraps an InputStream with a byte-oriented reader having extended function. If an IOException is thrown by the wrapped source, the provided stream will be closed.

        The wrapped stream should not be accessed directly after creating the stream. Doing so may result in unexpected or otherwise unpredictable behavior in both the stream and the wrapper.

        Parameters:
        source - the stream to wrap
        bufferSize - the size of the read buffer to use, in bytes. Data will be read in chunks of this size from the wrapped stream.
      • BinaryReader

        public BinaryReader​(byte[] bytes)
        Wraps a byte array with a byte-oriented reader.
        Parameters:
        bytes - the byte array to wrap.
    • Method Detail

      • getAbsolutePosition

        public final long getAbsolutePosition()
        Gets the current position in the underlying data.
        Returns:
        the number of bytes read so far
      • close

        public final void close()
        Closes the wrapped port and releases any allocated resources. Subsequent reads will behave as if end of data has been reached.
        Specified by:
        close in interface AutoCloseable
        Specified by:
        close in interface Closeable
        Overrides:
        close in class InputStream
      • isEmpty

        public boolean isEmpty()
        Indicates whether the byte stream for reading has been exhausted.
        Returns:
        true if no more bytes are available to be read, false otherwise.
      • skipBytes

        public void skipBytes​(long count)
        Advances the read position in the stream the specified number of bytes.
        Parameters:
        count - the number of bytes to skip
      • readByte

        public byte readByte()
        Reads a single byte from the stream. The read position is advanced one byte as a result.
        Returns:
        the byte at the current position
      • readBytes

        public byte[] readBytes​(int len)
        Reads a specified number of bytes from the stream. The read position is advanced the specified number of bytes.
        Parameters:
        len - the number of bytes to read
        Returns:
        the next len bytes starting at the current position
      • readChar

        public char readChar()
        Reads a character value from the stream. The read position is advanced 2 bytes.
        Returns:
        the character value encoded at the current position
      • readChars

        public char[] readChars​(int count)
        Reads a specified number of character values from the stream. The read position is advanced 2 bytes for each character requested.
        Parameters:
        count - the number of characters to read
        Returns:
        the next count characters starting at the current position
      • readSingleByteChars

        public char[] readSingleByteChars​(int count)
        Reads a specified number of character values from the stream, assuming the bytes are the least-significant bytes of the values. The read position is advanced only one byte for each character requested.
        Parameters:
        count - the number of characters to read
        Returns:
        the next count characters starting at the current position
      • readInt

        public int readInt()
        Reads an int value from the stream. The read position is advanced 4 bytes.
        Returns:
        the int value encoded at the current position
      • readUnsignedBase128Int

        public int readUnsignedBase128Int()
        Reads a base-128 encoded int value from the stream, assuming an unsigned encoding. The read position is advanced to the position following the last byte of the encoding.
        Returns:
        the int value base-128 encoded at the current position
        See Also:
        BinaryBuilder.appendUnsignedBase128(int)
      • readBase128Int

        public int readBase128Int()
        Reads a base-128 encoded int value from the stream. The read position is advanced to the position following the last byte of the encoding.
        Returns:
        the int value base-128 encoded at the current position
        See Also:
        BinaryBuilder.appendBase128(int)
      • readLong

        public long readLong()
        Reads a long value from the stream. The read position is advanced 8 bytes.
        Returns:
        the long value encoded at the current position
      • readUnsignedBase128Long

        public long readUnsignedBase128Long()
        Reads a base-128 encoded long value from the stream, assuming an unsigned encoding. The read position is advanced to the position following the last byte of the encoding.
        Returns:
        the long value base-128 encoded at the current position
        See Also:
        BinaryBuilder.appendUnsignedBase128(long)
      • readBase128Long

        public long readBase128Long()
        Reads a base-128 encoded long value from the stream. The read position is advanced to the position following the last byte of the encoding.
        Returns:
        the long value base-128 encoded at the current position
        See Also:
        BinaryBuilder.appendBase128(int)
      • readFloat

        public float readFloat()
        Reads a float value from the stream. The read position is advanced 4 bytes. The bytes are interpreted using Float.intBitsToFloat(int).
        Returns:
        the float value encoded at the current position
      • readDouble

        public double readDouble()
        Reads a double value from the stream. The read position is advanced 8 bytes. The bytes are interpreted using Double.longBitsToDouble(long).
        Returns:
        the double value encoded at the current position