Class FileBuffer.Range

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable
    Enclosing class:
    FileBuffer

    private class FileBuffer.Range
    extends java.io.FilterInputStream
    The Range object is used to provide a stream that can read a range of bytes from a provided input stream. This allows buffer segments to be allocated from the main buffer. Providing a range in this manner ensures that only one backing file is needed for the primary buffer allocated.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private boolean closed
      This is used to close the stream once it has been read.
      private long length
      This is the length of the bytes that exist in the range.
      • Fields inherited from class java.io.FilterInputStream

        in
    • Constructor Summary

      Constructors 
      Constructor Description
      Range​(java.io.InputStream source, long length)
      Constructor for the Range object.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int available()
      This returns the number of bytes that can be read from the range.
      void close()
      This is used to close the range once all of the content has been fully read.
      int read()
      This will read data from the underlying stream up to the number of bytes this range is allowed to read.
      int read​(byte[] array, int off, int size)
      This will read data from the underlying stream up to the number of bytes this range is allowed to read.
      long skip​(long size)
      This is the number of bytes to skip from the buffer.
      • Methods inherited from class java.io.FilterInputStream

        mark, markSupported, read, reset
      • Methods inherited from class java.io.InputStream

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

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

      • length

        private long length
        This is the length of the bytes that exist in the range.
      • closed

        private boolean closed
        This is used to close the stream once it has been read.
    • Constructor Detail

      • Range

        public Range​(java.io.InputStream source,
                     long length)
        Constructor for the Range object. This ensures that only a limited number of bytes can be consumed from a backing input stream giving the impression of an independent stream of bytes for a segmented region of the parent buffer.
        Parameters:
        source - this is the input stream used to read data
        length - this is the number of bytes that can be read
    • Method Detail

      • read

        public int read()
                 throws java.io.IOException
        This will read data from the underlying stream up to the number of bytes this range is allowed to read. When all of the bytes are exhausted within the stream this returns -1.
        Overrides:
        read in class java.io.FilterInputStream
        Returns:
        this returns the octet from the underlying stream
        Throws:
        java.io.IOException
      • read

        public int read​(byte[] array,
                        int off,
                        int size)
                 throws java.io.IOException
        This will read data from the underlying stream up to the number of bytes this range is allowed to read. When all of the bytes are exhausted within the stream this returns -1.
        Overrides:
        read in class java.io.FilterInputStream
        Parameters:
        array - this is the array to read the bytes in to
        off - this is the start offset to append the bytes to
        size - this is the number of bytes that are required
        Returns:
        this returns the number of bytes that were read
        Throws:
        java.io.IOException
      • available

        public int available()
                      throws java.io.IOException
        This returns the number of bytes that can be read from the range. This will be the actual number of bytes the range contains as the underlying file will not block reading.
        Overrides:
        available in class java.io.FilterInputStream
        Returns:
        this returns the number of bytes within the range
        Throws:
        java.io.IOException
      • skip

        public long skip​(long size)
                  throws java.io.IOException
        This is the number of bytes to skip from the buffer. This will allow up to the number of remaining bytes within the range to be read. When all the bytes have been read this will return zero indicating no bytes were skipped.
        Overrides:
        skip in class java.io.FilterInputStream
        Parameters:
        size - this returns the number of bytes to skip
        Returns:
        this returns the number of bytes that were skipped
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        This is used to close the range once all of the content has been fully read. The Range object forces the close of the stream once all the content has been consumed to ensure that excessive file descriptors are used. Also this will ensure that the files can be deleted.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.FilterInputStream
        Throws:
        java.io.IOException