Class NonBlockingInputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable
    Direct Known Subclasses:
    BodyInputStream, ByteBufferInputStream

    public abstract class NonBlockingInputStream
    extends java.io.InputStream
    An abstract InputStream extension that defines contract for non-blocking streaming read operations.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int NOTHING
      Constant used as a return value from tryRead() method, to indicate that nothing has been read.
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      int available()
      Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.
      abstract int tryRead()
      Behaves mostly like InputStream.read().
      abstract int tryRead​(byte[] b)
      Behaves mostly like InputStream.read(byte[]).
      abstract int tryRead​(byte[] b, int off, int len)
      Behaves mostly like InputStream.read(byte[], int, int).
      • Methods inherited from class java.io.InputStream

        close, mark, markSupported, nullInputStream, read, read, read, readAllBytes, readNBytes, readNBytes, reset, skip, transferTo
      • Methods inherited from class java.lang.Object

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

      • NOTHING

        public static final int NOTHING
        Constant used as a return value from tryRead() method, to indicate that nothing has been read.
        See Also:
        Constant Field Values
    • Constructor Detail

      • NonBlockingInputStream

        public NonBlockingInputStream()
    • Method Detail

      • available

        public int available()
                      throws java.io.IOException
        Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.

        Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.

        A subclass' implementation of this method may choose to throw an IOException if this input stream has been closed by invoking the InputStream.close() method.

        The default implementation of this method in NonBlockingInputStream throws an UnsupportedOperationException. This method must be overridden by subclasses. The overriding implementations must guarantee non-blocking behavior of the method. The overriding implementation must also guarantee that a non-empty stream does not return zero from the method. IOW, it must be possible to use the method for empty check: stream.available() == 0

        Overrides:
        available in class java.io.InputStream
        Returns:
        an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking or 0 when it reaches the end of the input stream or the stream is empty.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • tryRead

        public abstract int tryRead()
                             throws java.io.IOException
        Behaves mostly like InputStream.read(). The main difference is that this method is non-blocking. In case there are no data available to be read, the method returns NOTHING immediately.
        Returns:
        next byte of data, -1 if end of the stream has been reached or NOTHING in case no data are available to be read at the moment.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • tryRead

        public abstract int tryRead​(byte[] b)
                             throws java.io.IOException
        Behaves mostly like InputStream.read(byte[]). The main difference is that this method is non-blocking. In case there are no data available to be read, the method returns zero immediately.
        Parameters:
        b - the buffer into which the data is read.
        Returns:
        the total number of bytes read into the buffer or -1 if end of the stream has been reached or 0 in case no data are available to be read at the moment.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • tryRead

        public abstract int tryRead​(byte[] b,
                                    int off,
                                    int len)
                             throws java.io.IOException
        Behaves mostly like InputStream.read(byte[], int, int). The main difference is that this method is non-blocking. In case there are no data available to be read, the method returns zero immediately.
        Parameters:
        b - the buffer into which the data is read.
        off - the start offset in array b at which the data is written.
        len - the maximum number of bytes to read.
        Returns:
        the total number of bytes read into the buffer or -1 if end of the stream has been reached or 0 in case no data are available to be read at the moment.
        Throws:
        java.io.IOException - if an I/O error occurs.