Package com.drew.lang

Class ByteArrayReader


  • public class ByteArrayReader
    extends RandomAccessReader
    Provides methods to read specific values from a byte array, with a consistent, checked exception structure for issues.

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

    • Field Detail

      • _buffer

        private final byte[] _buffer
      • _baseOffset

        private final int _baseOffset
    • Constructor Detail

      • ByteArrayReader

        public ByteArrayReader​(byte[] buffer)
      • ByteArrayReader

        public ByteArrayReader​(byte[] buffer,
                               int baseOffset)
    • Method Detail

      • getLength

        public long getLength()
        Description copied from class: RandomAccessReader
        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.

        Specified by:
        getLength in class RandomAccessReader
        Returns:
        the length of the data source, in bytes.
      • getByte

        public byte getByte​(int index)
                     throws java.io.IOException
        Description copied from class: RandomAccessReader
        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.

        Specified by:
        getByte in class RandomAccessReader
        Parameters:
        index - The index from which to read the byte
        Returns:
        The read byte value
        Throws:
        BufferBoundsException - if the requested byte is beyond the end of the underlying data source
        java.io.IOException - if the byte is unable to be read
      • validateIndex

        protected void validateIndex​(int index,
                                     int bytesRequested)
                              throws java.io.IOException
        Description copied from class: RandomAccessReader
        Ensures that the buffered bytes extend to cover the specified index. If not, an attempt is made to read to that point.

        If the stream ends before the point is reached, a BufferBoundsException is raised.

        Specified by:
        validateIndex in class RandomAccessReader
        Parameters:
        index - the index from which the required bytes start
        bytesRequested - the number of bytes which are required
        Throws:
        java.io.IOException - if the stream ends before the required number of bytes are acquired
      • isValidIndex

        protected boolean isValidIndex​(int index,
                                       int bytesRequested)
                                throws java.io.IOException
        Specified by:
        isValidIndex in class RandomAccessReader
        Throws:
        java.io.IOException
      • getBytes

        public byte[] getBytes​(int index,
                               int count)
                        throws java.io.IOException
        Description copied from class: RandomAccessReader
        Returns the required number of bytes from the specified index from the underlying source.
        Specified by:
        getBytes in class RandomAccessReader
        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:
        BufferBoundsException - if the requested bytes extend beyond the end of the underlying data source
        java.io.IOException - if the byte is unable to be read