Slide 4.7: RecordMIDlet.java
  Slide 4.9: Line-by-line anatomy of RecordStore project
  Home


Preferences.java


All the RecordStore is encapsulated in the Preferences class. Preferences is a wrapper for a user name and password, stored as String member variables mUser and mPassword. A static method, get( ), provides access to a single Preferences object. Each time get( ) is called, the user name and password are loaded from a RecordStore.

 Preferences.java 

import  java.util.*;
import  javax.microedition.lcdui.*;
import  javax.microedition.rms.*;

public class  Preferences {
  private String     mRecordStoreName;
  private Hashtable  mHashtable;
  
  public  Preferences( String recordStoreName )
      throws  RecordStoreException {
    mRecordStoreName = recordStoreName;
    mHashtable       = new Hashtable( );
    load( );
  }
  
  public String  get( String key ) {
    return  (String) mHashtable.get( key );
  }
  
  public void  put( String key, String value ) {
    if ( value == null )  value = "";
    mHashtable.put( key, value );
  }
  
  private void  load( )  throws  RecordStoreException {
    RecordStore        rs = null;
    RecordEnumeration  re = null;

    try {
      rs = RecordStore.openRecordStore( mRecordStoreName, true );
      re = rs.enumerateRecords( null, null, false );
      while ( re.hasNextElement( ) ) {
        byte[ ]  raw  = re.nextRecord( );
        String  pref  = new String( raw );
        // Parse out the name.
        int     index = pref.indexOf( '|' );
        String  name  = pref.substring( 0, index );
        String  value = pref.substring( index + 1 );
        put( name, value );
      }
    }
    finally {
      if ( re != null )  re.destroy( );
      if ( rs != null )  rs.closeRecordStore( );
    }
  }
  
  public void  save( )  throws  RecordStoreException {
    RecordStore        rs = null;
    RecordEnumeration  re = null;
    try {
      rs = RecordStore.openRecordStore( mRecordStoreName, true );
      re = rs.enumerateRecords( null, null, false );
      
      // First remove all records, a little clumsy.
      while ( re.hasNextElement( ) ) {
        int  id = re.nextRecordId( );
        rs.deleteRecord( id );
      }
      
      // Now save the preferences records.
      Enumeration  keys = mHashtable.keys( );
      while ( keys.hasMoreElements( ) ) {
        String   key   = (String) keys.nextElement( );
        String   value = get( key );
        String   pref  = key + "|" + value;
        byte[ ]  raw   = pref.getBytes( );
        rs.addRecord( raw, 0, raw.length );
      }
    }
    finally {
      if ( re != null )  re.destroy( );
      if ( rs != null )  rs.closeRecordStore( );
    }
  }
}