Package org.agrona.io
Class DirectBufferDataInput
- java.lang.Object
-
- org.agrona.io.DirectBufferDataInput
-
- All Implemented Interfaces:
java.io.DataInput
public class DirectBufferDataInput extends java.lang.Object implements java.io.DataInput
A data input implementation that reads from a DirectBuffer. It adheres to the contract defined inDataInput
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 usesByteOrder.BIG_ENDIAN
byte order which allows it to read data produced by JDKDataOutput
implementations. Agrona buffers useByteOrder.LITTLE_ENDIAN
(unless overridden). UsebyteOrder(ByteOrder)
method to switch between JDK and Agrona compatibility.
-
-
Field Summary
Fields Modifier and Type Field Description private DirectBuffer
buffer
private java.nio.ByteOrder
byteOrder
private int
length
private int
position
-
Constructor Summary
Constructors Constructor Description DirectBufferDataInput(DirectBuffer buffer)
Wrap givenDirectBuffer
.DirectBufferDataInput(DirectBuffer buffer, int offset, int length)
Wrap givenDirectBuffer
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
boundsCheck0(int requestedReadBytes)
private static void
boundsCheckWrap(int offset, int length, int capacity)
void
byteOrder(java.nio.ByteOrder byteOrder)
Sets the byte order.boolean
readBoolean()
byte
readByte()
char
readChar()
double
readDouble()
float
readFloat()
void
readFully(byte[] destination)
void
readFully(byte[] destination, int destinationOffset, int length)
int
readInt()
java.lang.String
readLine()
int
readLine(java.lang.Appendable appendable)
Reads the next line of text from the input stream.long
readLong()
short
readShort()
java.lang.String
readStringAscii()
Reads in a string that has been encoded using ASCII format byMutableDirectBuffer.putStringAscii(int, CharSequence)
.int
readStringAscii(java.lang.Appendable appendable)
Get a String from bytes encoded in ASCII format that is length prefixed and append to anAppendable
.java.lang.String
readStringUTF8()
Reads in a string that has been encoded using UTF-8 format byMutableDirectBuffer.putStringUtf8(int, String)
.int
readUnsignedByte()
int
readUnsignedShort()
java.lang.String
readUTF()
int
remaining()
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 Detail
-
buffer
private DirectBuffer buffer
-
byteOrder
private java.nio.ByteOrder byteOrder
-
length
private int length
-
position
private int position
-
-
Constructor Detail
-
DirectBufferDataInput
public DirectBufferDataInput(DirectBuffer buffer)
Wrap givenDirectBuffer
.- Parameters:
buffer
- to wrap.
-
DirectBufferDataInput
public DirectBufferDataInput(DirectBuffer buffer, int offset, int length)
Wrap givenDirectBuffer
.- Parameters:
buffer
- to wrap.offset
- into the buffer.length
- in bytes.
-
-
Method Detail
-
wrap
public void wrap(DirectBuffer buffer)
Wrap givenDirectBuffer
.- Parameters:
buffer
- to wrap.
-
wrap
public void wrap(DirectBuffer buffer, int offset, int length)
Wrap givenDirectBuffer
.- Parameters:
buffer
- to wrap.offset
- into the buffer.length
- in bytes.
-
byteOrder
public void byteOrder(java.nio.ByteOrder 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
public void readFully(byte[] destination) throws java.io.EOFException
- Specified by:
readFully
in interfacejava.io.DataInput
- Throws:
java.io.EOFException
-
readFully
public void readFully(byte[] destination, int destinationOffset, int length) throws java.io.EOFException
- Specified by:
readFully
in interfacejava.io.DataInput
- Throws:
java.io.EOFException
-
skipBytes
public int skipBytes(int n)
- Specified by:
skipBytes
in interfacejava.io.DataInput
-
readBoolean
public boolean readBoolean() throws java.io.EOFException
- Specified by:
readBoolean
in interfacejava.io.DataInput
- Throws:
java.io.EOFException
-
readByte
public byte readByte() throws java.io.EOFException
- Specified by:
readByte
in interfacejava.io.DataInput
- Throws:
java.io.EOFException
-
readUnsignedByte
public int readUnsignedByte() throws java.io.EOFException
- Specified by:
readUnsignedByte
in interfacejava.io.DataInput
- Throws:
java.io.EOFException
-
readShort
public short readShort() throws java.io.EOFException
- Specified by:
readShort
in interfacejava.io.DataInput
- Throws:
java.io.EOFException
-
readUnsignedShort
public int readUnsignedShort() throws java.io.EOFException
- Specified by:
readUnsignedShort
in interfacejava.io.DataInput
- Throws:
java.io.EOFException
-
readChar
public char readChar() throws java.io.EOFException
- Specified by:
readChar
in interfacejava.io.DataInput
- Throws:
java.io.EOFException
-
readInt
public int readInt() throws java.io.EOFException
- Specified by:
readInt
in interfacejava.io.DataInput
- Throws:
java.io.EOFException
-
readLong
public long readLong() throws java.io.EOFException
- Specified by:
readLong
in interfacejava.io.DataInput
- Throws:
java.io.EOFException
-
readFloat
public float readFloat() throws java.io.EOFException
- Specified by:
readFloat
in interfacejava.io.DataInput
- Throws:
java.io.EOFException
-
readDouble
public double readDouble() throws java.io.EOFException
- Specified by:
readDouble
in interfacejava.io.DataInput
- Throws:
java.io.EOFException
-
readLine
public java.lang.String readLine() throws java.io.IOException
- Specified by:
readLine
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readLine
public int readLine(java.lang.Appendable appendable) throws java.io.IOException
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:
java.io.IOException
- propagated fromAppendable.append(char)
.
-
readUTF
public java.lang.String readUTF() throws java.io.EOFException
- Specified by:
readUTF
in interfacejava.io.DataInput
- Throws:
java.io.EOFException
-
readStringUTF8
public java.lang.String 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
public java.lang.String 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
public int readStringAscii(java.lang.Appendable appendable)
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
private void boundsCheck0(int requestedReadBytes) throws java.io.EOFException
- Throws:
java.io.EOFException
-
boundsCheckWrap
private static void boundsCheckWrap(int offset, int length, int capacity)
-
-