Class ByteBufferInputStream

  • All Implemented Interfaces:
    Closeable, AutoCloseable
    Direct Known Subclasses:
    VarlenaWrapper.Input.Stream

    public abstract class ByteBufferInputStream
    extends InputStream
    Wrap a readable ByteBuffer as an InputStream.

    An implementing class must provide a buffer() method that returns the ByteBuffer, and the method is responsible for knowing when the memory region windowed by the ByteBuffer is no longer to be accessed, and throwing an exception in that case.

    The implementing class may supply an object that the InputStream operations will be synchronized on.

    The underlying buffer's position and mark are used to maintain the corresponding values for the input stream.

    • Field Detail

      • m_lock

        protected final Object m_lock
        The object on which the InputStream operations will synchronize.
      • m_open

        protected boolean m_open
        Whether this stream is open; initially true.
    • Constructor Detail

      • ByteBufferInputStream

        protected ByteBufferInputStream()
        Construct an instance whose critical sections will synchronize on the instance itself.
      • ByteBufferInputStream

        protected ByteBufferInputStream​(Object lock)
        Construct an instance, given an object on which to synchronize.
        Parameters:
        lock - The Object to synchronize on.
    • Method Detail

      • pin

        protected void pin()
                    throws IOException
        Pin resources if necessary during a reading operation.

        This default implementation does nothing. A subclass should override it if (in addition to synchronizing on m_lock), some pinning of a resource is needed during access operations.

        Throws:
        IOException
      • unpin

        protected void unpin()
        Unpin resources if necessary after a reading operation.

        This default implementation does nothing.

      • buffer

        protected abstract ByteBuffer buffer()
                                      throws IOException
        Return the ByteBuffer being wrapped, or throw an exception if the memory windowed by the buffer should no longer be accessed.

        The monitor on m_lock is held when this method is called.

        This method also should throw an exception if m_open is false. It is called everywhere that should happen, so it is the perfect place for the test, and allows the implementing class to use a customized message in the exception.

        All uses of the buffer in this class are preceded by pin() and followed by unpin() (whose default implementations in this class do nothing). If a subclass overrides pin with a version that throws the appropriate exception in either case or both, it is then redundant and unnecessary for buffer to check the same conditions.

        Throws:
        IOException
      • mark

        public void mark​(int readlimit)
        Overrides:
        mark in class InputStream
      • markSupported

        public boolean markSupported()
        Return true; this class does support mark and reset.
        Overrides:
        markSupported in class InputStream