org.processmining.framework.log.rfb.io
Class VirtFsRandomAccessStorage

java.lang.Object
  extended by org.processmining.framework.log.rfb.io.VirtFsRandomAccessStorage
All Implemented Interfaces:
java.io.DataInput, java.io.DataOutput, RandomAccessStorage

public class VirtFsRandomAccessStorage
extends java.lang.Object
implements RandomAccessStorage

This class implements a file metaphor, i.e. an ordered set of bytes which can be randomly accessed and modified. As it is dynamically backed by a virtual file system, the VFS/NIKEFS restrictions apply:

This file metaphor has the restriction, that the file must either be written in exactly the same granularity on re-writing specific address spaces, or preferably no modification other than appending data does occur.

Author:
Christian W. Guenther (christian at deckfour dot org)

Field Summary
protected  java.util.ArrayList<StorageBlock> blocks
          List of blocks representing this file's contents
protected  VirtualFileSystem fs
          Virtual file system from which blocks are allocated
protected static int INSTANCE_COUNTER
          Static instance counter, used to provide short names to instances of this class.
protected  int instanceNumber
          Instance number, for short reference
static int MAX_BLOCKS_PER_STORAGE
          This constant holds the maximal number of blocks that a virtual file / random access storage from this class can be distributed over.
protected  long pointer
          Current virtual file pointer
 
Constructor Summary
VirtFsRandomAccessStorage(VirtualFileSystem vfs)
          Creates a new virtual file metaphor, based on the given virtual file system object for allocating blocks.
 
Method Summary
 void close()
          Closes the data storage container.
protected  StorageBlock consolidateBlocks(int minNewSize)
          Consolidates the virtual file, i.e.: allocates a new block that can accomodate at least the size of the currently contained blocks, copy the contents of these to the new block, and finally replace the previous blocks with the new block.
This prevents scattering of virtual files over too many blocks, thus increasing R/W performance of the virtual file system.
 RandomAccessStorage copy()
          Creates a clone, or copy, of this storage, having the exact same contents and the file pointer reset to zero.
protected  StorageBlock getBlockForReading(int bytes)
          Returns a block for safely reading the specified number of bytes from the current file pointer offset.
protected  StorageBlock getBlockForWriting(int bytes)
          Returns a block, to which the specified number of bytes can be safely written, starting from the current pointer.
 long getFilePointer()
          Returns the current file pointer of the storage container.
 long length()
          Returns the length, or size, in number of bytes currently used by this instance.
 boolean readBoolean()
           
 byte readByte()
           
 char readChar()
           
 double readDouble()
           
 float readFloat()
           
 void readFully(byte[] b)
           
 void readFully(byte[] b, int off, int len)
           
 int readInt()
           
 java.lang.String readLine()
           
 long readLong()
           
 short readShort()
           
 int readUnsignedByte()
           
 int readUnsignedShort()
           
 java.lang.String readUTF()
           
 void seek(long pos)
          Repositions the offset, or file pointer, at which the next read or write operation will occur.
 int skipBytes(int n)
          Moves the offset, or file pointer, a specified number of bytes towards the end of the storage container.
 void write(byte[] b)
           
 void write(byte[] b, int off, int len)
           
 void write(int b)
           
 void writeBoolean(boolean v)
           
 void writeByte(int v)
           
 void writeBytes(java.lang.String s)
           
 void writeChar(int v)
           
 void writeChars(java.lang.String s)
           
 void writeDouble(double v)
           
 void writeFloat(float v)
           
 void writeInt(int v)
           
 void writeLong(long v)
           
 void writeShort(int v)
           
 void writeUTF(java.lang.String str)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INSTANCE_COUNTER

protected static int INSTANCE_COUNTER
Static instance counter, used to provide short names to instances of this class.


MAX_BLOCKS_PER_STORAGE

public static final int MAX_BLOCKS_PER_STORAGE
This constant holds the maximal number of blocks that a virtual file / random access storage from this class can be distributed over. If this number of blocks is exceeded, the contents of the 'file' will be transferred to a newly allocated block which can accomodate the complete size of the 'file'.

See Also:
Constant Field Values

instanceNumber

protected int instanceNumber
Instance number, for short reference


fs

