|
Emerging Technology
Fall Semester, 2004 MIDlet Persistent Storage |
|
|---|---|---|
|
© 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
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.
| CS 683 Fall 04 | Doc 22, MIDlet Persistent Storage Slide # 2 |
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
| CS 683 Fall 04 | Doc 22, MIDlet Persistent Storage Slide # 3 |
| CS 683 Fall 04 | Doc 22, MIDlet Persistent Storage Slide # 4 |
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)
| CS 683 Fall 04 | Doc 22, MIDlet Persistent Storage Slide # 5 |
Enumerate through records
Forward and backward enumeration
Used to select which records read by RecordEnumeration
Has one method - boolean matches(byte[] candidate)
Used to determine order of records in a RecordEnumeration
Has one method - int compare(byte[] rec1, byte[] rec2)
Used to inform a RecordEnumeration of updates to store
RecordEnumeration updates index
Might have some performance problem
| CS 683 Fall 04 | Doc 22, MIDlet Persistent Storage Slide # 6 |
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();
}
| CS 683 Fall 04 | Doc 22, MIDlet Persistent Storage Slide # 7 |
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());
}
}
| CS 683 Fall 04 | Doc 22, MIDlet Persistent Storage Slide # 8 |
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;
}
| CS 683 Fall 04 | Doc 22, MIDlet Persistent Storage Slide # 9 |
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);
}
}
| CS 683 Fall 04 | Doc 22, MIDlet Persistent Storage Slide # 10 |
public BookInfo getBookInfo(int id) throws RecordStoreException,
IOException {
byte[] bytes = store.getRecord(id);
BookInfo info = new BookInfo(bytes);
info.id = id;
return info;
}
| CS 683 Fall 04 | Doc 22, MIDlet Persistent Storage Slide # 11 |
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;
}
| CS 683 Fall 04 | Doc 22, MIDlet Persistent Storage Slide # 12 |
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();
}
}
| CS 683 Fall 04 | Doc 22, MIDlet Persistent Storage Slide # 13 |
public void deleteBook(BookInfo bookInfo) throws RecordStoreException {
if (store != null) {
store.deleteRecord(bookInfo.id);
}
}
| CS 683 Fall 04 | Doc 22, MIDlet Persistent Storage Slide # 14 |
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;
}
Copyright ©, All rights reserved.
2004 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.