FileDocCategorySizeDatePackage
DBM.javaAPI DocExample3702Sat Jun 24 20:10:14 BST 2000None

DBM

public class DBM extends Object
This class provides a dbm-compatible interface to the UNIX-style database access methods described in dbm(3) (which is on some UNIXes a front-end to db(3).

Each unique record in the database is a unique key/value pair, similar to a java.util.Hashtable but stored on persistent medium, not kept in memory. Dbm was originally optimized for UNIX for fast access to individual key/value pairs.

author
This Java/C hookup by Ian F. Darwin, ian@darwinsys.com
version
$Id: DBM.java,v 1.12 2000/06/24 23:10:14 ian Exp $

Fields Summary
protected static boolean
inuse
Since you can only have one DBM database in use at a time due to implementation restrictions, we enforce this rule with a class-wide boolean.
protected String
fileName
Save the filename for messages, etc.
protected ByteArrayOutputStream
bo
Constructors Summary
public DBM(String file)
Construct a DBM given its filename


	       
	   
		synchronized(this) {
			if (inuse)
				throw new IllegalArgumentException(
					"Only one DBM object at a time per Java Machine");
			inuse = true;
		}
		fileName = file;
		int retCode = dbminit(fileName);
		if (retCode < 0)
			throw new IllegalArgumentException(
				"dbminit failed, code = " + retCode);
	
Methods Summary
protected voidcheckInUse()

		if (!inuse)
			throw new IllegalStateException("Method called when DBM not open");
	
public voidclose()
Public wrapper for close method.

		this.dbmclose();
		inuse = false;
	
protected native intdbmclose()

protected native byte[]dbmfetch(byte[] key)

protected native intdbminit(java.lang.String file)

protected native intdbmstore(byte[] key, byte[] content)

protected native intdelete(java.lang.Object key)

public java.lang.Objectfetch(java.lang.Object key)
Fetch using Objects

		checkInUse();
		byte[] datum = dbmfetch(toByteArray(key));
		return toObject(datum);
	
public byte[]fetch(byte[] key)
Fetch using byte arrays

		checkInUse();
		return dbmfetch(key);
	
public native byte[]firstkey()

public java.lang.ObjectfirstkeyObject()

		return toObject(firstkey());
	
public native byte[]nextkey(byte[] key)

public java.lang.Objectnextkey(java.lang.Object key)

		byte[] ba = nextkey(toByteArray(key));
		if (ba == null)
			return null;
		return toObject(ba);
	
public voidstore(byte[] key, byte[] value)
Store using byte arrays

		checkInUse();
		dbmstore(key, value);
	
public voidstore(java.lang.Object key, java.lang.Object value)
Store using Objects

		checkInUse();
		dbmstore(toByteArray(key), toByteArray(value));
	
protected byte[]toByteArray(java.lang.Object o)
serialize an Object to byte array.

		if (bo == null)
			bo = new ByteArrayOutputStream(1024);
		bo.reset();
		ObjectOutputStream os = new ObjectOutputStream(bo);
		os.writeObject(o);
		os.close();
		return bo.toByteArray();
	
protected java.lang.ObjecttoObject(byte[] b)
un-serialize an Object from a byte array.

		Object o;

		ByteArrayInputStream bi = new ByteArrayInputStream(b);
		ObjectInputStream os = new ObjectInputStream(bi);
		try {
			o = os.readObject();
		} catch (ClassNotFoundException ex) {
			// Convert ClassNotFoundException to I/O error
			throw new IOException(ex.getMessage());
		}
		os.close();
		return o;
	
public java.lang.StringtoString()

		return "DBM@" + hashCode() + "[" + fileName + "]";