RAFo

RandomAccessFile for Objects

com.linxpda.raf
Class ObjectFile

java.lang.Object
  |
  +--com.linxpda.raf.RaFile
        |
        +--com.linxpda.raf.ObjectFile

public class ObjectFile
extends com.linxpda.raf.RaFile

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 {
				ObjectFile file = new ObjectFile(filename, 64);
				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
ObjectFile(java.lang.String filename, int initialSize)
          Creates a new data file.
ObjectFile(java.lang.String filename, java.lang.String access)
          Opens an existing database and provides access restrictions as per the access parameter.
 
Method Summary
 void close()
           
 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 object)
          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 object)
          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

ObjectFile

public ObjectFile(java.lang.String filename,
                  int initialSize)
           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.

ObjectFile

public ObjectFile(java.lang.String filename,
                  java.lang.String access)
           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 object)
                  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 object)
                  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,
                                  RAFException,
                                  java.lang.ClassNotFoundException
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

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