Class ByteBufferDataInputStream

java.lang.Object
net.okapia.osid.torrefacto.transport.ByteBufferDataInputStream
All Implemented Interfaces:
Closeable, AutoCloseable, DataInputStream

public class ByteBufferDataInputStream extends Object implements DataInputStream
A DataInputStream backed by a Java ByteBuffer. The stream consumes the buffer it is given, advancing the buffer's position as bytes are read or skipped; the caller hands off ownership and should not use the buffer afterward. This class is not thread-safe.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new ByteBufferDataInputStream.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Tests if the end of this stream has been reached.
    long
    Gets the number of bytes available for retrieval.
    void
    Closes this stream and frees up any allocated resources.
    long
    read(byte[] buf, long n)
    Reads a specified number of bytes from this stream.
    long
    skip(long n)
    Skips a specified number of bytes in the stream.

    Methods inherited from class Object

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

    • ByteBufferDataInputStream

      public ByteBufferDataInputStream(ByteBuffer buffer)
      Constructs a new ByteBufferDataInputStream. The buffer must already be flipped and ready for reading; the stream takes ownership of it.
      Parameters:
      buffer - a flipped byte buffer
      Throws:
      NullArgumentException - buffer is null
  • Method Details

    • atEndOfStream

      @OSID public boolean atEndOfStream()
      Tests if the end of this stream has been reached. This may not be a permanent condition as more data may be available at a later time as in the case of tailing a file.
      Specified by:
      atEndOfStream in interface DataInputStream
      Returns:
      true if the end of this stream has been reached, false otherwise
      Throws:
      IllegalStateException - this stream has been closed
    • available

      @OSID public long available()
      Gets the number of bytes available for retrieval. The number returned by this method may be less than or equal to the total number of bytes in this stream.
      Specified by:
      available in interface DataInputStream
      Returns:
      the number of bytes available for retrieval
      Throws:
      IllegalStateException - this stream has been closed
    • skip

      @OSID public long skip(long n)
      Skips a specified number of bytes in the stream. A non-positive count skips nothing and returns zero.
      Specified by:
      skip in interface DataInputStream
      Parameters:
      n - the number of bytes to skip
      Returns:
      the actual number of bytes skipped
      Throws:
      IllegalStateException - this stream has been closed or atEndOfStream() is true
    • read

      @OSID public long read(byte[] buf, long n) throws OperationFailedException
      Reads a specified number of bytes from this stream. A non-positive count reads nothing and returns zero.
      Specified by:
      read in interface DataInputStream
      Parameters:
      buf - the buffer in which the data is read
      n - the number of bytes to read
      Returns:
      the actual number of bytes read
      Throws:
      IllegalStateException - this stream has been closed or atEndOfStream() is true
      InvalidArgumentException - the size of buf is less than n
      NullArgumentException - buf is null
      OperationFailedException - unable to complete request
    • close

      @OSID public void close()
      Closes this stream and frees up any allocated resources. Methods in this object may not be invoked after this method is called.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface DataInputStream
      Throws:
      IllegalStateException - this stream has been closed