Class RandomAccessBufferedFileInputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, RandomAccessRead

    public class RandomAccessBufferedFileInputStream
    extends java.io.InputStream
    implements RandomAccessRead
    Provides InputStream access to portions of a file combined with buffered reading of content. Start of next bytes to read can be set via seek method. File is accessed via RandomAccessFile and is read in byte chunks which are cached.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int available()
      Returns an estimate of the number of bytes that can be read.
      void close()  
      private java.io.File createTmpFile​(java.io.InputStream input)  
      private void deleteTempFile()
      Remove the temporary file.
      long getPosition()
      Returns offset in file at which next byte would be read.
      boolean isClosed()
      Returns true if this stream has been closed.
      boolean isEOF()
      A simple test to see if we are at the end of the data.
      long length()
      The total number of bytes that are available.
      int peek()
      This will peek at the next byte.
      int read()
      Read a single byte of data.
      int read​(byte[] b)
      Read a buffer of data.
      int read​(byte[] b, int off, int len)
      Read a buffer of data.
      byte[] readFully​(int length)
      Reads a given number of bytes.
      private byte[] readPage()
      Reads a page with data from current file position.
      void rewind​(int bytes)
      Seek backwards the given number of bytes.
      void seek​(long newOffset)
      Seeks to new position.
      long skip​(long n)  
      • Methods inherited from class java.io.InputStream

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

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

      • TMP_FILE_PREFIX

        private static final java.lang.String TMP_FILE_PREFIX
        The prefix for the temp file being used.
        See Also:
        Constant Field Values
      • pageSizeShift

        private int pageSizeShift
      • pageSize

        private int pageSize
      • pageOffsetMask

        private long pageOffsetMask
      • maxCachedPages

        private int maxCachedPages
      • tempFile

        private java.io.File tempFile
      • lastRemovedCachePage

        private byte[] lastRemovedCachePage
      • pageCache

        private final java.util.Map<java.lang.Long,​byte[]> pageCache
        Create a LRU page cache.
      • curPageOffset

        private long curPageOffset
      • curPage

        private byte[] curPage
      • offsetWithinPage

        private int offsetWithinPage
      • raFile

        private final java.io.RandomAccessFile raFile
      • fileLength

        private final long fileLength
      • fileOffset

        private long fileOffset
      • isClosed

        private boolean isClosed
    • Constructor Detail

      • RandomAccessBufferedFileInputStream

        public RandomAccessBufferedFileInputStream​(java.lang.String filename)
                                            throws java.io.IOException
        Create a random access input stream instance for the file with the given name.
        Parameters:
        filename - the filename of the file to be read.
        Throws:
        java.io.IOException - if something went wrong while accessing the given file.
      • RandomAccessBufferedFileInputStream

        public RandomAccessBufferedFileInputStream​(java.io.File file)
                                            throws java.io.IOException
        Create a random access input stream instance for the given file.
        Parameters:
        file - the file to be read.
        Throws:
        java.io.IOException - if something went wrong while accessing the given file.
      • RandomAccessBufferedFileInputStream

        public RandomAccessBufferedFileInputStream​(java.io.InputStream input)
                                            throws java.io.IOException
        Create a random access input stream for the given input stream by copying the data to a temporary file.
        Parameters:
        input - the input stream to be read. It will be closed by this method.
        Throws:
        java.io.IOException - if something went wrong while creating the temporary file.
    • Method Detail

      • createTmpFile

        private java.io.File createTmpFile​(java.io.InputStream input)
                                    throws java.io.IOException
        Throws:
        java.io.IOException
      • deleteTempFile

        private void deleteTempFile()
        Remove the temporary file. A temporary file is created if this class is instantiated with an InputStream
      • getPosition

        public long getPosition()
        Returns offset in file at which next byte would be read.
        Specified by:
        getPosition in interface RandomAccessRead
        Returns:
        offset of next byte which will be returned with next RandomAccessRead.read() (if no more bytes are left it returns a value >= length of source)
      • seek

        public void seek​(long newOffset)
                  throws java.io.IOException
        Seeks to new position. If new position is outside of current page the new page is either taken from cache or read from file and added to cache.
        Specified by:
        seek in interface RandomAccessRead
        Parameters:
        newOffset - the position to seek to.
        Throws:
        java.io.IOException - if something went wrong.
      • readPage

        private byte[] readPage()
                         throws java.io.IOException
        Reads a page with data from current file position. If we have a previously removed page from cache the buffer of this page is reused. Otherwise a new byte buffer is created.
        Throws:
        java.io.IOException
      • read

        public int read()
                 throws java.io.IOException
        Description copied from interface: RandomAccessRead
        Read a single byte of data.
        Specified by:
        read in interface RandomAccessRead
        Specified by:
        read in class java.io.InputStream
        Returns:
        The byte of data that is being read.
        Throws:
        java.io.IOException - If there is an error while reading the data.
      • read

        public int read​(byte[] b)
                 throws java.io.IOException
        Description copied from interface: RandomAccessRead
        Read a buffer of data.
        Specified by:
        read in interface RandomAccessRead
        Overrides:
        read in class java.io.InputStream
        Parameters:
        b - The buffer to write the data to.
        Returns:
        The number of bytes that were actually read.
        Throws:
        java.io.IOException - If there was an error while reading the data.
      • read

        public int read​(byte[] b,
                        int off,
                        int len)
                 throws java.io.IOException
        Description copied from interface: RandomAccessRead
        Read a buffer of data.
        Specified by:
        read in interface RandomAccessRead
        Overrides:
        read in class java.io.InputStream
        Parameters:
        b - The buffer to write the data to.
        off - Offset into the buffer to start writing.
        len - The amount of data to attempt to read.
        Returns:
        The number of bytes that were actually read.
        Throws:
        java.io.IOException - If there was an error while reading the data.
      • available

        public int available()
                      throws java.io.IOException
        Description copied from interface: RandomAccessRead
        Returns an estimate of the number of bytes that can be read.
        Specified by:
        available in interface RandomAccessRead
        Overrides:
        available in class java.io.InputStream
        Returns:
        the number of bytes that can be read
        Throws:
        java.io.IOException - if this random access has been closed
      • skip

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

        public long length()
                    throws java.io.IOException
        Description copied from interface: RandomAccessRead
        The total number of bytes that are available.
        Specified by:
        length in interface RandomAccessRead
        Returns:
        The number of bytes available.
        Throws:
        java.io.IOException - If there is an IO error while determining the length of the data stream.
      • 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
      • peek

        public int peek()
                 throws java.io.IOException
        Description copied from interface: RandomAccessRead
        This will peek at the next byte.
        Specified by:
        peek in interface RandomAccessRead
        Returns:
        The next byte on the stream, leaving it as available to read.
        Throws:
        java.io.IOException - If there is an error reading the next byte.
      • rewind

        public void rewind​(int bytes)
                    throws java.io.IOException
        Description copied from interface: RandomAccessRead
        Seek backwards the given number of bytes.
        Specified by:
        rewind in interface RandomAccessRead
        Parameters:
        bytes - the number of bytes to be seeked backwards
        Throws:
        java.io.IOException - If there is an error while seeking
      • readFully

        public byte[] readFully​(int length)
                         throws java.io.IOException
        Description copied from interface: RandomAccessRead
        Reads a given number of bytes.
        Specified by:
        readFully in interface RandomAccessRead
        Parameters:
        length - the number of bytes to be read
        Returns:
        a byte array containing the bytes just read
        Throws:
        java.io.IOException - if an I/O error occurs while reading data
      • isEOF

        public boolean isEOF()
                      throws java.io.IOException
        Description copied from interface: RandomAccessRead
        A simple test to see if we are at the end of the data.
        Specified by:
        isEOF in interface RandomAccessRead
        Returns:
        true if we are at the end of the data.
        Throws:
        java.io.IOException - If there is an error reading the next byte.