SDSU Emerging Technology
Fall Semester, 2004
MIDlet Persistent Storage
Previous     Lecture Notes Index     Next     
© 2004, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 28-Oct-04

CS 683 Emerging Technologies Fall Semester, 2004 Doc 22 MIDlet Persistent Storage

Contents

References

Example

Saving a New Book

Reading a Book by Id

Books By ID

Updating an Existing Book

Deleting

Comparing to Change the Order of an Enumeration

Copyright ©, All rights reserved. 2004 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent ( http://www.opencontent.org/opl.shtml) license defines the copyright on this document.

References

J2ME in a Nutshell, Kim Topley, O’Reilly, 2002, Chapter 6, pp 210-229

Examples in this lecture are based on examples in the above reference

Persistent Storage

RecordStore

Useful Methods

public static RecordStore openRecordStore( 
         String recordStoreName, 
         String vendorName,  
         String suiteName) 
   
public int addRecord(byte[] data,
                int offset,
                int numBytes)
   
public void setRecord(int recordId,
               byte[] newData,
               int offset,
               int numBytes)
   
public byte[] getRecord(int recordId)
   
public void deleteRecord(int recordId)
   
public int getNumRecords()
   
public int getNextRecordID()
   
public RecordEnumeration enumerateRecords(RecordFilter filter,
                           RecordComparator comparator,
                           boolean keepUpdated)

Useful Interfaces

RecordEnumeration

Enumerate through records

Forward and backward enumeration

RecordFilter

Used to select which records read by RecordEnumeration

Has one method - boolean matches(byte[] candidate)

RecordComparator

Used to determine order of records in a RecordEnumeration

Has one method - int compare(byte[] rec1, byte[] rec2)

RecordListener

Used to inform a RecordEnumeration of updates to store

RecordEnumeration updates index

Might have some performance problem

Example

Based on example from Chapter 6 of J2ME in a Nutshell

import java.io.InputStream;
import java.io.IOException;
   
public class BookInfo {
   int   id;
   String isbn;
   String title;
   
   public BookInfo(String isbn) {  this.isbn = isbn; }
   
   public BookInfo(byte[] bytes) {
      DataInputStream is = 
         new DataInputStream(new ByteArrayInputStream(bytes));
   
      isbn = is.readUTF();
      info.title = is.readUTF();
   }
   
   public String getIsbn() { return isbn; }
   
   public String getTitle() {  return title; }
   
   public byte[] toByteArray() throws IOException {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      DataOutputStream os = new DataOutputStream(baos);
   
      os.writeUTF(isbn);
      os.writeUTF(title == null ? "" : title);
   
      return baos.toByteArray();
   }

BookInfo Continued

   public boolean equals(Object aBook) {
      try {
         return getIsbn.equals(((BookInfo) aBook).getIsbn() );
      } catch (Exception notABook ) {
         return false;
      }
   }
   
   public boolean compareTo(BookInfo aBook ) {
      return getTitle().compareTo( aBook.getTitle());
   }
}

BookStore

import java.io.*;
import javax.microedition.rms.*;
   
public class BookStore implements RecordComparator, RecordFilter {
   
   private static final String STORE_NAME = "BookStore";
   
   private RecordStore store;
   private String searchISBN;
   
   public BookStore() {
      try {
         boolean createIfNeeded = true;
         store = 
            RecordStore.openRecordStore(STORE_NAME, createIfNeeded);
      } catch (RecordStoreException ex) {
         System.err.println(ex);
      }
   }
   
   public void close() throws RecordStoreException {
      if (store != null) {
         store.closeRecordStore();
      }
   }
   
   public int getBookCount() throws RecordStoreException {
      if (store != null) {
         return store.getNumRecords();
      }
      return 0;
   }

Saving a New Book

   public void saveNewBook(BookInfo bookInfo)
                        throws IOException, RecordStoreException {
      if (store != null) {
         bookInfo.id = store.getNextRecordID();
         byte[] bytes = bookInfo.toByteArray();
         int bufferOffset = 0;
         store.addRecord(bytes, bufferOffset, bytes.length);
      }
   }

Reading a Book by Id

   public BookInfo getBookInfo(int id) throws RecordStoreException,
                                       IOException {
      byte[] bytes = store.getRecord(id);
      BookInfo info = new BookInfo(bytes);
      info.id = id;
   
      return info;
   }

Books By ID

   public BookInfo getBookInfo(String isbn) 
      throws RecordStoreException, IOException {
   
      BookInfo bookInfo = null;
      searchISBN = isbn; 
      
      RecordFilter isbnFilter = this;
      RecordComparitor defaultSort = null;
      boolean  noUpdates = false;
      RecordEnumeration enum = 
         store.enumerateRecords(isbnFilter, defaultSort, noUpdates);
      
      if (enum.numRecords() > 0) {
         int id = enum.nextRecordId();
         bookInfo = getBookInfo(id);
      }
   
      enum.destroy();
      return bookInfo;
   }
      
   public boolean matches(byte[] bookBytes) {
      if (searchISBN != null) {
         try {
            BookInfo bookInfo = new BookInfo(bookBytes);
   
            return searchISBN.equals(bookInfo.getIsbn());
         } catch (IOException ex) {
            System.err.println(ex);
         }
      }
      return false;
   }

Updating an Existing Book

   public void saveBookInfo(BookInfo bookInfo)
                        throws IOException, RecordStoreException {
      if (store != null) {
         searchISBN = bookInfo.getIsbn();
         RecordFilter isbnFilter = this;
         RecordComparitor defaultSort = null;
         boolean  noUpdates = false;
   
         RecordEnumeration enum = 
            store.enumerateRecords(isbnFilter, defaultSort, noUpdates);
   
         if (enum.numRecords() > 0) {
            bookInfo.id = enum.nextRecordId();
            byte[] bytes = bookInfo.toByteArray();
            store.setRecord(bookInfo.id, bytes, 0, bytes.length);
         } else {
            saveNewBook(bookInfo);
         }
         enum.destroy();
      }
   }

Deleting

   public void deleteBook(BookInfo bookInfo) throws RecordStoreException {
      if (store != null) {
         store.deleteRecord(bookInfo.id);
      }
   }

Comparing to Change the Order of an Enumeration

   public int compare(byte[] book1Bytes, byte[] book2Bytes) {
      try {
         BookInfo book1 = new BookInfo(book1Bytes);
         BookInfo book2 = new BookInfo(book2Bytes);
   
         if (book1.equals(book2)) {
            return RecordComparator.EQUIVALENT;
         }
         int result = book1.compareTo(book2);
         if (result == 0) {
            return RecordComparator.EQUIVALENT;
         }
         return result < 0 ? 
            RecordComparator.PRECEDES : RecordComparator.FOLLOWS;
      } catch (IOException ex) {
         return RecordComparator.EQUIVALENT;
      }
   }
   
   public RecordEnumeration getBooks() throws RecordStoreException {
      if (store != null) {
         return store.enumerateRecords(null, this, false);
      }
      return null;
   }

Previous     visitors since 28-Oct-04     Next