Class PagedInputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable
    Direct Known Subclasses:
    AbstractStreamableObject.StreamableObjectInputStream, BlobStore.BLOBInputStream

    public abstract class PagedInputStream
    extends java.io.InputStream
    An implementation of InputStream that reads data from an underlying representation in fixed sized pages. This object maintains a single buffer that is the size of a page. This implementation supports 'skip' and buffered access to the data.

    The only method that needs to be implemented is the 'readPageContent' method.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private byte[] buf
      The buffer.
      private long buffer_pos
      The start buffer position.
      private int BUFFER_SIZE
      The size of the buffer page.
      private long mark_position
      Last marked position.
      private long position
      The current position in the stream.
      private long size
      The total size of the underlying dataset.
    • Constructor Summary

      Constructors 
      Constructor Description
      PagedInputStream​(int page_size, long total_size)
      Constructs the input stream.
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      int available()  
      void close()  
      private void fillBuffer​(long pos)
      Fills the buffer with data from the blob at the given position.
      void mark​(int limit)  
      boolean markSupported()  
      int read()  
      int read​(byte[] read_buf, int off, int len)  
      protected abstract void readPageContent​(byte[] buf, long pos, int length)
      Reads the page at the given offset in the underlying data into the given byte[] array.
      void reset()  
      long skip​(long n)  
      • Methods inherited from class java.io.InputStream

        nullInputStream, read, readAllBytes, readNBytes, readNBytes, transferTo
      • Methods inherited from class java.lang.Object

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

      • BUFFER_SIZE

        private final int BUFFER_SIZE
        The size of the buffer page.
      • position

        private long position
        The current position in the stream.
      • size

        private long size
        The total size of the underlying dataset.
      • buffer_pos

        private long buffer_pos
        The start buffer position.
      • buf

        private final byte[] buf
        The buffer.
      • mark_position

        private long mark_position
        Last marked position.
    • Constructor Detail

      • PagedInputStream

        public PagedInputStream​(int page_size,
                                long total_size)
        Constructs the input stream.
        Parameters:
        page_size - the size of the pages when accessing the underlying stream.
        total_size - the total size of the underlying data set.
    • Method Detail

      • readPageContent

        protected abstract void readPageContent​(byte[] buf,
                                                long pos,
                                                int length)
                                         throws java.io.IOException
        Reads the page at the given offset in the underlying data into the given byte[] array. The 'pos' variable given is guarenteed to be a multiple of buffer_size. For example, the first access will be to pos = 0, the second access to pos = BUFFER_SIZE, the third access to pos = BUFFER_SIZE * 2, etc. 'length' will always be either BUFFER_SIZE or a value smaller than BUFFER_SIZE if the page containing the end of the stream is read.
        Throws:
        java.io.IOException
      • fillBuffer

        private void fillBuffer​(long pos)
                         throws java.io.IOException
        Fills the buffer with data from the blob at the given position. A buffer may be partially filled if the end is reached.
        Throws:
        java.io.IOException
      • read

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

        public int read​(byte[] read_buf,
                        int off,
                        int len)
                 throws java.io.IOException
        Overrides:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • skip

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

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

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

        public void mark​(int limit)
        Overrides:
        mark in class java.io.InputStream
      • reset

        public void reset()
        Overrides:
        reset in class java.io.InputStream
      • markSupported

        public boolean markSupported()
        Overrides:
        markSupported in class java.io.InputStream