Class GroupedRandomAccessSource

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  GroupedRandomAccessSource.SourceEntry
      Used to track each source, along with useful meta data
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes this source.
      int get​(long position)
      Gets a byte at the specified position
      int get​(long position, byte[] bytes, int off, int len)
      Read an array of bytes of specified length from the specified position of source to the buffer applying the offset.
      private GroupedRandomAccessSource.SourceEntry getSourceEntryForOffset​(long offset)
      Returns the SourceEntry that contains the byte at the specified offset sourceReleased is called as a notification callback so subclasses can take care of cleanup when the source is no longer the active source
      protected int getStartingSourceIndex​(long offset)
      For a given offset, return the index of the source that contains the specified offset.
      long length()
      Gets the length of the source
      protected void sourceInUse​(IRandomAccessSource source)
      Called when a given source is about to become the active source.
      protected void sourceReleased​(IRandomAccessSource source)
      Called when a given source is no longer the active source.
      • Methods inherited from class java.lang.Object

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

      • size

        private final long size
        Cached size of the underlying channel
    • Constructor Detail

      • GroupedRandomAccessSource

        public GroupedRandomAccessSource​(IRandomAccessSource[] sources)
                                  throws java.io.IOException
        Constructs a new GroupedRandomAccessSource based on the specified set of sources
        Parameters:
        sources - the sources used to build this group
        Throws:
        java.io.IOException
    • Method Detail

      • getStartingSourceIndex

        protected int getStartingSourceIndex​(long offset)
        For a given offset, return the index of the source that contains the specified offset. This is an optimization feature to help optimize the access of the correct source without having to iterate through every single source each time. It is safe to always return 0, in which case the full set of sources will be searched. Subclasses should override this method if they are able to compute the source index more efficiently (for example FileChannelRandomAccessSource takes advantage of fixed size page buffers to compute the index)
        Parameters:
        offset - the offset
        Returns:
        the index of the input source that contains the specified offset, or 0 if unknown
      • getSourceEntryForOffset

        private GroupedRandomAccessSource.SourceEntry getSourceEntryForOffset​(long offset)
                                                                       throws java.io.IOException
        Returns the SourceEntry that contains the byte at the specified offset sourceReleased is called as a notification callback so subclasses can take care of cleanup when the source is no longer the active source
        Parameters:
        offset - the offset of the byte to look for
        Returns:
        the SourceEntry that contains the byte at the specified offset
        Throws:
        java.io.IOException - if there is a problem with IO (usually the result of the sourceReleased() call)
      • sourceReleased

        protected void sourceReleased​(IRandomAccessSource source)
                               throws java.io.IOException
        Called when a given source is no longer the active source. This gives subclasses the abilty to release resources, if appropriate.
        Parameters:
        source - the source that is no longer the active source
        Throws:
        java.io.IOException - if there are any problems
      • sourceInUse

        protected void sourceInUse​(IRandomAccessSource source)
                            throws java.io.IOException
        Called when a given source is about to become the active source. This gives subclasses the abilty to retrieve resources, if appropriate.
        Parameters:
        source - the source that is about to become the active source
        Throws:
        java.io.IOException - if there are any problems
      • get

        public int get​(long position)
                throws java.io.IOException
        Gets a byte at the specified position The source that contains the byte at position is retrieved, the correct offset into that source computed, then the value from that offset in the underlying source is returned.
        Specified by:
        get in interface IRandomAccessSource
        Parameters:
        position - byte position
        Returns:
        the byte, or -1 if EOF is reached
        Throws:
        java.io.IOException - in case of any reading error.
      • get

        public int get​(long position,
                       byte[] bytes,
                       int off,
                       int len)
                throws java.io.IOException
        Read an array of bytes of specified length from the specified position of source to the buffer applying the offset. If the number of bytes requested cannot be read, all the possible bytes will be read to the buffer, and the number of actually read bytes will be returned.
        Specified by:
        get in interface IRandomAccessSource
        Parameters:
        position - the position in the RandomAccessSource to read from
        bytes - output buffer
        off - offset into the output buffer where results will be placed
        len - the number of bytes to read
        Returns:
        the number of bytes actually read, or -1 if the file is at EOF
        Throws:
        java.io.IOException - in case of any I/O error.
      • length

        public long length()
        Gets the length of the source
        Specified by:
        length in interface IRandomAccessSource
        Returns:
        the length of this source
      • close

        public void close()
                   throws java.io.IOException
        Closes this source. The underlying data structure or source (if any) will also be closed
        Closes all of the underlying sources.
        Specified by:
        close in interface IRandomAccessSource
        Throws:
        java.io.IOException - in case of any reading error.