Class ScratchFileBuffer

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private byte[] currentPage
      The current page data.
      private boolean currentPageContentChanged
      true if current page was changed by a write method
      private long currentPageOffset
      The offset of the current page within this buffer.
      private int currentPagePositionInPageIndexes
      Index of current page in pageIndexes (the nth page within this buffer).
      private static org.apache.commons.logging.Log LOG  
      private int pageCount
      number of pages held by this buffer
      private ScratchFile pageHandler
      The underlying page handler.
      private int[] pageIndexes
      contains ordered list of pages with the index the page is known by page handler (ScratchFile)
      private int pageSize  
      private int positionInPage
      The current position (for next read/write) of the buffer as an offset in the current page.
      private long size
      The number of bytes of content in this buffer.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void addPage()
      Adds a new page and positions all pointers to start of new page.
      int available()
      Returns an estimate of the number of bytes that can be read.
      private void checkClosed()
      Checks if this buffer, or the underlying ScratchFile have been closed, throwing IOException if so.
      void clear()
      Clears all data of the buffer.
      void close()
      private boolean ensureAvailableBytesInPage​(boolean addNewPageIfNeeded)
      Ensures the current page has at least one byte left (positionInPage in < pageSize).
      protected void finalize()
      While calling finalize is normally discouraged we will have to use it here as long as closing a scratch file buffer is not done in every case.
      long getPosition()
      Returns offset of next byte to be returned by a read method.
      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 len)
      Reads a given number of bytes.
      void rewind​(int bytes)
      Seek backwards the given number of bytes.
      void seek​(long seekToPosition)
      Seek to a position in the data.
      void write​(byte[] b)
      Write a buffer of data to the stream.
      void write​(byte[] b, int off, int len)
      Write a buffer of data to the stream.
      void write​(int b)
      Write a byte to the stream.
      • Methods inherited from class java.lang.Object

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

      • pageSize

        private final int pageSize
      • pageHandler

        private ScratchFile pageHandler
        The underlying page handler.
      • size

        private long size
        The number of bytes of content in this buffer.
      • currentPagePositionInPageIndexes

        private int currentPagePositionInPageIndexes
        Index of current page in pageIndexes (the nth page within this buffer).
      • currentPageOffset

        private long currentPageOffset
        The offset of the current page within this buffer.
      • currentPage

        private byte[] currentPage
        The current page data.
      • positionInPage

        private int positionInPage
        The current position (for next read/write) of the buffer as an offset in the current page.
      • currentPageContentChanged

        private boolean currentPageContentChanged
        true if current page was changed by a write method
      • pageIndexes

        private int[] pageIndexes
        contains ordered list of pages with the index the page is known by page handler (ScratchFile)
      • pageCount

        private int pageCount
        number of pages held by this buffer
      • LOG

        private static final org.apache.commons.logging.Log LOG
    • Constructor Detail

      • ScratchFileBuffer

        ScratchFileBuffer​(ScratchFile pageHandler)
                   throws java.io.IOException
        Creates a new buffer using pages handled by provided ScratchFile.
        Parameters:
        pageHandler - The ScratchFile managing the pages to be used by this buffer.
        Throws:
        java.io.IOException - If getting first page failed.
    • Method Detail

      • checkClosed

        private void checkClosed()
                          throws java.io.IOException
        Checks if this buffer, or the underlying ScratchFile have been closed, throwing IOException if so.
        Throws:
        java.io.IOException - If either this buffer, or the underlying ScratchFile have been closed.
      • addPage

        private void addPage()
                      throws java.io.IOException
        Adds a new page and positions all pointers to start of new page.
        Throws:
        java.io.IOException - if requesting a new page fails
      • length

        public long length()
                    throws java.io.IOException
        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.
      • ensureAvailableBytesInPage

        private boolean ensureAvailableBytesInPage​(boolean addNewPageIfNeeded)
                                            throws java.io.IOException
        Ensures the current page has at least one byte left (positionInPage in < pageSize).

        If this is not the case we go to next page (writing current one if changed). If current buffer has no more pages we add a new one.

        Parameters:
        addNewPageIfNeeded - if true it is allowed to add a new page in case we are currently at end of last buffer page
        Returns:
        true if we were successful positioning pointer before end of page; we might return false if it is not allowed to add another page and current pointer points at end of last page
        Throws:
        java.io.IOException
      • write

        public void write​(int b)
                   throws java.io.IOException
        Write a byte to the stream.
        Specified by:
        write in interface RandomAccessWrite
        Parameters:
        b - The byte to write.
        Throws:
        java.io.IOException - If there is an IO error while writing.
      • write

        public void write​(byte[] b)
                   throws java.io.IOException
        Write a buffer of data to the stream.
        Specified by:
        write in interface RandomAccessWrite
        Parameters:
        b - The buffer to get the data from.
        Throws:
        java.io.IOException - If there is an error while writing the data.
      • write

        public void write​(byte[] b,
                          int off,
                          int len)
                   throws java.io.IOException
        Write a buffer of data to the stream.
        Specified by:
        write in interface RandomAccessWrite
        Parameters:
        b - The buffer to get the data from.
        off - An offset into the buffer to get the data from.
        len - The length of data to write.
        Throws:
        java.io.IOException - If there is an error while writing the data.
      • clear

        public final void clear()
                         throws java.io.IOException
        Clears all data of the buffer.
        Specified by:
        clear in interface RandomAccessWrite
        Throws:
        java.io.IOException
      • getPosition

        public long getPosition()
                         throws java.io.IOException
        Returns offset of next byte to be returned by a read method.
        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)
        Throws:
        java.io.IOException
      • seek

        public void seek​(long seekToPosition)
                  throws java.io.IOException
        Seek to a position in the data.
        Specified by:
        seek in interface RandomAccessRead
        Parameters:
        seekToPosition - The position to seek to.
        Throws:
        java.io.IOException - If there is an error while seeking.
      • isClosed

        public boolean isClosed()
        Returns true if this stream has been closed.
        Specified by:
        isClosed in interface RandomAccessRead
      • peek

        public int peek()
                 throws java.io.IOException
        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
        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 len)
                         throws java.io.IOException
        Reads a given number of bytes.
        Specified by:
        readFully in interface RandomAccessRead
        Parameters:
        len - 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
        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.
      • available

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

        public int read()
                 throws java.io.IOException
        Read a single byte of data.
        Specified by:
        read in interface RandomAccessRead
        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
        Read a buffer of data.
        Specified by:
        read in interface RandomAccessRead
        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
        Read a buffer of data.
        Specified by:
        read in interface RandomAccessRead
        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.
      • close

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

        protected void finalize()
                         throws java.lang.Throwable
        While calling finalize is normally discouraged we will have to use it here as long as closing a scratch file buffer is not done in every case. Currently COSStream creates new buffers without closing the old one - which might still be used.

        Enabling debugging one will see if there are still cases where the buffer is not closed.

        Overrides:
        finalize in class java.lang.Object
        Throws:
        java.lang.Throwable