Class BlobInputStream

java.lang.Object
java.io.InputStream
org.postgresql.largeobject.BlobInputStream
All Implemented Interfaces:
Closeable, AutoCloseable

public class BlobInputStream extends InputStream
This is an implementation of an InputStream from a large object.
  • Field Details

    • DEFAULT_MAX_BUFFER_SIZE

      static final int DEFAULT_MAX_BUFFER_SIZE
      See Also:
    • INITIAL_BUFFER_SIZE

      static final int INITIAL_BUFFER_SIZE
      See Also:
    • lo

      private LargeObject lo
      The parent LargeObject.
    • lock

      private final ResourceLock lock
    • absolutePosition

      private long absolutePosition
      The absolute position.
    • buffer

      private byte[] buffer
      Buffer used to improve performance.
    • bufferPosition

      private int bufferPosition
      Position within buffer.
    • lastBufferSize

      private int lastBufferSize
      The amount of bytes to read on the next read. Currently, we nullify buffer, so we can't use buffer.length.
    • maxBufferSize

      private final int maxBufferSize
      The buffer size.
    • markPosition

      private long markPosition
      The mark position.
    • limit

      private final long limit
      The limit.
  • Constructor Details

    • BlobInputStream

      public BlobInputStream(LargeObject lo)
      Parameters:
      lo - LargeObject to read from
    • BlobInputStream

      public BlobInputStream(LargeObject lo, int bsize)
      Parameters:
      lo - LargeObject to read from
      bsize - buffer size
    • BlobInputStream

      public BlobInputStream(LargeObject lo, int bsize, long limit)
      Parameters:
      lo - LargeObject to read from
      bsize - buffer size
      limit - max number of bytes to read
  • Method Details

    • read

      public int read() throws IOException
      The minimum required to implement input stream.
      Specified by:
      read in class InputStream
      Throws:
      IOException
    • getNextBufferSize

      private int getNextBufferSize(int len)
      Computes the next buffer size to use for reading data from the large object. The idea is to avoid allocating too much memory, especially if the user will use just a few bytes of the data.
      Parameters:
      len - estimated read request
      Returns:
      next buffer size or maxBufferSize if the buffer should not be increased
    • read

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

      public void close() throws IOException
      Closes this input stream and releases any system resources associated with the stream.

      The close method of InputStream does nothing.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class InputStream
      Throws:
      IOException - if an I/O error occurs.
    • mark

      public void mark(int readlimit)
      Marks the current position in this input stream. A subsequent call to the reset method repositions this stream at the last marked position so that subsequent reads re-read the same bytes.

      The readlimit arguments tells this input stream to allow that many bytes to be read before the mark position gets invalidated.

      The general contract of mark is that, if the method markSupported returns true, the stream somehow remembers all the bytes read after the call to mark and stands ready to supply those same bytes again if and whenever the method reset is called. However, the stream is not required to remember any data at all if more than readlimit bytes are read from the stream before reset is called.

      Marking a closed stream should not have any effect on the stream.

      Overrides:
      mark in class InputStream
      Parameters:
      readlimit - the maximum limit of bytes that can be read before the mark position becomes invalid.
      See Also:
    • reset

      public void reset() throws IOException
      Repositions this stream to the position at the time the mark method was last called on this input stream. NB: If mark is not called we move to the beginning.
      Overrides:
      reset in class InputStream
      Throws:
      IOException
      See Also:
    • markSupported

      public boolean markSupported()
      Tests if this input stream supports the mark and reset methods. The markSupported method of InputStream returns false.
      Overrides:
      markSupported in class InputStream
      Returns:
      true if this true type supports the mark and reset method; false otherwise.
      See Also:
    • getLo

      private LargeObject getLo() throws IOException
      Throws:
      IOException