public interface RandomDataInput
RandomDataInput
class provides a series of methods for reading data
from various types of inputs. It allows to read data from an input source
in a non-sequential manner, i.e., the data can be accessed at any offset.
This class supports reading of primitive data types like int
,
long
, double
etc., as well as more complex data structures
like byte[]
, String
and ByteBuffer
. It also provides
methods for direct reading from memory and for reading with a load barrier.
Furthermore, the RandomDataInput
class provides additional methods for
advanced operations like copying data to native memory, finding a specific byte,
calculating the hash code of a sequence of bytes, and more.
Methods in this class may throw BufferUnderflowException
if the offset
specified is outside the limits of the byte sequence or IllegalStateException
if the byte sequence has been released.
Note: Implementations of this class are typically not thread-safe. If multiple
threads interact with a RandomDataInput
instance concurrently, it must be synchronized
externally.
Modifier and Type | Field and Description |
---|---|
static String[] |
charToString |
Modifier and Type | Method and Description |
---|---|
default double |
addAndGetDouble(long offset,
double adding)
Deprecated.
|
default float |
addAndGetFloat(long offset,
float adding)
Deprecated.
|
default int |
addAndGetInt(long offset,
int adding)
Deprecated.
|
default long |
addAndGetLong(long offset,
long adding)
Deprecated.
|
long |
addressForRead(long offset)
Retrieves the underlying memory address for reading.
|
default long |
addressForRead(long offset,
int buffer)
Retrieves the underlying memory address for reading.
|
long |
addressForWrite(long offset)
Retrieves the underlying memory address for writing.
|
long |
addressForWritePosition()
Retrieves the underlying memory address for writing at the current write position.
|
default ByteOrder |
byteOrder()
Retrieves the byte order used by the buffer.
|
@NotNull Bytes<?> |
bytesForRead()
Retrieves a Bytes object for reading.
|
@NotNull Bytes<?> |
bytesForWrite()
Retrieves a Bytes object for writing.
|
default boolean |
canReadDirect()
Checks if this byte sequence can be read directly from native memory.
|
default boolean |
canReadDirect(long length)
Checks if the specified length of bytes can be read directly from native memory.
|
default long |
capacity() |
default boolean |
compareAndSwapDouble(long offset,
double expected,
double value)
Deprecated.
|
default boolean |
compareAndSwapFloat(long offset,
float expected,
float value)
Deprecated.
|
boolean |
compareAndSwapInt(long offset,
int expected,
int value)
Deprecated.
|
boolean |
compareAndSwapLong(long offset,
long expected,
long value)
Deprecated.
|
default boolean |
compareUtf8(long offset,
@Nullable CharSequence other)
Compares the UTF-8 encoded char sequence, written in this
RandomDataInput at the
given offset, with the given char sequence. |
default int |
copyTo(byte[] bytes)
Copies data from this byte source into a byte array.
|
default int |
copyTo(@NotNull ByteBuffer bb)
Copies data from this RandomDataInput to the specified ByteBuffer.
|
static @NotNull String[] |
createCharToString()
Creates a lookup table mapping byte values to their corresponding String representations.
|
default int |
fastHash(long offset,
int length)
Computes a hash code for a sequence of bytes.
|
default long |
findByte(byte stopByte)
Finds the first occurrence of the specified byte in this byte sequence.
|
boolean |
isDirectMemory()
Checks if the buffer uses direct memory.
|
default long |
lengthWritten(long startPosition)
Calculates the length of data written from the given start position.
|
void |
nativeRead(long position,
long address,
long size)
Expert-level method for transferring data from this byte source to native memory.
|
default long |
parseLong(long offset)
Parses a long value from a specified offset.
|
int |
peekUnsignedByte(long offset)
Reads an unsigned byte value from a specific offset.
|
default int |
peekVolatileInt()
Reads a volatile int value from the current reading position.
|
default String |
printable(long offset)
Reads a byte value from a specified offset and converts it into a printable string.
|
default long |
read(long offsetInRDI,
byte[] bytes,
int offset,
int length)
Reads a sequence of bytes from the specified offset into a byte array.
|
default boolean |
readBoolean(long offset)
Reads a boolean value from a specific offset.
|
byte |
readByte(long offset)
Reads a byte value from a specific offset.
|
double |
readDouble(long offset)
Reads a 64-bit floating point value from a specified offset.
|
float |
readFloat(long offset)
Reads a 32-bit floating point value from a specified offset.
|
default long |
readIncompleteLong(long offset)
Reads a long value from the specified offset.
|
int |
readInt(long offset)
Reads a 32-bit integer value from a specific offset.
|
default long |
readLimit()
If the resource is closed, the returned value is unspecified.
|
long |
readLong(long offset)
Reads a 64-bit long value from a specific offset.
|
default long |
readPosition()
Returns the read position.
|
default long |
readRemaining()
Calculates the number of bytes remaining that can be read from the current read position.
|
short |
readShort(long offset)
Reads a short value from a specific offset.
|
default int |
readUnsignedByte(long offset)
Reads an unsigned byte value from a specific offset.
|
default long |
readUnsignedInt(long offset)
Reads an unsigned 32-bit integer value from a specific offset.
|
default int |
readUnsignedInt24(long offset)
Reads an unsigned 24-bit integer value from a specific offset.
|
default int |
readUnsignedShort(long offset)
Reads an unsigned short value from a specific offset.
|
default <T extends Appendable & CharSequence> |
readUtf8(long offset,
T sb)
Truncates
sb (it must be a StringBuilder or Bytes ) and reads a char
sequence from the given offset , encoded as Utf8, into it. |
default @Nullable String |
readUtf8Limited(long offset,
int maxUtf8Len)
Reads a char sequence from the given
offset , encoded as Utf8. |
default <T extends Appendable & CharSequence> |
readUtf8Limited(long offset,
T sb,
int maxUtf8Len)
Truncates
sb (it must be a StringBuilder or Bytes ) and reads a char
sequence from the given offset , encoded as Utf8, into it. |
byte |
readVolatileByte(long offset)
Reads a volatile 8-bit byte value from a specified offset.
|
default double |
readVolatileDouble(long offset)
Reads a volatile 64-bit double value from a specified offset.
|
default float |
readVolatileFloat(long offset)
Reads a volatile 32-bit floating point value from a specified offset.
|
int |
readVolatileInt(long offset)
Reads a volatile 32-bit integer value from a specified offset.
|
long |
readVolatileLong(long offset)
Reads a volatile 64-bit long value from a specified offset.
|
short |
readVolatileShort(long offset)
Reads a volatile 16-bit short value from a specified offset.
|
long |
realCapacity()
Returns the actual capacity that can be potentially read from this byte source.
|
default long |
realReadRemaining()
Calculates the number of bytes that can be safely read directly.
|
default long |
realWriteRemaining()
Calculates the number of bytes remaining that can be written from the current write position with resizing.
|
boolean |
sharedMemory()
Checks if the Bytes use shared memory.
|
default long |
start() |
default @NotNull BytesStore |
subBytes(long start,
long length)
Returns a new BytesStore that is a subsequence of this byte sequence, starting at the specified index and of the specified length.
|
void |
testAndSetInt(long offset,
int expected,
int value)
Deprecated.
|
default byte[] |
toByteArray() |
default ByteBuffer |
toTemporaryDirectByteBuffer()
Converts the byte sequence into a direct byte buffer.
|
default long |
writeLimit()
Retrieves the maximum writable position within the buffer.
|
default long |
writePosition()
Returns the write position.
|
default long |
writeRemaining()
Calculates the number of bytes remaining that can be written from the current write position.
|
static final String[] charToString
@NotNull static @NotNull String[] createCharToString()
default int peekVolatileInt() throws BufferUnderflowException, IllegalStateException
BufferUnderflowException
- if the reading position is outside the bounds of the byte source.IllegalStateException
- if the byte source has been released.default boolean readBoolean(long offset) throws BufferUnderflowException, IllegalStateException
offset
- the location from where the boolean value is read.BufferUnderflowException
- if the offset is outside the bounds of the byte source.IllegalStateException
- if the byte source has been released.byte readByte(long offset) throws BufferUnderflowException, IllegalStateException
offset
- the location from where the byte value is read.BufferUnderflowException
- if the offset is outside the bounds of the byte source.IllegalStateException
- if the byte source has been released.default int readUnsignedByte(long offset) throws BufferUnderflowException, IllegalStateException
offset
- the location from where the unsigned byte value is read.BufferUnderflowException
- if the offset is outside the bounds of the byte source.IllegalStateException
- if the byte source has been released.int peekUnsignedByte(long offset) throws IllegalStateException
offset
- the location from where the unsigned byte value is read.IllegalStateException
- if the byte source has been released.short readShort(long offset) throws BufferUnderflowException, IllegalStateException
offset
- the location from where the short value is read.BufferUnderflowException
- if the offset is outside the bounds of the byte source.IllegalStateException
- if the byte source has been released.default int readUnsignedShort(long offset) throws BufferUnderflowException, IllegalStateException
offset
- the location from where the unsigned short value is read.BufferUnderflowException
- if the offset is outside the bounds of the byte source.IllegalStateException
- if the byte source has been released.default int readUnsignedInt24(long offset) throws BufferUnderflowException, IllegalStateException
offset
- the location from where the unsigned 24-bit integer value is read.BufferUnderflowException
- if the offset is outside the bounds of the byte source.IllegalStateException
- if the byte source has been released.int readInt(long offset) throws BufferUnderflowException, IllegalStateException
offset
- the location from where the 32-bit integer value is read.BufferUnderflowException
- if the offset is outside the bounds of the byte source.IllegalStateException
- if the byte source has been released.default long readUnsignedInt(long offset) throws BufferUnderflowException, IllegalStateException
offset
- the location from where the unsigned 32-bit integer value is read.BufferUnderflowException
- if the offset is outside the bounds of the byte source.IllegalStateException
- if the byte source has been released.long readLong(long offset) throws BufferUnderflowException, IllegalStateException
offset
- the location from where the long value is read.BufferUnderflowException
- if the offset is outside the bounds of the byte source.IllegalStateException
- if the byte source has been released.float readFloat(long offset) throws BufferUnderflowException, IllegalStateException
offset
- the location from where the float value is read.BufferUnderflowException
- if the offset is beyond the limits of the byte source.IllegalStateException
- if the byte source has been released.double readDouble(long offset) throws BufferUnderflowException, IllegalStateException
offset
- the location from where the double value is read.BufferUnderflowException
- if the offset is beyond the limits of the byte source.IllegalStateException
- if the byte source has been released.default String printable(long offset) throws BufferUnderflowException, IllegalStateException
offset
- the location from where the byte value is read.BufferUnderflowException
- if the offset is beyond the limits of the byte source.IllegalStateException
- if the byte source has been released.byte readVolatileByte(long offset) throws BufferUnderflowException, IllegalStateException
offset
- the location from where the byte value is read.BufferUnderflowException
- if the offset is beyond the limits of the byte source.IllegalStateException
- if the byte source has been released.short readVolatileShort(long offset) throws BufferUnderflowException, IllegalStateException
offset
- the location from where the short value is read.BufferUnderflowException
- if the offset is beyond the limits of the byte source.IllegalStateException
- if the byte source has been released.int readVolatileInt(long offset) throws BufferUnderflowException, IllegalStateException
offset
- the location from where the int value is read.BufferUnderflowException
- if the offset is beyond the limits of the byte source.IllegalStateException
- if the byte source has been released.default float readVolatileFloat(long offset) throws BufferUnderflowException, IllegalStateException
offset
- the location from where the float value is read.BufferUnderflowException
- if the offset is beyond the limits of the byte source.IllegalStateException
- if the byte source has been released.long readVolatileLong(long offset) throws BufferUnderflowException, IllegalStateException
offset
- the location from where the long value is read.BufferUnderflowException
- if the offset is beyond the limits of the byte source.IllegalStateException
- if the byte source has been released.default double readVolatileDouble(long offset) throws BufferUnderflowException, IllegalStateException
offset
- the location from where the double value is read.BufferUnderflowException
- if the offset is beyond the limits of the byte source.IllegalStateException
- if the byte source has been released.default long parseLong(long offset) throws BufferUnderflowException, IllegalStateException
offset
- the location from where the long value is read.BufferUnderflowException
- if the offset is beyond the limits of the byte source.IllegalStateException
- if the byte source has been released.void nativeRead(long position, long address, long size) throws BufferUnderflowException, IllegalStateException
position
- the starting point within the byte source from which data is copied.address
- the destination address in native memory.size
- the number of bytes to transfer.BufferUnderflowException
- if the specified position or size exceeds the byte source limits.IllegalStateException
- if the byte source has been released.default int copyTo(byte[] bytes) throws BufferUnderflowException, IllegalStateException
RandomCommon.readPosition()
up to RandomCommon.readLimit()
.bytes
- the target byte array to which the data is copied.BufferUnderflowException
- if the source's read position or limit is beyond the byte source limits.IllegalStateException
- if the byte source has been released.default int copyTo(@NotNull @NotNull ByteBuffer bb) throws BufferUnderflowException, IllegalStateException
bb
- the target ByteBuffer to which the data is copied.BufferUnderflowException
- if the read operation encounters end of the byte source.IllegalStateException
- if the byte source has been released.default long readIncompleteLong(long offset) throws IllegalStateException
offset
- the location from where the long value is read.IllegalStateException
- if the byte source has been released.long realCapacity()
@Deprecated default int addAndGetInt(long offset, int adding) throws BufferUnderflowException, IllegalStateException
offset
- to add and getadding
- value to add, can be 1BufferUnderflowException
- if the offset is outside the limits of the BytesIllegalStateException
- if released@Deprecated default long addAndGetLong(long offset, long adding) throws BufferUnderflowException, IllegalStateException
offset
- to add and getadding
- value to add, can be 1BufferUnderflowException
- if the offset is outside the limits of the BytesIllegalStateException
- if released@Deprecated default float addAndGetFloat(long offset, float adding) throws BufferUnderflowException, IllegalStateException
offset
- to add and getadding
- value to add, can be 1BufferUnderflowException
- if the offset is outside the limits of the BytesIllegalStateException
- if released@Deprecated default double addAndGetDouble(long offset, double adding) throws BufferUnderflowException, IllegalStateException
offset
- to add and getadding
- value to add, can be 1BufferUnderflowException
- if the offset is outside the limits of the BytesIllegalStateException
- if released@Deprecated boolean compareAndSwapInt(long offset, int expected, int value) throws BufferOverflowException, IllegalStateException
offset
- to perform CASexpected
- valuevalue
- to setIllegalStateException
- if releasedBufferOverflowException
@Deprecated void testAndSetInt(long offset, int expected, int value) throws BufferOverflowException, IllegalStateException
@Deprecated boolean compareAndSwapLong(long offset, long expected, long value) throws BufferOverflowException, IllegalStateException
offset
- to perform CASexpected
- valuevalue
- to setIllegalStateException
- if releasedBufferOverflowException
@Deprecated default boolean compareAndSwapFloat(long offset, float expected, float value) throws BufferOverflowException, IllegalStateException
offset
- to perform CASexpected
- valuevalue
- to setIllegalStateException
- if releasedBufferOverflowException
@Deprecated default boolean compareAndSwapDouble(long offset, double expected, double value) throws BufferOverflowException, IllegalStateException
offset
- to perform CASexpected
- valuevalue
- to setIllegalStateException
- if releasedBufferOverflowException
@NotNull default @NotNull BytesStore subBytes(long start, long length) throws BufferUnderflowException, IllegalStateException
start
- the start index, inclusive.length
- the length of the subsequence.BufferUnderflowException
- if the start index or length are outside the limits of this byte sequence.IllegalStateException
- if the byte source has been released.default long findByte(byte stopByte) throws IllegalStateException
stopByte
- the byte to be searched for.IllegalStateException
- if the byte source has been released.default <T extends Appendable & CharSequence> long readUtf8(long offset, @NotNull T sb) throws net.openhft.chronicle.core.io.IORuntimeException, IllegalArgumentException, BufferUnderflowException, ArithmeticException, IllegalStateException
sb
(it must be a StringBuilder
or Bytes
) and reads a char
sequence from the given offset
, encoded as Utf8, into it. Returns offset after
the read Utf8, if a normal char sequence was read, or -1 - offset
, if null
was observed (in this case, sb
is truncated too, but not updated then, by querying
sb
only this case is indistinguishable from reading an empty char sequence).T
- buffer type, must be StringBuilder
or Bytes
offset
- the offset in this RandomDataInput
to read char sequence fromsb
- the buffer to read char sequence into (truncated first)null
net.openhft.chronicle.core.io.IORuntimeException
- if the reading operation encounters an unexpected error.IllegalArgumentException
- if the buffer is not a StringBuilder
or Bytes
.BufferUnderflowException
- if the reading operation encounters the end of the byte source.ArithmeticException
- if the calculated length of the UTF-8 encoded string is invalid.IllegalStateException
- if the byte source has been released.RandomDataOutput.writeUtf8(long, CharSequence)
default <T extends Appendable & CharSequence> long readUtf8Limited(long offset, @NotNull T sb, int maxUtf8Len) throws net.openhft.chronicle.core.io.IORuntimeException, IllegalArgumentException, BufferUnderflowException, IllegalStateException
sb
(it must be a StringBuilder
or Bytes
) and reads a char
sequence from the given offset
, encoded as Utf8, into it. Returns offset after
the read Utf8, if a normal char sequence was read, or -1 - offset
, if null
was observed (in this case, sb
is truncated too, but not updated then, by querying
sb
only this case is indistinguishable from reading an empty char sequence). If
length of Utf8 encoding of the char sequence exceeds maxUtf8Len
,
IllegalStateException
is thrown.T
- buffer type, must be StringBuilder
or Bytes
offset
- the offset in this RandomDataInput
to read char sequence fromsb
- the buffer to read char sequence into (truncated first)maxUtf8Len
- the maximum allowed length of the char sequence in Utf8 encodingnull
IllegalStateException
- if releasednet.openhft.chronicle.core.io.IORuntimeException
IllegalArgumentException
BufferUnderflowException
RandomDataOutput.writeUtf8Limited(long, CharSequence, int)
@Nullable default @Nullable String readUtf8Limited(long offset, int maxUtf8Len) throws BufferUnderflowException, net.openhft.chronicle.core.io.IORuntimeException, IllegalArgumentException, IllegalStateException
offset
, encoded as Utf8. If length of Utf8
encoding of the char sequence exceeds maxUtf8Len
, IllegalStateException
is thrown.offset
- the offset in this RandomDataInput
to read char sequence frommaxUtf8Len
- the maximum allowed length of the char sequence in Utf8 encodingIllegalStateException
- if releasedBufferUnderflowException
net.openhft.chronicle.core.io.IORuntimeException
IllegalArgumentException
RandomDataOutput.writeUtf8Limited(long, CharSequence, int)
default boolean compareUtf8(long offset, @Nullable @Nullable CharSequence other) throws net.openhft.chronicle.core.io.IORuntimeException, BufferUnderflowException, IllegalStateException
RandomDataInput
at the
given offset, with the given char sequence. Returns true
, if they are equal. Both
char sequences (encoded in bytes and the given) may be null
.offset
- the offset in this RandomDataInput
where the char sequence to compare
is writtenother
- the second char sequence to comparetrue
if two char sequences are equalIllegalStateException
- if releasednet.openhft.chronicle.core.io.IORuntimeException
- if the contents are not a valid string.BufferUnderflowException
default byte[] toByteArray() throws IllegalStateException
IllegalStateException
default long read(long offsetInRDI, byte[] bytes, int offset, int length) throws IllegalStateException
offsetInRDI
- the offset in the byte sequence from which to start reading.bytes
- the byte array into which the data is read.offset
- the start offset in the byte array at which the data is written.length
- the maximum number of bytes to read.IllegalStateException
- if the byte sequence has been released.default ByteBuffer toTemporaryDirectByteBuffer() throws IllegalArgumentException, ArithmeticException, IllegalStateException
IllegalArgumentException
- if the byte sequence cannot be converted into a ByteBuffer.ArithmeticException
- if the calculated size of the ByteBuffer is invalid.IllegalStateException
- if the byte sequence has been released.default int fastHash(long offset, int length) throws BufferUnderflowException, IllegalStateException
offset
- the start offset of the sequence of bytes.length
- the length of the sequence of bytes.BufferUnderflowException
- if the specified sequence of bytes extends beyond the limits of this byte sequence.IllegalStateException
- if the byte sequence has been released.default boolean canReadDirect()
default boolean canReadDirect(long length)
length
- the number of bytes to check.default long start()
default long capacity()
default long readPosition()
The read position is start() <= readPosition() && readPosition() <= readLimit() && readPosition < safeLimit()
If the resource is closed, the returned value is unspecified.
default long writePosition()
The write position is readPosition() <= writePosition() && writePosition() <= writeLimit()
If the resource is closed, the returned value is unspecified.
default long lengthWritten(long startPosition)
Typically this calculates the difference however for HexDumpBytes it's not as simple.
If the resource is closed, the returned value is unspecified.
startPosition
- The position to calculate the length from.default long readRemaining()
If the resource is closed, the returned value is unspecified.
default long realReadRemaining()
If the resource is closed, the returned value is unspecified.
default long writeRemaining()
If the resource is closed, the returned value is unspecified.
default long realWriteRemaining()
If the resource is closed, the returned value is unspecified.
default long readLimit()
If the resource is closed, the returned value is unspecified.
default long writeLimit()
If the resource is closed, the returned value is unspecified.
long addressForRead(long offset) throws UnsupportedOperationException, BufferUnderflowException, IllegalStateException
offset
- within this buffer. addressForRead(start()) is the actual addressForRead of the first byte.UnsupportedOperationException
- if the underlying buffer is on the heapBufferUnderflowException
- if the offset is before the start() or the after the capacity()IllegalStateException
- if the buffer has been closed.default long addressForRead(long offset, int buffer) throws UnsupportedOperationException, BufferUnderflowException, IllegalStateException
offset
- The offset within this buffer.buffer
- The buffer index.UnsupportedOperationException
- if the underlying buffer is on the heap.BufferUnderflowException
- if the offset is before the start or after the capacity.IllegalStateException
- if the buffer has been closed.long addressForWrite(long offset) throws UnsupportedOperationException, BufferOverflowException, IllegalStateException
offset
- within this buffer. addressForRead(start()) is the actual addressForRead of the first byte.UnsupportedOperationException
- if the underlying buffer is on the heapBufferOverflowException
- if the offset is before the start() or the after the capacity()IllegalStateException
long addressForWritePosition() throws UnsupportedOperationException, BufferOverflowException, IllegalStateException
UnsupportedOperationException
- if the underlying buffer is on the heap.BufferOverflowException
- if the current write position is before the start or after the capacity.IllegalStateException
- if the buffer state doesn't allow the operation.default ByteOrder byteOrder()
@NotNull @NotNull Bytes<?> bytesForRead() throws IllegalStateException
IllegalStateException
- if the buffer state doesn't allow the operation.@NotNull @NotNull Bytes<?> bytesForWrite() throws IllegalStateException
IllegalStateException
- if the buffer state doesn't allow the operation.boolean sharedMemory()
boolean isDirectMemory()
Copyright © 2023. All rights reserved.