Package org.deckfour.xes.nikefs2
Class NikeFS2BlockProvider
- java.lang.Object
-
- org.deckfour.xes.nikefs2.NikeFS2BlockProvider
-
public class NikeFS2BlockProvider extends java.lang.ObjectThis 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.BitSetblockAllocationMapBit set indicating which blocks served by this provider are still free, i.e.protected intblockSizeSize (in bytes) of each block in this block provider.protected java.io.FilefileBacking file for this block provider.protected booleanmappedFlag indicating whether this block provider is using memory mapping for faster access to its contents (default and recommended is true).protected intnumberOfBlocksNumber of blocks provided by this block provider.protected java.io.RandomAccessFilerafileRandom-access file wrapper, if non-mappedprotected intsizeSize (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 NikeFS2BlockallocateBlock()Allocates a new block from this block provider.intblockSize()Returns the size of blocks provided by this instance.voidfreeBlock(NikeFS2Block block)De-allocates the specified block in this provider.intgetBlockOffset(int blockNumber)Returns the internal offset of the specified block in the backing file (in bytes).java.io.FilegetFile()Provides direct access to the backing file.intnumberOfBlocks()Returns the number of blocks provided by this instance.intnumberOfFreeBlocks()Returns the number of non-allocated blocks in this instance.intread(int blockNumber, int blockOffset)Reads from the specified block.intread(int blockNumber, int blockOffset, byte[] buffer)Reads from the specified block.intread(int blockNumber, int blockOffset, byte[] buffer, int bufferOffset, int length)Reads from the specified block.intsize()Returns the size, in bytes, of this block provider in total.voidwrite(int blockNumber, int blockOffset, byte[] buffer)Writes to this block.voidwrite(int blockNumber, int blockOffset, byte[] buffer, int bufferOffset, int length)Writes to this block.voidwrite(int blockNumber, int blockOffset, int value)Writes to this block.
-
-
-
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 totrue, at the block's index in this bit set, indicates that the block is free. Blocks whose index's bit is set tofalseare currently allocated.
-
-
Constructor Detail
-
NikeFS2BlockProvider
public NikeFS2BlockProvider(java.io.File storage, int size, int blockSize, boolean mapped) throws java.io.IOExceptionCreates 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 totrue).- 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.IOExceptionReads 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.IOExceptionReads 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.IOExceptionReads 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.IOExceptionWrites 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.IOExceptionWrites 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.IOExceptionWrites 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
-
-