java.lang.Object
org.apache.commons.jcs3.auxiliary.disk.block.BlockDisk
All Implemented Interfaces:
AutoCloseable

public class BlockDisk extends Object implements AutoCloseable
This class manages reading an writing data to disk. When asked to write a value, it returns a block array. It can read an object from the block numbers in a byte array.
  • Field Details

  • Constructor Details

  • Method Details

    • write

      protected <T> int[] write(T object) throws IOException
      This writes an object to disk and returns the blocks it was stored in.

      The program flow is as follows:

      1. Serialize the object.
      2. Determine the number of blocks needed.
      3. Look for free blocks in the emptyBlock list.
      4. If there were not enough in the empty list. Take the nextBlock and increment it.
      5. If the data will not fit in one block, create sub arrays.
      6. Write the subarrays to disk.
      7. If the process fails we should decrement the block count if we took from it.
      Parameters:
      object -
      Returns:
      the blocks we used.
      Throws:
      IOException
    • getBlockChunks

      protected byte[][] getBlockChunks(byte[] complete, int numBlocksNeeded)
      Return the amount to put in each block. Fill them all the way, minus the header.

      Parameters:
      complete -
      numBlocksNeeded -
      Returns:
      byte[][]
    • read

      protected <T> T read(int[] blockNumbers) throws IOException, ClassNotFoundException
      Reads an object that is located in the specified blocks.

      Parameters:
      blockNumbers -
      Returns:
      the object instance
      Throws:
      IOException
      ClassNotFoundException
    • freeBlocks

      protected void freeBlocks(int[] blocksToFree)
      Add these blocks to the emptyBlock list.

      Parameters:
      blocksToFree -
    • calculateByteOffsetForBlockAsLong

      protected long calculateByteOffsetForBlockAsLong(int block)
      Calculates the file offset for a particular block.

      Parameters:
      block - number
      Returns:
      the byte offset for this block in the file as a long
      Since:
      2.0
    • calculateTheNumberOfBlocksNeeded

      protected int calculateTheNumberOfBlocksNeeded(byte[] data)
      The number of blocks needed.

      Parameters:
      data -
      Returns:
      the number of blocks needed to store the byte array
    • length

      protected long length() throws IOException
      Returns the file length.

      Returns:
      the size of the file.
      Throws:
      IOException
    • close

      public void close() throws IOException
      Closes the file.

      Specified by:
      close in interface AutoCloseable
      Throws:
      IOException
    • reset

      protected void reset() throws IOException
      Resets the file.

      Throws:
      IOException
    • getNumberOfBlocks

      protected int getNumberOfBlocks()
      Returns:
      Returns the numberOfBlocks.
    • getBlockSizeBytes

      protected int getBlockSizeBytes()
      Returns:
      Returns the blockSizeBytes.
    • getAveragePutSizeBytes

      protected long getAveragePutSizeBytes()
      Returns:
      Returns the average size of the an element inserted.
    • getEmptyBlocks

      protected int getEmptyBlocks()
      Returns:
      Returns the number of empty blocks.
    • toString

      public String toString()
      For debugging only.

      Overrides:
      toString in class Object
      Returns:
      String with details.
    • getFilePath

      protected String getFilePath()
      This is used for debugging.

      Returns:
      the file path.