📄 rmshandler.java
字号:
/*
* Copyright 2003, 2004 Symbian Ltd.
* For License terms see http://www.symbian.com/developer/techlib/codelicense.html
*/
package picturepuzzle;
import javax.microedition.rms.*;
import java.io.*;
import java.util.*;
/**
* Used to store images in RMS storage.
* Stores images in the IMAGE record store. Creates an INDEX record store to store the
* name of the image and its record id for easy retrieval.
*/
public class RMSHandler {
/**
* Name of record store for storing images
*/
public static final String IMAGE_RECORD_STORE = "IMAGES";
/**
* Name of record store for storing index entries
*/
public static final String INDEX_RECORD_STORE = "KEYS";
private RecordStore indexRecordStore;
private RecordStore imageRecordStore;
private Hashtable hashTable;
public RMSHandler(){
hashTable = new Hashtable();
}
/**
* Opens IMAGE and INDEX record stores
* @throws ApplicationException If unable to open either record store
*/
public void openRecordStore() throws ApplicationException {
try {
imageRecordStore = RecordStore.openRecordStore(IMAGE_RECORD_STORE, true);
indexRecordStore = RecordStore.openRecordStore(INDEX_RECORD_STORE, true);
} catch (RecordStoreException rse) {
throw new ApplicationException("Unable to open record store", rse);
}
}
/**
* Closes IMAGE and INDEX record stores.
* @throws ApplicationException If unable to close either INDEX or IMAGE record store
*/
public void closeRecordStore() throws ApplicationException {
try {
imageRecordStore.closeRecordStore();
indexRecordStore.closeRecordStore();
} catch (RecordStoreException rse) {
throw new ApplicationException("Unable to close record store", rse);
}
}
/**
* Adds an entry to the INDEX store
* @param name The name of the image.
* @param key The record ID of the image in the IMAGE store
* @throws ApplicationException If unable to add key to the INDEX store
*/
private int addKey(String name, int recordID) throws ApplicationException {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeUTF(name);
dos.writeInt(recordID);
byte[] data = baos.toByteArray();
int keyID = indexRecordStore.addRecord(data, 0, data.length);
return keyID;
} catch (IOException ioe) {
throw new ApplicationException("Unable to add key to record store", ioe);
} catch (RecordStoreException rse) {
throw new ApplicationException("Unable to add key to record store", rse);
}
}
/**
* Deletes the index entry (name and record ID of an Image) from the INDEX record store.
* @param keyID The record ID of the index entry in the INDEX record store.
*/
private void deleteKey(int keyID) throws ApplicationException {
try {
indexRecordStore.deleteRecord(keyID);
} catch (RecordStoreException rse) {
throw new ApplicationException("Unable to delete key from record store", rse);
}
}
/**
* Adds Image data to IMAGE record store.
* @param data The Image data.
*/
private int addImageRecord(byte[] data) throws ApplicationException {
try {
int recordID = imageRecordStore.addRecord(data, 0, data.length);
return recordID;
} catch (RecordStoreException rse) {
throw new ApplicationException("Unable to add record to record store", rse);
}
}
/**
* Deletes Image data from IMAGE record store.
* @param recordID The record ID of the Image data.
*/
private void deleteImageRecord(int recordID) throws ApplicationException {
try {
imageRecordStore.deleteRecord(recordID);
return;
} catch (RecordStoreException rse) {
throw new ApplicationException("Unable to delete record from record store", rse);
}
}
/**
* Adds an Image to Image record store and its name and record ID to INDEX record
* store.
* @param name The name of the Image.
* @param imageData The Image data.
* @throws ApplicationException If unable to add Image to record store
*/
public void addImageToStore(String name, byte[] imageData) throws ApplicationException {
int[] recordIndices = new int[2];
recordIndices[0] = addImageRecord(imageData);
recordIndices[1] = addKey(name, recordIndices[0]);
hashTable.put(name, recordIndices);
}
/**
* deletes image from IMAGE store and associated entry in INDEX store
* @param name Image name
* @throws ApplicationException If unable to delete Image entry of index key entry
*/
public void deleteImageFromStore(String name) throws ApplicationException {
int[] recordIndices = (int[])hashTable.get(name);
if (recordIndices != null) {
deleteImageRecord(recordIndices[0]);
deleteKey(recordIndices[1]);
hashTable.remove(name);
}
}
/**
* Retrieves an Image from the IMAGE record store.
* @param name The Image name.
* @throws ApplicationException If unable to retrieve Image.
* @return The Image data.
*/
public byte[] retrieveImageFromStore(String name) throws ApplicationException {
int[] recordIndices = (int[])hashTable.get(name);
byte[] imageData = null;
if (recordIndices != null) {
try {
imageData = imageRecordStore.getRecord(recordIndices[0]);
}catch(RecordStoreException rse) {
throw new ApplicationException("Unable to retrieve record from record store", rse);
}
}
return imageData;
}
/**
* Retrieves the names of images stored in the record store.
* @throws ApplicationException If unable to retrieve image
* @return An array of the names of stored images
*/
public String[] retrieveStoredImageNames() throws ApplicationException {
String[] entries = null;
try {
if (indexRecordStore.getNumRecords() == 0) {
return null;
}
RecordEnumeration records = indexRecordStore.enumerateRecords(null, null, false);
int numOfRecords = records.numRecords();
int[][] recordIndices = new int[numOfRecords][2];
entries = new String[numOfRecords];
for (int i = 0; i < numOfRecords; i++) {
int keyID = records.nextRecordId();
byte[] data = indexRecordStore.getRecord(keyID);
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);
String imageName = dis.readUTF();
int recordID = dis.readInt();
recordIndices[i][0] = recordID;
recordIndices[i][1] = keyID;
entries[i] = imageName;
hashTable.put(imageName, recordIndices[i]);
}
return entries;
} catch (IOException ioe) {
throw new ApplicationException("Unable to read from record store", ioe);
} catch (RecordStoreException rse) {
throw new ApplicationException("Unable to read from record store", rse);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -