public class RandomAccessStorageModule extends Object implements IStorageModule
RandomAccessFile, which will immediately write all
changes in the data to hard-disk.
This class is not thread-safe.
RandomAccessFileIStorageModule.STORAGEKIND| Modifier and Type | Field and Description |
|---|---|
protected long |
sizeInBlocks
The size of the medium in blocks.
|
VIRTUAL_BLOCK_SIZE| Modifier | Constructor and Description |
|---|---|
protected |
RandomAccessStorageModule(long sizeInBlocks,
RandomAccessFile randomAccessFile)
Creates a new
RandomAccessStorageModule backed by the specified
file. |
| Modifier and Type | Method and Description |
|---|---|
int |
checkBounds(long logicalBlockAddress,
int transferLengthInBlocks)
This method can be used for checking if a (series of) I/O operations will
result in an
IOException due to trying to access blocks outside
the medium's boundaries. |
void |
close()
Closes the backing
RandomAccessFile. |
long |
getSizeInBlocks()
Returns the storage space size in bytes divided by
the block size in bytes (rounded down).
|
static IStorageModule |
open(File file,
long storageLength,
boolean create,
IStorageModule.STORAGEKIND kind)
This is the build method for creating instances of
RandomAccessStorageModule. |
void |
read(byte[] bytes,
int bytesOffset,
int length,
long storageIndex)
Copies bytes from storage to the passed byte array.
|
void |
write(byte[] bytes,
int bytesOffset,
int length,
long storageIndex)
Saves part of the passed byte array's content.
|
protected final long sizeInBlocks
IStorageModule.VIRTUAL_BLOCK_SIZEprotected RandomAccessStorageModule(long sizeInBlocks,
RandomAccessFile randomAccessFile)
throws FileNotFoundException
RandomAccessStorageModule backed by the specified
file. If no such file exists, a FileNotFoundException will be
thrown.sizeInBlocks - blocksize for this modulerandomAccessFile - the path to the file serving as storage mediumFileNotFoundException - if the specified file does not existpublic void read(byte[] bytes,
int bytesOffset,
int length,
long storageIndex)
throws IOException
read in interface IStorageModulebytes - the array into which the data will be copiedbytesOffset - the position of the first byte in bytes, which
will be filled with data from storagelength - the number of bytes to copystorageIndex - the position of the first byte to be copiedIOExceptionpublic void write(byte[] bytes,
int bytesOffset,
int length,
long storageIndex)
throws IOException
write in interface IStorageModulebytes - the source of the data to be storedbytesOffset - offset of the first byte to be storedlength - the number of bytes to be copiedstorageIndex - byte offset in the storage areaIOExceptionpublic final long getSizeInBlocks()
getSizeInBlocks in interface IStorageModulepublic final int checkBounds(long logicalBlockAddress,
int transferLengthInBlocks)
IOException due to trying to access blocks outside
the medium's boundaries.
The SCSI standard requires checking for these boundary violations right after receiving a read or write command, so that an appropriate error message can be returned to the initiator. Therefore this method must be called prior to each read or write sequence.
The values returned by this method and their meaning with regard to the interval [0,
IStorageModule.getSizeInBlocks() - 1] are shown in the following table:
| Return Value | Meaning |
|---|---|
| 0 | no boundaries are violated |
| 1 | the logicalBlockAddress parameter lies outside of the interval |
| 2 | the interval [logicalBlockAddress, logicalBlockAddress +
transferLengthInBlocks] lies outside of the interval, or transferLengthInBlocks is negative |
Note that the parameters of this method are referring to blocks, not to byte indices.
checkBounds in interface IStorageModulelogicalBlockAddress - the index of the first block of data to be read or writtentransferLengthInBlocks - the total number of consecutive blocks about to be read or
writtenpublic final void close()
throws IOException
RandomAccessFile.close in interface IStorageModuleIOException - if an I/O Error occurspublic static final IStorageModule open(File file, long storageLength, boolean create, IStorageModule.STORAGEKIND kind) throws IOException
RandomAccessStorageModule. If there is no
file to be found at the
specified filePath, then a FileNotFoundException will be thrown.file - a path leading to the file serving as storage mediumstorageLength - length of storage (if not already existing)create - should the storage be createdRandomAccessStorageModuleIOExceptionCopyright © 2012 University of Konstanz, Distributed Systems Group. All Rights Reserved.