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 Summary
FieldsModifier and TypeFieldDescriptionstatic final byteThe size of the header that indicates the amount of data stored in an occupied block. -
Constructor Summary
ConstructorsConstructorDescriptionCreates 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 Summary
Modifier 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_BYTES
The size of the header that indicates the amount of data stored in an occupied block.- See Also:
-
-
Constructor Details
-
BlockDisk
Constructor for the Disk object- Parameters:
file-elementSerializer-- Throws:
IOException
-
BlockDisk
Creates the file and set the block size in bytes.- Parameters:
file-blockSizeBytes-- Throws:
IOException
-
BlockDisk
public 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
-
write
This 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
-
getBlockChunks
Return the amount to put in each block. Fill them all the way, minus the header.- Parameters:
complete-numBlocksNeeded-- Returns:
- byte[][]
-
read
Reads an object that is located in the specified blocks.- Parameters:
blockNumbers-- Returns:
- the object instance
- Throws:
IOExceptionClassNotFoundException
-
freeBlocks
Add these blocks to the emptyBlock list.- Parameters:
blocksToFree-
-
calculateByteOffsetForBlockAsLong
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
The number of blocks needed.- Parameters:
data-- Returns:
- the number of blocks needed to store the byte array
-
length
Returns the file length.- Returns:
- the size of the file.
- Throws:
IOException
-
close
Closes the file.- Specified by:
closein interfaceAutoCloseable- Throws:
IOException
-
reset
Resets 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.
-
toString
For debugging only. -
getFilePath
This is used for debugging.- Returns:
- the file path.
-