Class ByteBufferInputStream

java.lang.Object
java.io.InputStream
org.postgresql.pljava.internal.ByteBufferInputStream
All Implemented Interfaces:
Closeable, AutoCloseable
Direct Known Subclasses:
DatumImpl.IStream

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 Details

    • 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 Details

    • 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 Details

    • 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
    • read

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

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

      public long skip(long n) throws IOException
      Overrides:
      skip in class InputStream
      Throws:
      IOException
    • available

      public int available() throws IOException
      Overrides:
      available in class InputStream
      Throws:
      IOException
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class InputStream
      Throws:
      IOException
    • mark

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

      public void reset() throws IOException
      Overrides:
      reset in class InputStream
      Throws:
      IOException
    • markSupported

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