Class ByteBufInputStream

java.lang.Object
java.io.InputStream
io.netty.buffer.ByteBufInputStream
All Implemented Interfaces:
java.io.Closeable, java.io.DataInput, java.lang.AutoCloseable

public class ByteBufInputStream extends java.io.InputStream implements java.io.DataInput
An InputStream which reads data from a ByteBuf.

A read operation against this stream will occur at the readerIndex of its underlying buffer and the readerIndex will increase during the read operation. Please note that it only reads up to the number of readable bytes determined at the moment of construction. Therefore, updating ByteBuf.writerIndex() will not affect the return value of available().

This stream implements DataInput for your convenience. The endianness of the stream is not always big endian but depends on the endianness of the underlying buffer.

See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final ByteBuf
     
    private boolean
     
    private final int
     
    private java.lang.StringBuilder
     
    private final boolean
    To preserve backwards compatibility (which didn't transfer ownership) we support a conditional flag which indicates if buffer should be released when this InputStream is closed.
    private final int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new stream which reads data from the specified buffer starting at the current readerIndex and ending at the current writerIndex.
    ByteBufInputStream(ByteBuf buffer, boolean releaseOnClose)
    Creates a new stream which reads data from the specified buffer starting at the current readerIndex and ending at the current writerIndex.
    ByteBufInputStream(ByteBuf buffer, int length)
    Creates a new stream which reads data from the specified buffer starting at the current readerIndex and ending at readerIndex + length.
    ByteBufInputStream(ByteBuf buffer, int length, boolean releaseOnClose)
    Creates a new stream which reads data from the specified buffer starting at the current readerIndex and ending at readerIndex + length.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
     
    private void
    checkAvailable(int fieldSize)
     
    void
     
    void
    mark(int readlimit)
     
    boolean
     
    int
     
    int
    read(byte[] b, int off, int len)
     
    boolean
     
    byte
     
    int
    Returns the number of read bytes by this stream so far.
    char
     
    double
     
    float
     
    void
    readFully(byte[] b)
     
    void
    readFully(byte[] b, int off, int len)
     
    int
     
    java.lang.String
     
    long
     
    short
     
    int
     
    int
     
    java.lang.String
     
    void
     
    long
    skip(long n)
     
    int
    skipBytes(int n)
     

    Methods inherited from class java.io.InputStream

    read

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • buffer

      private final ByteBuf buffer
    • startIndex

      private final int startIndex
    • endIndex

      private final int endIndex
    • closed

      private boolean closed
    • releaseOnClose

      private final boolean releaseOnClose
      To preserve backwards compatibility (which didn't transfer ownership) we support a conditional flag which indicates if buffer should be released when this InputStream is closed. However in future releases ownership should always be transferred and callers of this class should call ReferenceCounted.retain() if necessary.
    • lineBuf

      private java.lang.StringBuilder lineBuf
  • Constructor Details

    • ByteBufInputStream

      public ByteBufInputStream(ByteBuf buffer)
      Creates a new stream which reads data from the specified buffer starting at the current readerIndex and ending at the current writerIndex.
      Parameters:
      buffer - The buffer which provides the content for this InputStream.
    • ByteBufInputStream

      public ByteBufInputStream(ByteBuf buffer, int length)
      Creates a new stream which reads data from the specified buffer starting at the current readerIndex and ending at readerIndex + length.
      Parameters:
      buffer - The buffer which provides the content for this InputStream.
      length - The length of the buffer to use for this InputStream.
      Throws:
      java.lang.IndexOutOfBoundsException - if readerIndex + length is greater than writerIndex
    • ByteBufInputStream

      public ByteBufInputStream(ByteBuf buffer, boolean releaseOnClose)
      Creates a new stream which reads data from the specified buffer starting at the current readerIndex and ending at the current writerIndex.
      Parameters:
      buffer - The buffer which provides the content for this InputStream.
      releaseOnClose - true means that when close() is called then ReferenceCounted.release() will be called on buffer.
    • ByteBufInputStream

      public ByteBufInputStream(ByteBuf buffer, int length, boolean releaseOnClose)
      Creates a new stream which reads data from the specified buffer starting at the current readerIndex and ending at readerIndex + length.
      Parameters:
      buffer - The buffer which provides the content for this InputStream.
      length - The length of the buffer to use for this InputStream.
      releaseOnClose - true means that when close() is called then ReferenceCounted.release() will be called on buffer.
      Throws:
      java.lang.IndexOutOfBoundsException - if readerIndex + length is greater than writerIndex
  • Method Details

    • readBytes

      public int readBytes()
      Returns the number of read bytes by this stream so far.
    • close

      public void close() throws java.io.IOException
      Specified by:
      close in interface java.lang.AutoCloseable
      Specified by:
      close in interface java.io.Closeable
      Overrides:
      close in class java.io.InputStream
      Throws:
      java.io.IOException
    • available

      public int available() throws java.io.IOException
      Overrides:
      available in class java.io.InputStream
      Throws:
      java.io.IOException
    • mark

      public void mark(int readlimit)
      Overrides:
      mark in class java.io.InputStream
    • markSupported

      public boolean markSupported()
      Overrides:
      markSupported in class java.io.InputStream
    • read

      public int read() throws java.io.IOException
      Specified by:
      read in class java.io.InputStream
      Throws:
      java.io.IOException
    • read

      public int read(byte[] b, int off, int len) throws java.io.IOException
      Overrides:
      read in class java.io.InputStream
      Throws:
      java.io.IOException
    • reset

      public void reset() throws java.io.IOException
      Overrides:
      reset in class java.io.InputStream
      Throws:
      java.io.IOException
    • skip

      public long skip(long n) throws java.io.IOException
      Overrides:
      skip in class java.io.InputStream
      Throws:
      java.io.IOException
    • readBoolean

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

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

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

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

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

      public void readFully(byte[] b) throws java.io.IOException
      Specified by:
      readFully in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readFully

      public void readFully(byte[] b, int off, int len) throws java.io.IOException
      Specified by:
      readFully in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readInt

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

      public java.lang.String readLine() throws java.io.IOException
      Specified by:
      readLine in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readLong

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

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

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

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

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

      public int skipBytes(int n) throws java.io.IOException
      Specified by:
      skipBytes in interface java.io.DataInput
      Throws:
      java.io.IOException
    • checkAvailable

      private void checkAvailable(int fieldSize) throws java.io.IOException
      Throws:
      java.io.IOException