RAFo

RandomAccessFile for Objects

com.linxpda.raf
Class RAFo

java.lang.Object
  |
  +--com.linxpda.raf.RAFo

public class RAFo
extends java.lang.Object

This class reads and writes Java™ Objects to a local file, using String keys to reference objects stored. It maintains an in-memory index of file keys for faster reading and writing operations.

A typical ObjectFile session would go:

 import com.linxpda.raf.*;
	import java.util.Date;
	
	public class FileTest {
		
		public FileTest(String filename) {
			try {
				RAFo file = new RAFo(filename);
				Date data = new Date();
				file.insertObject("created", date);
				file.insertObject("created-by", "Tom Cole");
				//update a record
				file.updateObject("created-by", "Sid Thomas");
				//delete a record
				file.removeObject("created-by");
				Date date = (Date) file.getObject("created");
				System.out.println("File created: " + date.toGMTString());
				file.close();
			}
			catch(IOException ioe) {
				ioe.printStackTrace();
			}
			catch(RAFException dbe) {
				dbe.printStackTrace();
			}
			catch(ClassNotFoundException cnfe) {
				cbfe.printStackTrace();
			}
			finally {
				System.exit(0);
			}
		}
		
		public static void main(String args[]) {
			if (args.length != 1) {
				System.out.println("USEAGE: java FileTest ");
				System.exit(0);
			}
			else {

				new FileTest(args[0]);
			}
		}
	}	


Constructor Summary
RAFo(java.lang.String filename)
          Convenience constructor that attempts to open an existing file with the given filename, if that fails then a new file is created.
RAFo(java.lang.String filename, int initialCapacity)
          Creates a new data file.
RAFo(java.lang.String filename, java.lang.String accessFlags)
          Opens an existing database and provides access restrictions as per the access parameter.
 
Method Summary
 void close()
          Closes the data file and removes the header index from memory.
 boolean containsKey(java.lang.String key)
          Denotes whether the specified String key points to an object in the file.
 java.util.Enumeration getKeys()
          Returns an enumeration of all the keys currently referencing objects in the file.
 java.lang.Object getObject(java.lang.String key)
          Returns the object in the file referenced by the supplied key.
 int getObjectCount()
          Returns the total number of objects currently stored in the file.
 void insertObject(java.lang.String key, java.lang.Object value)
          Add the specified to the file, referencing it with the supplied key.
 void removeObject(java.lang.String key)
          Removes the object referenced by key from the file.
 void updateObject(java.lang.String key, java.lang.Object value)
          Replaces the object referred to by key with the new object specified.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RAFo

public RAFo(java.lang.String filename)
     throws java.io.IOException,
            RAFException
Convenience constructor that attempts to open an existing file with the given filename, if that fails then a new file is created.
Parameters:
filename - The filename of the file to open or create.
Throws:
java.io.IOException - upon a system level access violation
RAFException - if unable to open or create the file

RAFo

public RAFo(java.lang.String filename,
            int initialCapacity)
     throws java.io.IOException,
            RAFException
Creates a new data file.
Parameters:
filename - Filename for the new data file.
initialSize - The amount of space allocated for the index header. May increase/decrease as needed. Provided for efficiency.
Throws:
java.io.IOException - upon receipt of a file writing error.
RAFException - if the specified file already exists.

RAFo

public RAFo(java.lang.String filename,
            java.lang.String accessFlags)
     throws java.io.IOException,
            RAFException
Opens an existing database and provides access restrictions as per the access parameter.
Parameters:
filename - The data file to open.
access - Access flags denoting file permissions. May be 'r' for read-only, 'w' for write-only or 'rw' for read & write permissions.
Throws:
java.io.IOException - upon receipt of a file I/O error.
RAFException - if the specified file does not exist.
Method Detail

getKeys

public java.util.Enumeration getKeys()
Returns an enumeration of all the keys currently referencing objects in the file.

getObjectCount

public int getObjectCount()
Returns the total number of objects currently stored in the file.
Returns:
The number of objects in the datafile.

containsKey

public boolean containsKey(java.lang.String key)
Denotes whether the specified String key points to an object in the file.
Parameters:
key - The String key to look for.
Returns:
true if the file contains the key 'key', false if it does not.

insertObject

public void insertObject(java.lang.String key,
                         java.lang.Object value)
                  throws java.io.IOException,
                         RAFException
Add the specified to the file, referencing it with the supplied key.
Parameters:
key - The string key to use for referencing the object.
object - The object to store.
Throws:
java.io.IOException - upon a file I/O error.
RAFException - Upon a serialization or file access error.

updateObject

public void updateObject(java.lang.String key,
                         java.lang.Object value)
                  throws java.io.IOException,
                         RAFException
Replaces the object referred to by key with the new object specified.
Parameters:
key - The key for the object to replace.
object - The new object to store.
Throws:
java.io.IOException - upon a file I/O error.
RAFException - upon a file access or serialization error.

removeObject

public void removeObject(java.lang.String key)
                  throws java.io.IOException,
                         RAFException
Removes the object referenced by key from the file.
Parameters:
key - The key for the object to delete.
Throws:
java.io.IOException - upon a file I/O error.
RAFException - upon a file access violation.

getObject

public java.lang.Object getObject(java.lang.String key)
                           throws java.io.IOException,
                                  java.lang.ClassNotFoundException,
                                  RAFException
Returns the object in the file referenced by the supplied key.
Parameters:
key - The key for the file to retrieve.
Returns:
An instance of object referenced by the supplied key.
Throws:
java.io.IOException - upon a file I/O error.
RAFException - if the specified key does not exist, or if a file access violation has occured.
java.lang.ClassNotFoundException - if the class file for the read file cannot be found.

close

public void close()
           throws java.io.IOException,
                  RAFException
Closes the data file and removes the header index from memory.

(c)2001 Linxpda, Inc. www.linxpda.com