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.
-
Constructor Summary
ConstructorsConstructorDescriptionDirectBufferDataInput(DirectBuffer buffer) Wrap givenDirectBuffer.DirectBufferDataInput(DirectBuffer buffer, int offset, int length) Wrap givenDirectBuffer. -
Method Summary
Modifier and TypeMethodDescriptionvoidSets the byte order.booleanbytereadByte()charreadChar()doublefloatvoidreadFully(byte[] destination) voidreadFully(byte[] destination, int destinationOffset, int length) intreadInt()readLine()intreadLine(Appendable appendable) Reads the next line of text from the input stream.longreadLong()shortReads in a string that has been encoded using ASCII format byMutableDirectBuffer.putStringAscii(int, CharSequence).intreadStringAscii(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).intintreadUTF()intReturn the number of bytes remaining in the buffer.intskipBytes(int n) voidwrap(DirectBuffer buffer) Wrap givenDirectBuffer.voidwrap(DirectBuffer buffer, int offset, int length) Wrap givenDirectBuffer.
-
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 toDataInputcontract and usesByteOrder.BIG_ENDIANwhich allows it to read data produced by JDKDataOutputimplementations. 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:
readFullyin interfaceDataInput- Throws:
EOFException
-
readFully
- Specified by:
readFullyin interfaceDataInput- Throws:
EOFException
-
skipBytes
public int skipBytes(int n) -
readBoolean
- Specified by:
readBooleanin interfaceDataInput- Throws:
EOFException
-
readByte
- Specified by:
readBytein interfaceDataInput- Throws:
EOFException
-
readUnsignedByte
- Specified by:
readUnsignedBytein interfaceDataInput- Throws:
EOFException
-
readShort
- Specified by:
readShortin interfaceDataInput- Throws:
EOFException
-
readUnsignedShort
- Specified by:
readUnsignedShortin interfaceDataInput- Throws:
EOFException
-
readChar
- Specified by:
readCharin interfaceDataInput- Throws:
EOFException
-
readInt
- Specified by:
readIntin interfaceDataInput- Throws:
EOFException
-
readLong
- Specified by:
readLongin interfaceDataInput- Throws:
EOFException
-
readFloat
- Specified by:
readFloatin interfaceDataInput- Throws:
EOFException
-
readDouble
- Specified by:
readDoublein interfaceDataInput- Throws:
EOFException
-
readLine
- Specified by:
readLinein 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:
readUTFin 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.
-