Class BlockSource

  • All Implemented Interfaces:
    java.lang.AutoCloseable
    Direct Known Subclasses:
    DfsReftable.CacheSource

    public abstract class BlockSource
    extends java.lang.Object
    implements java.lang.AutoCloseable
    Provides content blocks of file.

    BlockSource implementations must decide if they will be thread-safe, or not.

    • Constructor Summary

      Constructors 
      Constructor Description
      BlockSource()  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      void adviseSequentialRead​(long startPos, long endPos)
      Advise the BlockSource a sequential scan is starting.
      abstract void close()
      static BlockSource from​(byte[] content)
      Wrap a byte array as a BlockSource.
      static BlockSource from​(java.io.FileInputStream in)
      Read from a FileInputStream.
      static BlockSource from​(java.nio.channels.FileChannel ch)
      Read from a FileChannel.
      abstract java.nio.ByteBuffer read​(long position, int blockSize)
      Read a block from the file.
      abstract long size()
      Determine the size of the file.
      • Methods inherited from class java.lang.Object

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

      • BlockSource

        public BlockSource()
    • Method Detail

      • from

        public static BlockSource from​(byte[] content)
        Wrap a byte array as a BlockSource.
        Parameters:
        content - input file.
        Returns:
        block source to read from content.
      • from

        public static BlockSource from​(java.io.FileInputStream in)
        Read from a FileInputStream.

        The returned BlockSource is not thread-safe, as it must seek the file channel to read a block.

        Parameters:
        in - the file. The BlockSource will close in.
        Returns:
        wrapper for in.
      • from

        public static BlockSource from​(java.nio.channels.FileChannel ch)
        Read from a FileChannel.

        The returned BlockSource is not thread-safe, as it must seek the file channel to read a block.

        Parameters:
        ch - the file. The BlockSource will close ch.
        Returns:
        wrapper for ch.
      • read

        public abstract java.nio.ByteBuffer read​(long position,
                                                 int blockSize)
                                          throws java.io.IOException
        Read a block from the file.

        To reduce copying, the returned ByteBuffer should have an accessible array and arrayOffset() == 0. The caller will discard the ByteBuffer and directly use the backing array.

        Parameters:
        position - position of the block in the file, specified in bytes from the beginning of the file.
        blockSize - size to read.
        Returns:
        buffer containing the block content.
        Throws:
        java.io.IOException - if block cannot be read.
      • size

        public abstract long size()
                           throws java.io.IOException
        Determine the size of the file.
        Returns:
        total number of bytes in the file.
        Throws:
        java.io.IOException - if size cannot be obtained.
      • adviseSequentialRead

        public void adviseSequentialRead​(long startPos,
                                         long endPos)
        Advise the BlockSource a sequential scan is starting.
        Parameters:
        startPos - starting position.
        endPos - ending position.
      • close

        public abstract void close()
        Specified by:
        close in interface java.lang.AutoCloseable