protected VirtualFileSystem fs
Virtual file system from which blocks are allocated


blocks

protected java.util.ArrayList<StorageBlock> blocks
List of blocks representing this file's contents


pointer

protected long pointer
Current virtual file pointer

Constructor Detail

VirtFsRandomAccessStorage

public VirtFsRandomAccessStorage(VirtualFileSystem vfs)
Creates a new virtual file metaphor, based on the given virtual file system object for allocating blocks.

Parameters:
vfs - Virtual file system providing blocks for this file.
Method Detail

getBlockForWriting

protected StorageBlock getBlockForWriting(int bytes)
                                   throws java.io.IOException
Returns a block, to which the specified number of bytes can be safely written, starting from the current pointer. The block is preconfigured to point to the given virtual offset internally before it is returned.

Parameters:
bytes - Number of bytes to be written.
Returns:
A storage block, to which the specified number of bytes can be written at the specified position.
Throws:
java.io.IOException

consolidateBlocks

protected StorageBlock consolidateBlocks(int minNewSize)
                                  throws java.io.IOException
Consolidates the virtual file, i.e.: allocates a new block that can accomodate at least the size of the currently contained blocks, copy the contents of these to the new block, and finally replace the previous blocks with the new block.
This prevents scattering of virtual files over too many blocks, thus increasing R/W performance of the virtual file system. Notice, however, that consolidating virtual files usually goes hand in hand with increased swap disk space consumption, as the virtual file system can not manage the available real disk space as efficiently as before.

Parameters:
minNewSize - Minimal size, in bytes, the newly allocated block should be able to accomodate.
Returns:
The newly allocated block (already added to the list of blocks)
Throws:
java.io.IOException

getBlockForReading

protected StorageBlock getBlockForReading(int bytes)
                                   throws java.io.IOException
Returns a block for safely reading the specified number of bytes from the current file pointer offset. The block is preconfigured to point to the given virtual offset internally before it is returned.

Parameters:
bytes - Number of bytes to be read.
Returns:
Block for reading the given number of bytes safely.
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Description copied from interface: RandomAccessStorage
Closes the data storage container. After this method has been invoked, no further access to the represented instance is allowed.

Specified by:
close in interface RandomAccessStorage
Throws:
java.io.IOException

getFilePointer

public long getFilePointer()
                    throws java.io.IOException
Description copied from interface: RandomAccessStorage
Returns the current file pointer of the storage container. A file pointer is the offset in bytes, from the beginning of the sequential byte storage, at which the next read or write operation would occur.

Specified by:
getFilePointer in interface RandomAccessStorage
Returns:
Offset in bytes from beginning of storage.
Throws:
java.io.IOException

length

public long length()
            throws java.io.IOException
Description copied from interface: RandomAccessStorage
Returns the length, or size, in number of bytes currently used by this instance.

Specified by:
length in interface RandomAccessStorage
Returns:
Number of bytes currently allocated.
Throws:
java.io.IOException

seek

public void seek(long pos)
          throws java.io.IOException
Description copied from interface: RandomAccessStorage
Repositions the offset, or file pointer, at which the next read or write operation will occur.

Specified by:
seek in interface RandomAccessStorage
Parameters:
pos - The offset in bytes, at which the next operation will occur.
Throws:
java.io.IOException

skipBytes

public int skipBytes(int n)
              throws java.io.IOException
Description copied from interface: RandomAccessStorage
Moves the offset, or file pointer, a specified number of bytes towards the end of the storage container.

Specified by:
skipBytes in interface java.io.DataInput
Specified by:
skipBytes in interface RandomAccessStorage
Throws:
java.io.IOException

write

public void write(int b)
           throws java.io.IOException
Specified by:
write in interface java.io.DataOutput
Throws:
java.io.IOException

write

public void write(byte[] b)
           throws java.io.IOException
Specified by:
write in interface java.io.DataOutput
Throws:
java.io.IOException

write

public void write(byte[] b,
                  int off,
                  int len)
           throws java.io.IOException
Specified by:
write in interface java.io.DataOutput
Throws:
java.io.IOException

writeBoolean

public void writeBoolean(boolean v)
                  throws java.io.IOException
