Interface RandomAccessData

All Superinterfaces:
AutoCloseable, Closeable

public interface RandomAccessData extends Closeable
A random access readable byte sequence. It has a fixed length and an associated data type.

Note: implementations are not required to implement any kind of thread safety. It is up to the client to ensure this if necessary.

  • Method Summary

    Modifier and Type
    Method
    Description
    long
    Get the position in the sequence.
    default int
    read(byte[] buffer)
    Read bytes into the given buffer, starting at position 0 in the buffer.
    default int
    read(byte[] buffer, int count)
    Read bytes into the given buffer, starting at position 0 in the buffer.
    int
    read(byte[] buffer, int offset, int count)
    Read data into the given buffer, starting at position offset in the buffer.
    default byte[]
    readNBytes(int count)
    Read from the data sequence, returning the read bytes as an array.The data will be read from the current position and the amount of bytes read will equal count, unless the sequence contains fewer remaining bytes.
    default long
    Get the number of remaining bytes in this data sequence.
    void
    seek(long position)
    Move to the given absolute position in the data sequence.
    long
    Get the number of bytes contained in this data sequence.

    Methods inherited from interface java.io.Closeable

    close
  • Method Details

    • size

      long size()
      Get the number of bytes contained in this data sequence.
      Returns:
      the size in bytes
    • position

      long position()
      Get the position in the sequence. The position will be a number in range of 0 and size(), both inclusive.

      For example: if the size is equal to 16, the position can take any positive value up to and including 16.

      Returns:
      the current position
    • remaining

      default long remaining()
      Get the number of remaining bytes in this data sequence.
      Returns:
      size() - position()
    • seek

      void seek(long position) throws IOException
      Move to the given absolute position in the data sequence.
      Parameters:
      position - the position to move to
      Throws:
      IllegalArgumentException - if given position is not in range 0 and size(), both inclusive
      IOException - when an I/O error occurs
    • read

      default int read(byte[] buffer) throws IOException
      Read bytes into the given buffer, starting at position 0 in the buffer. The data will be read from the current position and the amount of bytes copied will equal the length of the buffer, unless the data sequence contains fewer remaining bytes. In that case, data is read until the end of the sequence.
      Parameters:
      buffer - the buffer to read into
      Returns:
      the number of bytes actually read
      Throws:
      IOException - when an I/O error occurs
    • read

      default int read(byte[] buffer, int count) throws IOException
      Read bytes into the given buffer, starting at position 0 in the buffer. The data will be read from the current position and the amount of bytes copied will equal count, unless the data sequence contains fewer remaining bytes. In that case, data is read until the end of the sequence.
      Parameters:
      buffer - the buffer to read into
      count - the amount of bytes to read
      Returns:
      the number of bytes actually read
      Throws:
      IllegalArgumentException - if count is negative or larger than the size of the buffer
      IOException - when an I/O error occurs
    • read

      int read(byte[] buffer, int offset, int count) throws IOException
      Read data into the given buffer, starting at position offset in the buffer. The data will be read from the current position and the amount of bytes read will equal count, unless the sequence contains fewer remaining bytes. In that case, data is read until the end of the sequence.
      Parameters:
      buffer - the buffer to read into
      offset - the offset in the buffer from which to start writing
      count - the amount of bytes to read
      Returns:
      the number of bytes actually read
      Throws:
      IllegalArgumentException - if count or offset is negative, or if offset + count is larger than the size of the buffer
      IOException - when an I/O error occurs
    • readNBytes

      default byte[] readNBytes(int count) throws IOException
      Read from the data sequence, returning the read bytes as an array.The data will be read from the current position and the amount of bytes read will equal count, unless the sequence contains fewer remaining bytes. In that case, data is read until the end of the sequence and a smaller array is returned, with a length equal to the number of read bytes.

      Note: this method will allocate a new buffer each time it is called. It is intended for simple cases where it is convenient to read a specified number of bytes into a byte array.

      Parameters:
      count - the amount of bytes to read
      Returns:
      a buffer containing the read bytes, or an empty array if we were at the end of the stream
      Throws:
      IllegalArgumentException - if count is negative
      IOException - when an I/O error occurs