Package org.agrona.io

Class 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 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 Detail

      • byteOrder

        private java.nio.ByteOrder byteOrder
      • length

        private int length
      • position

        private int position
    • Constructor Detail

      • DirectBufferDataInput

        public DirectBufferDataInput​(DirectBuffer buffer)
        Wrap given DirectBuffer.
        Parameters:
        buffer - to wrap.
      • DirectBufferDataInput

        public DirectBufferDataInput​(DirectBuffer buffer,
                                     int offset,
                                     int length)
        Wrap given DirectBuffer.
        Parameters:
        buffer - to wrap.
        offset - into the buffer.
        length - in bytes.
    • Method Detail

      • wrap

        public void wrap​(DirectBuffer buffer,
                         int offset,
                         int length)
        Wrap given DirectBuffer.
        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 to DataInput contract and uses ByteOrder.BIG_ENDIAN which allows it to read data produced by JDK DataOutput implementations. Agrona buffers use ByteOrder.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 interface java.io.DataInput
        Throws:
        java.io.EOFException
      • readFully

        public void readFully​(byte[] destination,
                              int destinationOffset,
                              int length)
                       throws java.io.EOFException
        Specified by:
        readFully in interface java.io.DataInput
        Throws:
        java.io.EOFException
      • skipBytes

        public int skipBytes​(int n)
        Specified by:
        skipBytes in interface java.io.DataInput
      • readBoolean

        public boolean readBoolean()
                            throws java.io.EOFException
        Specified by:
        readBoolean in interface java.io.DataInput
        Throws:
        java.io.EOFException
      • readByte

        public byte readByte()
                      throws java.io.EOFException
        Specified by:
        readByte in interface java.io.DataInput
        Throws:
        java.io.EOFException
      • readUnsignedByte

        public int readUnsignedByte()
                             throws java.io.EOFException
        Specified by:
        readUnsignedByte in interface java.io.DataInput
        Throws:
        java.io.EOFException
      • readShort

        public short readShort()
                        throws java.io.EOFException
        Specified by:
        readShort in interface java.io.DataInput
        Throws:
        java.io.EOFException
      • readUnsignedShort

        public int readUnsignedShort()
                              throws java.io.EOFException
        Specified by:
        readUnsignedShort in interface java.io.DataInput
        Throws:
        java.io.EOFException
      • readChar

        public char readChar()
                      throws java.io.EOFException
        Specified by:
        readChar in interface java.io.DataInput
        Throws:
        java.io.EOFException
      • readInt

        public int readInt()
                    throws java.io.EOFException
        Specified by:
        readInt in interface java.io.DataInput
        Throws:
        java.io.EOFException
      • readLong

        public long readLong()
                      throws java.io.EOFException
        Specified by:
        readLong in interface java.io.DataInput
        Throws:
        java.io.EOFException
      • readFloat

        public float readFloat()
                        throws java.io.EOFException
        Specified by:
        readFloat in interface java.io.DataInput
        Throws:
        java.io.EOFException
      • readDouble

        public double readDouble()
                          throws java.io.EOFException
        Specified by:
        readDouble in interface java.io.DataInput
        Throws:
        java.io.EOFException
      • readLine

        public java.lang.String readLine()
                                  throws java.io.IOException
        Specified by:
        readLine in interface java.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 of DataInput.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 from Appendable.append(char).
      • readUTF

        public java.lang.String readUTF()
                                 throws java.io.EOFException
        Specified by:
        readUTF in interface java.io.DataInput
        Throws:
        java.io.EOFException
      • readStringAscii

        public int readStringAscii​(java.lang.Appendable appendable)
        Get a String from bytes encoded in ASCII format that is length prefixed and append to an Appendable. This is a thin wrapper over DirectBuffer.getStringAscii(int, Appendable, ByteOrder). Honours byte order set by byteOrder(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)