Class BlockDisk
java.lang.Object
org.apache.commons.jcs3.auxiliary.disk.block.BlockDisk
- All Implemented Interfaces:
- 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 SummaryFieldsModifier and TypeFieldDescriptionstatic final byteThe size of the header that indicates the amount of data stored in an occupied block.
- 
Constructor SummaryConstructorsConstructorDescriptionCreates the file and set the block size in bytes.BlockDisk(File file, int blockSizeBytes, IElementSerializer elementSerializer) Creates the file and set the block size in bytes.BlockDisk(File file, IElementSerializer elementSerializer) Constructor for the Disk object
- 
Method SummaryModifier and TypeMethodDescriptionprotected longcalculateByteOffsetForBlockAsLong(int block) Calculates the file offset for a particular block.protected intcalculateTheNumberOfBlocksNeeded(byte[] data) The number of blocks needed.voidclose()Closes the file.protected voidfreeBlocks(int[] blocksToFree) Add these blocks to the emptyBlock list.protected longprotected byte[][]getBlockChunks(byte[] complete, int numBlocksNeeded) Return the amount to put in each block.protected intprotected intprotected StringThis is used for debugging.protected intprotected longlength()Returns the file length.protected <T> Tread(int[] blockNumbers) Reads an object that is located in the specified blocks.protected voidreset()Resets the file.toString()For debugging only.protected <T> int[]write(T object) This writes an object to disk and returns the blocks it was stored in.
- 
Field Details- 
HEADER_SIZE_BYTESThe size of the header that indicates the amount of data stored in an occupied block.- See Also:
 
 
- 
- 
Constructor Details- 
BlockDiskConstructor for the Disk object- Parameters:
- file-
- elementSerializer-
- Throws:
- IOException
 
- 
BlockDiskCreates the file and set the block size in bytes.- Parameters:
- file-
- blockSizeBytes-
- Throws:
- IOException
 
- 
BlockDiskpublic BlockDisk(File file, int blockSizeBytes, IElementSerializer elementSerializer) throws IOException Creates the file and set the block size in bytes.- Parameters:
- file-
- blockSizeBytes-
- elementSerializer-
- Throws:
- IOException
 
 
- 
- 
Method Details- 
writeThis writes an object to disk and returns the blocks it was stored in.The program flow is as follows: - Serialize the object.
- Determine the number of blocks needed.
- Look for free blocks in the emptyBlock list.
- If there were not enough in the empty list. Take the nextBlock and increment it.
- If the data will not fit in one block, create sub arrays.
- Write the subarrays to disk.
- If the process fails we should decrement the block count if we took from it.
 - Parameters:
- object-
- Returns:
- the blocks we used.
- Throws:
- IOException
 
- 
getBlockChunksReturn the amount to put in each block. Fill them all the way, minus the header.- Parameters:
- complete-
- numBlocksNeeded-
- Returns:
- byte[][]
 
- 
readReads an object that is located in the specified blocks.- Parameters:
- blockNumbers-
- Returns:
- the object instance
- Throws:
- IOException
- ClassNotFoundException
 
- 
freeBlocksAdd these blocks to the emptyBlock list.- Parameters:
- blocksToFree-
 
- 
calculateByteOffsetForBlockAsLongCalculates 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
 
- 
calculateTheNumberOfBlocksNeededThe number of blocks needed.- Parameters:
- data-
- Returns:
- the number of blocks needed to store the byte array
 
- 
lengthReturns the file length.- Returns:
- the size of the file.
- Throws:
- IOException
 
- 
closeCloses the file.- Specified by:
- closein interface- AutoCloseable
- Throws:
- IOException
 
- 
resetResets the file.- Throws:
- IOException
 
- 
getNumberOfBlocks- Returns:
- Returns the numberOfBlocks.
 
- 
getBlockSizeBytes- Returns:
- Returns the blockSizeBytes.
 
- 
getAveragePutSizeBytes- Returns:
- Returns the average size of the an element inserted.
 
- 
getEmptyBlocks- Returns:
- Returns the number of empty blocks.
 
- 
toStringFor debugging only.
- 
getFilePathThis is used for debugging.- Returns:
- the file path.
 
 
-