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

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

public class CachedRandomAccessFile
extends ManagedRandomAccessFile

This class implements the interface of ManagedRandomAccessFile, providing high-level type read and write operations on a managed random access file.

On top of such managed random access file, this class provides a transparent caching layer, which is supposed to speed up read operations by limiting actual calls to the low-level I/O layer.

Caching is performed read-only, which has, in its current implementation, the following implications:

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

Nested Class Summary
 
Nested classes/interfaces inherited from class org.processmining.framework.log.rfb.io.ManagedRandomAccessFile
ManagedRandomAccessFile.ShutdownHook
 
Field Summary
protected  java.nio.ByteBuffer buffer
          Byte buffer which abstracts from the cache for read operations of basic Java types.
protected  byte[] cache
          Cache for underlying file data.
protected  boolean cacheDirty
          Indicates, whether the cache has been invalidated (e.g.
protected  long cacheOffset
          Describes the offset within the cached file, which is represented by the first byte currently in the cache.
protected  int cacheSize
          Size of the cache, in bytes.
protected  int cacheValidSize
          Describes the remaining valid bytes in the cache.
static int DEFAULT_CACHE_SIZE
          Defines the default cache size, which is used when no value is provided in the constructor.
 
Fields inherited from class org.processmining.framework.log.rfb.io.ManagedRandomAccessFile
currentFilePointer, file, isOpen, maxOpenFiles, openFilesCounter, openFilesList, raf, TEMP_FILE_PREFIX, TEMP_FILE_SUFFIX
 
Constructor Summary
CachedRandomAccessFile()
          Creates a new instance caching the default amount of bytes in heap memory.
CachedRandomAccessFile(CachedRandomAccessFile template)
          Creates a new instance which is a clone of the provided template instance.
CachedRandomAccessFile(int cacheSize)
          Creates a new instance caching the given amount of bytes in heap memory.
 
Method Summary
protected  java.nio.ByteBuffer buffer(int size)
          Retrieves a ByteBuffer, which can read the given number of bytes from the current (virtual) file pointer position.
protected  void cache(int size)
          Ensures that the given amount of bytes from the current file position are available from the cache.
 void close()
          Closes this instance virtually (flushes and releases the managed file)
 RandomAccessStorage copy()
          Creates a clone, or copy, of this storage, having the exact same contents and the file pointer reset to zero.
 boolean readBoolean()
          Wrapped method from DataInput interface.
 byte readByte()
          Wrapped method from DataInput interface.
 char readChar()
          Wrapped method from DataInput interface.
 double readDouble()
          Wrapped method from DataInput interface.
 float readFloat()
          Wrapped method from DataInput interface.
 void readFully(byte[] arr)
          Wrapped method from DataInput interface.
 void readFully(byte[] arg0, int offset, int length)
          Wrapped method from DataInput interface.
 int readInt()
          Wrapped method from DataInput interface.
 java.lang.String readLine()
          Wrapped method from DataInput interface.
 long readLong()
          Wrapped method from DataInput interface.
 short readShort()
          Wrapped method from DataInput interface.
 int readUnsignedByte()
          Wrapped method from DataInput interface.
 int readUnsignedShort()
          Wrapped method from DataInput interface.
 java.lang.String readUTF()
          Warning: custom implementation will expect different low-level byte encoding than e.g.
 int skipBytes(int arg0)
          Wrapped method from DataInput interface.
 void write(byte[] arg)
          Wrapped method from DataOutput interface.
 void write(byte[] arg, int arg1, int arg2)
          Wrapped method from DataOutput interface.
 void write(int val)
          Wrapped method from DataOutput interface.
 void writeBoolean(boolean bool)
          Wrapped method from DataOutput interface.
 void writeByte(int arg0)
          Wrapped method from DataOutput interface.
 void writeBytes(java.lang.String arg0)
          Wrapped method from DataOutput interface.
 void writeChar(int arg0)
          Wrapped method from DataOutput interface.
 void writeChars(java.lang.String arg0)
          Wrapped method from DataOutput interface.
 void writeDouble(double arg0)
          Wrapped method from DataOutput interface.
 void writeFloat(float arg0)
          Wrapped method from DataOutput interface.
 void writeInt(int arg0)
          Wrapped method from DataOutput interface.
 void writeLong(long arg0)
          Wrapped method from DataOutput interface.
 void writeShort(int arg0)
          Wrapped method from DataOutput interface.
 void writeUTF(java.lang.String arg0)
          Warning: This method uses a custom format to encode UTF-8 strings to bytes than specified in DataOutput.
 
Methods inherited from class org.processmining.framework.log.rfb.io.ManagedRandomAccessFile
closeHandle, copyFile, createTempFile, delete, deleteOnExit, finalize, getFilePointer, length, raf, releaseOpenFileSlot, reOpen, retrieveOpenFileSlot, seek
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_CACHE_SIZE

public static int DEFAULT_CACHE_SIZE
Defines the default cache size, which is used when no value is provided in the constructor.


cacheSize

protected int cacheSize
Size of the cache, in bytes.


cacheValidSize

protected int cacheValidSize
Describes the remaining valid bytes in the cache.


cacheOffset

protected long cacheOffset
Describes the offset within the cached file, which is represented by the first byte currently in the cache.


cache

protected byte[] cache
Cache for underlying file data.


cacheDirty

protected boolean cacheDirty
Indicates, whether the cache has been invalidated (e.g. by a write operation to the underlying file).


buffer

protected java.nio.ByteBuffer buffer
Byte buffer which abstracts from the cache for read operations of basic Java types.

Constructor Detail

CachedRandomAccessFile

public CachedRandomAccessFile(int cacheSize)
                       throws java.io.IOException
Creates a new instance caching the given amount of bytes in heap memory.

Parameters:
cacheSize - Size of the memory cache, in bytes.
Throws:
java.io.IOException

CachedRandomAccessFile

public CachedRandomAccessFile()
                       throws java.io.IOException
Creates a new instance caching the default amount of bytes in heap memory.

Throws:
java.io.IOException

CachedRandomAccessFile

public CachedRandomAccessFile(CachedRandomAccessFile template)
                       throws java.io.IOException
Creates a new instance which is a clone of the provided template instance. The new instance will contain an exact copy of the provided template from the beginning.

Changes are defined to be not synchronized between template and clone, i.e. the created clone will behave as a truly separate and independent instance after creation.

Parameters:
template - Template to create a clone from.
Throws:
java.io.IOException
Method Detail

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
Overrides:
copy in class ManagedRandomAccessFile
Returns:
Throws:
java.io.IOException

cache

protected void cache(int size)
              throws java.io.IOException
Ensures that the given amount of bytes from the current file position are available from the cache. This implies potentially loading data from the managed file into the cache and ajusting the buffer position.

Parameters:
size - Number of bytes to be cached, referred from the current (virtual) file pointer position.

Notice: It is assumed that the given number of bytes is between zero and the cache size. This property must be ensured outside of this method!

Throws:
java.io.IOException

buffer

protected java.nio.ByteBuffer buffer(int size)
                              throws java.io.IOException
Retrieves a ByteBuffer, which can read the given number of bytes from the current (virtual) file pointer position. This method will ensure that caching will be performed where necessary. Where caching is not possible, due to the size of the requested area exceeding the cache size, the given number of bytes are directly fetched and wrapped in a byte buffer. Notice that this will affect performance significantly and should not happen too frequently (as it invalidates the cache).

Parameters:
size - Number of bytes, that are to be read from the provided byte buffer.

Warning: This method relies on the correctness of this value, which must be absolute and correct; i.e. this is not an upper bound, but must be exactly the number of bytes actually read from the byte buffer. If this is not ensured, the internal state of this instance will become inconsistend, rendering future read operation results unpredictable.

Returns:
A byte buffer to read the requested number of bytes from.
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Description copied from class: ManagedRandomAccessFile
Closes this instance virtually (flushes and releases the managed file)

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

readBoolean

public boolean readBoolean()
                    throws java.io.IOException
Description copied from class: ManagedRandomAccessFile
Wrapped method from DataInput interface.

Preserves the internal state of the managed proxy instance.

Specified by:
readBoolean in interface java.io.DataInput
Overrides:
readBoolean in class ManagedRandomAccessFile
Throws:
java.io.IOException
See Also:
DataInput

readByte

public byte readByte()
              throws java.io.IOException
Description copied from class: ManagedRandomAccessFile
Wrapped method from DataInput interface.

Preserves the internal state of the managed proxy instance.

Specified by:
readByte in interface java.io.DataInput
Overrides:
readByte in class ManagedRandomAccessFile
Throws:
java.io.IOException
See Also:
DataInput

readChar

public char readChar()
              throws java.io.IOException
Description copied from class: ManagedRandomAccessFile
Wrapped method from DataInput interface.

Preserves the internal state of the managed proxy instance.

Specified by:
readChar in interface java.io.DataInput
Overrides:
readChar in class ManagedRandomAccessFile
Throws:
java.io.IOException
See Also:
DataInput

readDouble

public double readDouble()
                  throws java.io.IOException
Description copied from class: ManagedRandomAccessFile
Wrapped method from DataInput interface.

Preserves the internal state of the managed proxy instance.

Specified by:
readDouble in interface java.io.DataInput
Overrides:
readDouble in class ManagedRandomAccessFile
Throws:
java.io.IOException
See Also:
DataInput

readFloat

public float readFloat()
                throws java.io.IOException
Description copied from class: ManagedRandomAccessFile
Wrapped method from DataInput interface.

Preserves the internal state of the managed proxy instance.

Specified by:
readFloat in interface java.io.DataInput
Overrides:
readFloat in class ManagedRandomAccessFile
Throws:
java.io.IOException
See Also:
DataInput

readFully

public void readFully(byte[] arg0,
                      int offset,
                      int length)
               throws java.io.IOException
Description copied from class: ManagedRandomAccessFile
Wrapped method from DataInput interface.

Preserves the internal state of the managed proxy instance.

Specified by:
readFully in interface java.io.DataInput
Overrides:
readFully in class ManagedRandomAccessFile
Throws:
java.io.IOException
See Also:
DataInput

readFully

public void readFully(byte[] arr)
               throws java.io.IOException
Description copied from class: ManagedRandomAccessFile
Wrapped method from DataInput interface.

Preserves the internal state of the managed proxy instance.

Specified by:
readFully in interface java.io.DataInput
Overrides:
readFully in class ManagedRandomAccessFile
Throws:
java.io.IOException
See Also:
DataInput

readInt

public int readInt()
            throws java.io.IOException
Description copied from class: ManagedRandomAccessFile
Wrapped method from DataInput interface.

Preserves the internal state of the managed proxy instance.

Specified by:
readInt in interface java.io.DataInput
Overrides:
readInt in class ManagedRandomAccessFile
Throws:
java.io.IOException
See Also:
DataInput

readLine

public java.lang.String readLine()
                          throws java.io.IOException
Description copied from class: ManagedRandomAccessFile
Wrapped method from DataInput interface.

Preserves the internal state of the managed proxy instance.

Specified by:
readLine in interface java.io.DataInput
Overrides:
readLine in class ManagedRandomAccessFile
Throws:
java.io.IOException
See Also:
DataInput

readLong

public long readLong()
              throws java.io.IOException
Description copied from class: ManagedRandomAccessFile
Wrapped method from DataInput interface.

Preserves the internal state of the managed proxy instance.

Specified by:
readLong in interface java.io.DataInput
Overrides:
readLong in class ManagedRandomAccessFile
Throws:
java.io.IOException
See Also:
DataInput

readShort

public short readShort()
                throws java.io.IOException
Description copied from class: ManagedRandomAccessFile
Wrapped method from DataInput interface.

Preserves the internal state of the managed proxy instance.

Specified by:
readShort in interface java.io.DataInput
Overrides:
readShort in class ManagedRandomAccessFile
Throws:
java.io.IOException
See Also:
DataInput

readUnsignedByte

public int readUnsignedByte()
                     throws java.io.IOException
Description copied from class: ManagedRandomAccessFile
Wrapped method from DataInput interface.

Preserves the internal state of the managed proxy instance.

Specified by:
readUnsignedByte in interface java.io.DataInput
Overrides:
readUnsignedByte in class ManagedRandomAccessFile
Throws:
java.io.IOException
See Also:
DataInput

readUnsignedShort

public int readUnsignedShort()
                      throws java.io.IOException
Description copied from class: ManagedRandomAccessFile
Wrapped method from DataInput interface.

Preserves the internal state of the managed proxy instance.

Specified by:
readUnsignedShort in interface java.io.DataInput
Overrides:
readUnsignedShort in class ManagedRandomAccessFile
Throws:
java.io.IOException
See Also:
DataInput

readUTF

public java.lang.String readUTF()
                         throws java.io.IOException
Description copied from class: ManagedRandomAccessFile
Warning: custom implementation will expect different low-level byte encoding than e.g. the identical method from RandomAccessFile!

Preserves the internal state of the managed proxy instance.

Specified by:
readUTF in interface java.io.DataInput
Overrides:
readUTF in class ManagedRandomAccessFile
Throws:
java.io.IOException
See Also:
DataInput

skipBytes

public int skipBytes(int arg0)
              throws java.io.IOException
Description copied from class: ManagedRandomAccessFile
Wrapped method from DataInput interface.

Preserves the internal state of the managed proxy instance.

Specified by:
skipBytes in interface java.io.DataInput
Specified by:
skipBytes in interface RandomAccessStorage
Overrides:
skipBytes in class ManagedRandomAccessFile
Throws:
java.io.IOException
See Also:
DataInput

write

public void write(byte[] arg,
                  int arg1,
                  int arg2)
           throws java.io.IOException
Description copied from class: ManagedRandomAccessFile
Wrapped method from DataOutput interface.

Preserves the internal state of the managed proxy instance.

Specified by:
write in interface java.io.DataOutput
Overrides:
write in class ManagedRandomAccessFile
Throws:
java.io.IOException
See Also:
DataOutput

write

public void write(byte[] arg)
           throws java.io.IOException
Description copied from class: ManagedRandomAccessFile
Wrapped method from DataOutput interface.

Preserves the internal state of the managed proxy instance.

Specified by:
write in interface java.io.DataOutput
Overrides:
write in class ManagedRandomAccessFile
Throws:
java.io.IOException
See Also:
DataOutput

write

public void write(int val)
           throws java.io.IOException
Description copied from class: ManagedRandomAccessFile
Wrapped method from DataOutput interface.

Preserves the internal state of the managed proxy instance.

Specified by:
write in interface java.io.DataOutput
Overrides:
write in class ManagedRandomAccessFile
Throws:
java.io.IOException
See Also:
DataOutput

writeBoolean

public void writeBoolean(boolean bool)
                  throws java.io.IOException
Description copied from class: ManagedRandomAccessFile
Wrapped method from DataOutput interface.

Preserves the internal state of the managed proxy instance.

Specified by:
writeBoolean in interface java.io.DataOutput
Overrides:
writeBoolean in class ManagedRandomAccessFile
Throws:
java.io.IOException
See Also:
DataOutput

writeByte

public void writeByte(int arg0)
               throws java.io.IOException
Description copied from class: ManagedRandomAccessFile
Wrapped method from DataOutput interface.

Preserves the internal state of the managed proxy instance.

Specified by:
writeByte in interface java.io.DataOutput
Overrides:
writeByte in class ManagedRandomAccessFile
Throws:
java.io.IOException
See Also:
DataOutput

writeBytes

public void writeBytes(java.lang.String arg0)
                throws java.io.IOException
Description copied from class: ManagedRandomAccessFile
Wrapped method from DataOutput interface.

Preserves the internal state of the managed proxy instance.

Specified by:
writeBytes in interface java.io.DataOutput
Overrides:
writeBytes in class ManagedRandomAccessFile
Throws:
java.io.IOException
See Also:
DataOutput

writeChar

public void writeChar(int arg0)
               throws java.io.IOException
Description copied from class: ManagedRandomAccessFile
Wrapped method from DataOutput interface.

Preserves the internal state of the managed proxy instance.

Specified by:
writeChar in interface java.io.DataOutput
Overrides:
writeChar in class ManagedRandomAccessFile
Throws:
java.io.IOException
See Also:
DataOutput

writeChars

public void writeChars(java.lang.String arg0)
                throws java.io.IOException
Description copied from class: ManagedRandomAccessFile
Wrapped method from DataOutput interface.

Preserves the internal state of the managed proxy instance.

Specified by:
writeChars in interface java.io.DataOutput
Overrides:
writeChars in class ManagedRandomAccessFile
Throws:
java.io.IOException
See Also:
DataOutput

writeDouble

public void writeDouble(double arg0)
                 throws java.io.IOException
Description copied from class: ManagedRandomAccessFile
Wrapped method from DataOutput interface.

Preserves the internal state of the managed proxy instance.

Specified by:
writeDouble in interface java.io.DataOutput
Overrides:
writeDouble in class ManagedRandomAccessFile
Throws:
java.io.IOException
See Also:
DataOutput

writeFloat

public void writeFloat(float arg0)
                throws java.io.IOException
Description copied from class: ManagedRandomAccessFile
Wrapped method from DataOutput interface.

Preserves the internal state of the managed proxy instance.

Specified by:
writeFloat in interface java.io.DataOutput
Overrides:
writeFloat in class ManagedRandomAccessFile
Throws:
java.io.IOException
See Also:
DataOutput

writeInt

public void writeInt(int arg0)
              throws java.io.IOException
Description copied from class: ManagedRandomAccessFile
Wrapped method from DataOutput interface.

Preserves the internal state of the managed proxy instance.

Specified by:
writeInt in interface java.io.DataOutput
Overrides:
writeInt in class ManagedRandomAccessFile
Throws:
java.io.IOException
See Also:
DataOutput

writeLong

public void writeLong(long arg0)
               throws java.io.IOException
Description copied from class: ManagedRandomAccessFile
Wrapped method from DataOutput interface.

Preserves the internal state of the managed proxy instance.

Specified by:
writeLong in interface java.io.DataOutput
Overrides:
writeLong in class ManagedRandomAccessFile
Throws:
java.io.IOException
See Also:
DataOutput

writeShort

public void writeShort(int arg0)
                throws java.io.IOException
Description copied from class: ManagedRandomAccessFile
Wrapped method from DataOutput interface.

Preserves the internal state of the managed proxy instance.

Specified by:
writeShort in interface java.io.DataOutput
Overrides:
writeShort in class ManagedRandomAccessFile
Throws:
java.io.IOException
See Also:
DataOutput

writeUTF

public void writeUTF(java.lang.String arg0)
              throws java.io.IOException
Description copied from class: ManagedRandomAccessFile
Warning: This method uses a custom format to encode UTF-8 strings to bytes than specified in DataOutput.

Preserves the internal state of the managed proxy instance.

Specified by:
writeUTF in interface java.io.DataOutput
Overrides:
writeUTF in class ManagedRandomAccessFile
Throws:
java.io.IOException
See Also:
DataOutput