Specified by:
writeBoolean in interface java.io.DataOutput
Throws:
java.io.IOException

writeByte

public void writeByte(int v)
               throws java.io.IOException
Specified by:
writeByte in interface java.io.DataOutput
Throws:
java.io.IOException

writeBytes

public void writeBytes(java.lang.String s)
                throws java.io.IOException
Specified by:
writeBytes in interface java.io.DataOutput
Throws:
java.io.IOException

writeChar

public void writeChar(int v)
               throws java.io.IOException
Specified by:
writeChar in interface java.io.DataOutput
Throws:
java.io.IOException

writeChars

public void writeChars(java.lang.String s)
                throws java.io.IOException
Specified by:
writeChars in interface java.io.DataOutput
Throws:
java.io.IOException

writeDouble

public void writeDouble(double v)
                 throws java.io.IOException
Specified by:
writeDouble in interface java.io.DataOutput
Throws:
java.io.IOException

writeFloat

public void writeFloat(float v)
                throws java.io.IOException
Specified by:
writeFloat in interface java.io.DataOutput
Throws:
java.io.IOException

writeInt

public void writeInt(int v)
              throws java.io.IOException
Specified by:
writeInt in interface java.io.DataOutput
Throws:
java.io.IOException

writeLong

public void writeLong(long v)
               throws java.io.IOException
Specified by:
writeLong in interface java.io.DataOutput
Throws:
java.io.IOException

writeShort

public void writeShort(int v)
                throws java.io.IOException
Specified by:
writeShort in interface java.io.DataOutput
Throws:
java.io.IOException

writeUTF

public void writeUTF(java.lang.String str)
              throws java.io.IOException
Specified by:
writeUTF in interface java.io.DataOutput
Throws:
java.io.IOException

readBoolean

public boolean readBoolean()
                    throws java.io.IOException
Specified by:
readBoolean in interface java.io.DataInput
Throws:
java.io.IOException

readByte

public byte readByte()
              throws java.io.IOException
Specified by:
readByte in interface java.io.DataInput
Throws:
java.io.IOException

readChar

public char readChar()
              throws java.io.IOException
Specified by:
readChar in interface java.io.DataInput
Throws:
java.io.IOException

readDouble

public double readDouble()
                  throws java.io.IOException
Specified by:
readDouble in interface java.io.DataInput
Throws:
java.io.IOException

readFloat

public float readFloat()
                throws java.io.IOException
Specified by:
readFloat in interface java.io.DataInput
Throws:
java.io.IOException

readFully

public void readFully(byte[] b)
               throws java.io.IOException
Specified by:
readFully in interface java.io.DataInput
Throws:
java.io.IOException

readFully

public void readFully(byte[] b,
                      int off,
                      int len)
               throws java.io.IOException
Specified by:
readFully in interface java.io.DataInput
Throws:
java.io.IOException

readInt

public int readInt()
            throws java.io.IOException
Specified by:
readInt in interface java.io.DataInput
Throws:
java.io.IOException

readLine

public java.lang.String readLine()
                          throws java.io.IOException
Specified by:
readLine in interface java.io.DataInput
Throws:
java.io.IOException

readLong

public long readLong()
              throws java.io.IOException
Specified by:
readLong in interface java.io.DataInput
Throws:
java.io.IOException

readShort

public short readShort()
                throws java.io.IOException
Specified by:
readShort in interface java.io.DataInput
Throws:
java.io.IOException

readUTF

public java.lang.String readUTF()
                         throws java.io.IOException
Specified by:
readUTF in interface java.io.DataInput
Throws:
java.io.IOException

readUnsignedByte

public int readUnsignedByte()
                     throws java.io.IOException
Specified by:
readUnsignedByte in interface java.io.DataInput
Throws:
java.io.IOException

readUnsignedShort

public int readUnsignedShort()
                      throws java.io.IOException
Specified by:
readUnsignedShort in interface java.io.DataInput
Throws:
java.io.IOException

copy

public RandomAccessStorage copy()
                         throws java.io.IOException
Description copied from interface: RandomAccessStorage
Creates a clone, or copy, of this storage, having the exact same contents and the file pointer reset to zero.

Specified by:
copy in interface RandomAccessStorage
Returns:
Throws:
java.io.IOException