Methods Summary |
---|
public void | addRecordListener(javax.microedition.rms.RecordListener l)
if (store != null) {
store.addRecordListener(l);
}
|
public void | close()
if (store != null) {
store.closeRecordStore();
}
|
public int | compare(byte[] book1, byte[] book2)
try {
DataInputStream stream1 =
new DataInputStream(new ByteArrayInputStream(book1));
DataInputStream stream2 =
new DataInputStream(new ByteArrayInputStream(book2));
// Match based on the ISBN, but sort based on the title.
String isbn1 = stream1.readUTF();
String isbn2 = stream2.readUTF();
if (isbn1.equals(isbn2)) {
return RecordComparator.EQUIVALENT;
}
String title1 = stream1.readUTF();
String title2 = stream2.readUTF();
int result = title1.compareTo(title2);
if (result == 0) {
return RecordComparator.EQUIVALENT;
}
return result < 0 ? RecordComparator.PRECEDES :
RecordComparator.FOLLOWS;
} catch (IOException ex) {
return RecordComparator.EQUIVALENT;
}
|
public void | deleteBook(BookInfo bookInfo)
if (store != null) {
store.deleteRecord(bookInfo.id);
}
|
public int | getBookCount()
if (store != null) {
return store.getNumRecords();
}
return 0;
|
public BookInfo | getBookInfo(java.lang.String isbn)
BookInfo bookInfo = null;
searchISBN = isbn;
// Look for a book with the given ISBN
RecordEnumeration enum = store.enumerateRecords(
this, null, false);
// If found, get its identifier and
// fetch its BookInfo object
if (enum.numRecords() > 0) {
int id = enum.nextRecordId();
bookInfo = getBookInfo(id);
}
// Release the enumeration
enum.destroy();
return bookInfo;
|
public BookInfo | getBookInfo(int id)
byte[] bytes = store.getRecord(id);
DataInputStream is = new DataInputStream(
new ByteArrayInputStream(bytes));
String isbn = is.readUTF();
BookInfo info = new BookInfo(isbn);
info.id = id;
info.title = is.readUTF();
info.ranking = is.readInt();
info.reviews = is.readInt();
info.lastRanking = is.readInt();
info.lastReviews = is.readInt();
return info;
|
public javax.microedition.rms.RecordEnumeration | getBooks()
if (store != null) {
return store.enumerateRecords(null, this, false);
}
return null;
|
public boolean | matches(byte[] book)
if (searchISBN != null) {
try {
DataInputStream stream =
new DataInputStream(new ByteArrayInputStream(book));
// Match based on the ISBN.
return searchISBN.equals(stream.readUTF());
} catch (IOException ex) {
System.err.println(ex);
}
}
// Default is not to match
return false;
|
public void | removeRecordListener(javax.microedition.rms.RecordListener l)
if (store != null) {
store.removeRecordListener(l);
}
|
public void | saveBookInfo(BookInfo bookInfo)
if (store != null) {
searchISBN = bookInfo.getIsbn();
RecordEnumeration enum = store.enumerateRecords(
this, null, false);
if (enum.numRecords() > 0) {
// A matching record exists. Set the id
// of the BookInfo to match the existing record
bookInfo.id = enum.nextRecordId();
byte[] bytes = toByteArray(bookInfo);
store.setRecord(bookInfo.id, bytes, 0, bytes.length);
} else {
// Create a new record
bookInfo.id = store.getNextRecordID();
byte[] bytes = toByteArray(bookInfo);
store.addRecord(bytes, 0, bytes.length);
}
// Finally, destroy the RecordEnumeration
enum.destroy();
}
|
private byte[] | toByteArray(BookInfo bookInfo)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(baos);
os.writeUTF(bookInfo.isbn);
os.writeUTF(bookInfo.title == null ? "" : bookInfo.title);
os.writeInt(bookInfo.ranking);
os.writeInt(bookInfo.reviews);
os.writeInt(bookInfo.lastRanking);
os.writeInt(bookInfo.lastReviews);
return baos.toByteArray();
|