Package org.agrona.io
Class DirectBufferDataInput
java.lang.Object
org.agrona.io.DirectBufferDataInput
- All Implemented Interfaces:
DataInput
A data input implementation that reads from a DirectBuffer. It adheres to the contract defined in
DataInput
description including throwing checked exception on end of file. It adds few more methods to read strings without
allocations.
Note about byte ordering: by default, this class conforms to DataInput
contract and uses
ByteOrder.BIG_ENDIAN
byte order which allows it to read data produced by JDK DataOutput
implementations. Agrona buffers use ByteOrder.LITTLE_ENDIAN
(unless overridden). Use
byteOrder(ByteOrder)
method to switch between JDK and Agrona compatibility.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionDirectBufferDataInput
(DirectBuffer buffer) Wrap givenDirectBuffer
.DirectBufferDataInput
(DirectBuffer buffer, int offset, int length) Wrap givenDirectBuffer
. -
Method Summary
Modifier and TypeMethodDescriptionprivate void
boundsCheck0
(int requestedReadBytes) private static void
boundsCheckWrap
(int offset, int length, int capacity) void
Sets the byte order.boolean
byte
readByte()
char
readChar()
double
float
void
readFully
(byte[] destination) void
readFully
(byte[] destination, int destinationOffset, int length) int
readInt()
readLine()
int
readLine
(Appendable appendable) Reads the next line of text from the input stream.long
readLong()
short
Reads in a string that has been encoded using ASCII format byMutableDirectBuffer.putStringAscii(int, CharSequence)
.int
readStringAscii
(Appendable appendable) Get a String from bytes encoded in ASCII format that is length prefixed and append to anAppendable
.Reads in a string that has been encoded using UTF-8 format byMutableDirectBuffer.putStringUtf8(int, String)
.int
int
readUTF()
int
Return the number of bytes remaining in the buffer.int
skipBytes
(int n) void
wrap
(DirectBuffer buffer) Wrap givenDirectBuffer
.void
wrap
(DirectBuffer buffer, int offset, int length) Wrap givenDirectBuffer
.
-
Field Details
-
buffer
-
byteOrder
-
length
private int length -
position
private int position
-
-
Constructor Details
-
DirectBufferDataInput
Wrap givenDirectBuffer
.- Parameters:
buffer
- to wrap.
-
DirectBufferDataInput
Wrap givenDirectBuffer
.- Parameters:
buffer
- to wrap.offset
- into the buffer.length
- in bytes.
-
-
Method Details
-
wrap
Wrap givenDirectBuffer
.- Parameters:
buffer
- to wrap.
-
wrap
Wrap givenDirectBuffer
.- Parameters:
buffer
- to wrap.offset
- into the buffer.length
- in bytes.
-
byteOrder
Sets the byte order. By default, this class conforms toDataInput
contract and usesByteOrder.BIG_ENDIAN
which allows it to read data produced by JDKDataOutput
implementations. Agrona buffers useByteOrder.LITTLE_ENDIAN
(unless overridden). Use this method to switch compatibility between these two worlds.- Parameters:
byteOrder
- of the underlying buffer.
-
remaining
public int remaining()Return the number of bytes remaining in the buffer.- Returns:
- the number of bytes remaining.
-
readFully
- Specified by:
readFully
in interfaceDataInput
- Throws:
EOFException
-
readFully
- Specified by:
readFully
in interfaceDataInput
- Throws:
EOFException
-
skipBytes
public int skipBytes(int n) -
readBoolean
- Specified by:
readBoolean
in interfaceDataInput
- Throws:
EOFException
-
readByte
- Specified by:
readByte
in interfaceDataInput
- Throws:
EOFException
-
readUnsignedByte
- Specified by:
readUnsignedByte
in interfaceDataInput
- Throws:
EOFException
-
readShort
- Specified by:
readShort
in interfaceDataInput
- Throws:
EOFException
-
readUnsignedShort
- Specified by:
readUnsignedShort
in interfaceDataInput
- Throws:
EOFException
-
readChar
- Specified by:
readChar
in interfaceDataInput
- Throws:
EOFException
-
readInt
- Specified by:
readInt
in interfaceDataInput
- Throws:
EOFException
-
readLong
- Specified by:
readLong
in interfaceDataInput
- Throws:
EOFException
-
readFloat
- Specified by:
readFloat
in interfaceDataInput
- Throws:
EOFException
-
readDouble
- Specified by:
readDouble
in interfaceDataInput
- Throws:
EOFException
-
readLine
- Specified by:
readLine
in interfaceDataInput
- Throws:
IOException
-
readLine
Reads the next line of text from the input stream. It reads successive bytes, converting each byte separately into a character, until it encounters a line terminator or end of file; Adheres to the contract ofDataInput.readLine()
.- Parameters:
appendable
- to append the chars to.- Returns:
- number of bytes the stream advanced while reading the line (including line terminators).
- Throws:
IOException
- propagated fromAppendable.append(char)
.
-
readUTF
- Specified by:
readUTF
in interfaceDataInput
- Throws:
EOFException
-
readStringUTF8
Reads in a string that has been encoded using UTF-8 format byMutableDirectBuffer.putStringUtf8(int, String)
.This is a thin wrapper over
DirectBuffer.getStringUtf8(int, ByteOrder)
. Honours byte order set bybyteOrder(ByteOrder)
.- Returns:
- the String as represented by the ASCII encoded bytes.
-
readStringAscii
Reads in a string that has been encoded using ASCII format byMutableDirectBuffer.putStringAscii(int, CharSequence)
.This is a thin wrapper over
DirectBuffer.getStringAscii(int, ByteOrder)
. Honours byte order set bybyteOrder(ByteOrder)
- Returns:
- the String as represented by the ASCII encoded bytes.
-
readStringAscii
Get a String from bytes encoded in ASCII format that is length prefixed and append to anAppendable
. This is a thin wrapper overDirectBuffer.getStringAscii(int, Appendable, ByteOrder)
. Honours byte order set bybyteOrder(ByteOrder)
.- Parameters:
appendable
- to append the chars to.- Returns:
- the number of bytes copied.
-
boundsCheck0
- Throws:
EOFException
-
boundsCheckWrap
private static void boundsCheckWrap(int offset, int length, int capacity)
-