Interface RandomAccessData
- All Superinterfaces:
AutoCloseable
,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 TypeMethodDescriptionlong
position()
Get the position in the sequence.default int
read
(byte[] buffer) Read bytes into the given buffer, starting at position0
in the buffer.default int
read
(byte[] buffer, int count) Read bytes into the given buffer, starting at position0
in the buffer.int
read
(byte[] buffer, int offset, int count) Read data into the given buffer, starting at positionoffset
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 currentposition
and the amount of bytes read will equalcount
, 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
size()
Get the number of bytes contained in this data sequence.
-
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 andsize()
, 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
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 andsize()
, both inclusiveIOException
- when an I/O error occurs
-
read
Read bytes into the given buffer, starting at position0
in the buffer. The data will be read from the currentposition
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
Read bytes into the given buffer, starting at position0
in the buffer. The data will be read from the currentposition
and the amount of bytes copied will equalcount
, 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 intocount
- 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 bufferIOException
- when an I/O error occurs
-
read
Read data into the given buffer, starting at positionoffset
in the buffer. The data will be read from the currentposition
and the amount of bytes read will equalcount
, 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 intooffset
- the offset in the buffer from which to start writingcount
- 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 bufferIOException
- when an I/O error occurs
-
readNBytes
Read from the data sequence, returning the read bytes as an array.The data will be read from the currentposition
and the amount of bytes read will equalcount
, 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
- ifcount
is negativeIOException
- when an I/O error occurs
-