Interface RandomAccessData
-
- All Superinterfaces:
AutoCloseable
- All Known Implementing Classes:
RandomAccessByteArrayImpl
,RandomAccessDataScope
,RandomAccessFileChannelImpl
,RandomAccessFileImpl
,RandomAccessFileStreamImpl
public interface RandomAccessData extends AutoCloseable
This interface is an abstraction for random data access. Implementations of this interface could use theRandomAccessFile
class or could implement a similar functionality based on byte arrays.- See Also:
RandomAccessDataFactory
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
close()
long
getCurrentOffset()
Returns the current offset within this data.long
getLength()
Returns the length of the data in bytes.boolean
isClosed()
Returns true, if the data source was closed before.void
readBytes(byte[] buffer, int offset, int length)
Reads an array of bytes of the given length from this data source into the given buffer at the given offset.byte[]
readBytes(int length)
Reads an array of bytes of the given length from this data source.int
readUnsignedByte()
Reads an unsigned eight bit number from this data.void
setCurrentOffset(long offset)
Sets the current offset within this data.
-
-
-
Method Detail
-
close
void close() throws RandomAccessDataException
- Specified by:
close
in interfaceAutoCloseable
- Throws:
RandomAccessDataException
-
isClosed
boolean isClosed()
Returns true, if the data source was closed before.- Returns:
- true, if the data source was closed
-
getLength
long getLength() throws RandomAccessDataException
Returns the length of the data in bytes.- Returns:
- the length of the data in bytes
- Throws:
RandomAccessDataException
- if RandomAccessData source is invalid
-
getCurrentOffset
long getCurrentOffset() throws RandomAccessDataException
Returns the current offset within this data.- Returns:
- offset in bytes
- Throws:
RandomAccessDataException
- if RandomAccessData source is invalid
-
setCurrentOffset
void setCurrentOffset(long offset) throws RandomAccessDataException, IllegalArgumentException
Sets the current offset within this data. The offset may be set beyond the end of the file without throwing an exception. (Reading after that will do so.)- Parameters:
offset
- offset in bytes- Throws:
RandomAccessDataException
- if an I/O error occursIllegalArgumentException
- if the offset given is less than zero
-
readUnsignedByte
int readUnsignedByte() throws RandomAccessDataException
Reads an unsigned eight bit number from this data.- Returns:
- the next byte of this file as an unsigned eight bit number
- Throws:
RandomAccessDataException
- if an I/O error occursRandomAccessEndOfDataException
- if the end of data is reached
-
readBytes
byte[] readBytes(int length) throws RandomAccessDataException
Reads an array of bytes of the given length from this data source.- Parameters:
length
- the number of bytes to read- Returns:
- the bytes read as byte array
- Throws:
RandomAccessDataException
- if an I/O error occursRandomAccessEndOfDataException
- if the end of data is reached
-
readBytes
void readBytes(byte[] buffer, int offset, int length) throws RandomAccessDataException
Reads an array of bytes of the given length from this data source into the given buffer at the given offset. The caller is responsible to provide a buffer of sufficient size.- Parameters:
buffer
- the buffer to write the read data tooffset
- starting index to write the data read into the bufferlength
- the number of bytes to read- Throws:
RandomAccessDataException
- if an I/O error occursRandomAccessEndOfDataException
- if the end of data is reached- See Also:
readBytes(int)
-
-