Class NikeFS2BlockProvider


  • public class NikeFS2BlockProvider
    extends java.lang.Object
    This class implements a block provider for the NikeFS2 virtual file system. It is backed by an OS-level random-access file, which stores the actual contents of the blocks. Also, this class contains static facilities for managing the current set of open files, thereby reducing the number of concurrently open and used file handles to a sensible minimum.
    Author:
    Christian W. Guenther (christian@deckfour.org)
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.BitSet blockAllocationMap
      Bit set indicating which blocks served by this provider are still free, i.e.
      protected int blockSize
      Size (in bytes) of each block in this block provider.
      protected java.io.File file
      Backing file for this block provider.
      protected boolean mapped
      Flag indicating whether this block provider is using memory mapping for faster access to its contents (default and recommended is true).
      protected int numberOfBlocks
      Number of blocks provided by this block provider.
      protected java.io.RandomAccessFile rafile
      Random-access file wrapper, if non-mapped
      protected int size
      Size (in bytes) of this block provider, i.e.
    • Constructor Summary

      Constructors 
      Constructor Description
      NikeFS2BlockProvider​(java.io.File storage, int size, int blockSize, boolean mapped)
      Creates a new block provider.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      NikeFS2Block allocateBlock()
      Allocates a new block from this block provider.
      int blockSize()
      Returns the size of blocks provided by this instance.
      void freeBlock​(NikeFS2Block block)
      De-allocates the specified block in this provider.
      int getBlockOffset​(int blockNumber)
      Returns the internal offset of the specified block in the backing file (in bytes).
      java.io.File getFile()
      Provides direct access to the backing file.
      int numberOfBlocks()
      Returns the number of blocks provided by this instance.
      int numberOfFreeBlocks()
      Returns the number of non-allocated blocks in this instance.
      int read​(int blockNumber, int blockOffset)
      Reads from the specified block.
      int read​(int blockNumber, int blockOffset, byte[] buffer)
      Reads from the specified block.
      int read​(int blockNumber, int blockOffset, byte[] buffer, int bufferOffset, int length)
      Reads from the specified block.
      int size()
      Returns the size, in bytes, of this block provider in total.
      void write​(int blockNumber, int blockOffset, byte[] buffer)
      Writes to this block.
      void write​(int blockNumber, int blockOffset, byte[] buffer, int bufferOffset, int length)
      Writes to this block.
      void write​(int blockNumber, int blockOffset, int value)
      Writes to this block.
      • Methods inherited from class java.lang.Object

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

      • mapped

        protected final boolean mapped
        Flag indicating whether this block provider is using memory mapping for faster access to its contents (default and recommended is true).
      • file

        protected final java.io.File file
        Backing file for this block provider.
      • rafile

        protected final java.io.RandomAccessFile rafile
        Random-access file wrapper, if non-mapped
      • size

        protected final int size
        Size (in bytes) of this block provider, i.e. of its backing file.
      • blockSize

        protected final int blockSize
        Size (in bytes) of each block in this block provider.
      • numberOfBlocks

        protected final int numberOfBlocks
        Number of blocks provided by this block provider.
      • blockAllocationMap

        protected final java.util.BitSet blockAllocationMap
        Bit set indicating which blocks served by this provider are still free, i.e. not yet allocated. Every block is addressed by its unique index within this provider. For each block, a bit set to true, at the block's index in this bit set, indicates that the block is free. Blocks whose index's bit is set to false are currently allocated.
    • Constructor Detail

      • NikeFS2BlockProvider

        public NikeFS2BlockProvider​(java.io.File storage,
                                    int size,
                                    int blockSize,
                                    boolean mapped)
                             throws java.io.IOException
        Creates a new block provider.
        Parameters:
        storage - Backing file to store actual contents in.
        size - Size of the backing file in bytes.
        blockSize - Size of blocks in bytes.
        mapped - Whether to use memory mapping for this block provider (it is recommended to set this flag to true).
        Throws:
        java.io.IOException
    • Method Detail

      • getFile

        public java.io.File getFile()
        Provides direct access to the backing file.
        Returns:
        The random access file backing this block provider.
      • size

        public int size()
        Returns the size, in bytes, of this block provider in total.
        Returns:
        Number of bytes totally provided by this instance.
      • numberOfBlocks

        public int numberOfBlocks()
        Returns the number of blocks provided by this instance.
        Returns:
        The number of blocks provided by this instance.
      • numberOfFreeBlocks

        public int numberOfFreeBlocks()
        Returns the number of non-allocated blocks in this instance.
        Returns:
        The number of non-allocated blocks in this instance.
      • blockSize

        public int blockSize()
        Returns the size of blocks provided by this instance.
        Returns:
        The size of blocks (in bytes) provided by this instance.
      • allocateBlock

        public NikeFS2Block allocateBlock()
        Allocates a new block from this block provider.
        Returns:
        A newly allocated block from this provider. May return null, if no free blocks are currently available.
      • freeBlock

        public void freeBlock​(NikeFS2Block block)
        De-allocates the specified block in this provider.
        Parameters:
        block - The block to be freed.
      • getBlockOffset

        public int getBlockOffset​(int blockNumber)
        Returns the internal offset of the specified block in the backing file (in bytes).
        Parameters:
        blockNumber - The internal number of the block in question.
        Returns:
        Internal offset of the block, in bytes, in the backing file.
      • read

        public int read​(int blockNumber,
                        int blockOffset,
                        byte[] buffer)
                 throws java.io.IOException
        Reads from the specified block.
        Parameters:
        blockNumber - Internal number of the block in question.
        blockOffset - Offset, in bytes, within this block.
        buffer - Buffer to store read data in.
        Returns:
        The number of read bytes.
        Throws:
        java.io.IOException
      • read

        public int read​(int blockNumber,
                        int blockOffset,
                        byte[] buffer,
                        int bufferOffset,
                        int length)
                 throws java.io.IOException
        Reads from the specified block.
        Parameters:
        blockNumber - Internal number of the block in question.
        blockOffset - Offset, in bytes, within this block.
        buffer - Buffer to store read data in.
        bufferOffset - Offset in the given buffer to start writing at.
        length - Number of bytes to be read.
        Returns:
        The number of read bytes.
        Throws:
        java.io.IOException
      • read

        public int read​(int blockNumber,
                        int blockOffset)
                 throws java.io.IOException
        Reads from the specified block.
        Parameters:
        blockNumber - Internal number of the block in question.
        blockOffset - Offset, in bytes, within this block.
        Returns:
        The read byte.
        Throws:
        java.io.IOException
      • write

        public void write​(int blockNumber,
                          int blockOffset,
                          byte[] buffer)
                   throws java.io.IOException
        Writes to this block.
        Parameters:
        blockNumber - Internal number of the block in question.
        blockOffset - Offset within the block to commence writing at.
        buffer - Buffer storing the data to be written.
        Throws:
        java.io.IOException
      • write

        public void write​(int blockNumber,
                          int blockOffset,
                          byte[] buffer,
                          int bufferOffset,
                          int length)
                   throws java.io.IOException
        Writes to this block.
        Parameters:
        blockNumber - Internal number of the block in question.
        blockOffset - Offset within the block to commence writing at.
        buffer - Buffer storing the data to be written.
        bufferOffset - Offset within the buffer from where to read.
        length - Number of bytes to be written.
        Throws:
        java.io.IOException
      • write

        public void write​(int blockNumber,
                          int blockOffset,
                          int value)
                   throws java.io.IOException
        Writes to this block.
        Parameters:
        blockNumber - Internal number of the block in question.
        blockOffset - Offset within the block to commence writing at.
        value - Byte value to be written.
        Throws:
        java.io.IOException