Program: DBM

This program lets you use the original Unix DBM (database access method) routines from Java. DBM is actually emulated using the newer Berkeley Database (DB) routines, but the DBM interface is more traditional, and simpler. DBM was used in Section 20.3 to provide a name-to-login database, which is similar to how many modern Unixes actually store name and password information. That recipe also showed how to use it to display your Netscape history, even under MS-Windows.

I’ll now show the Java version of the DBM library, DBM.java (see Example 26-10).

Example 26-10. DBM.java

import java.io.*; /** 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). * <P>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. */ public class DBM { /** 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 static boolean inuse = false; /** Save the filename for messages, etc. */ protected String fileName; /** Construct a DBM given its filename */ public DBM(String file) { synchronized(this) { if (inuse) throw new IllegalArgumentException( "Only one DBM object at a time per Java Machine"); inuse = true; ...

Get Java Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.