?? rmshandler.java
字號:
package picturepuzzle;
import javax.microedition.rms.*;
import java.io.*;
import java.util.*;
/**
* 用來存儲圖片的RMS存儲器
*/
public class RMSHandler {
public static final String IMAGE_RECORD_STORE = "IMAGES";
public static final String INDEX_RECORD_STORE = "KEYS";
private RecordStore indexRecordStore;
private RecordStore imageRecordStore;
private Hashtable hashTable;
public RMSHandler(){
hashTable = new Hashtable();
}
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);
}
}
public void closeRecordStore() throws ApplicationException {
try {
imageRecordStore.closeRecordStore();
indexRecordStore.closeRecordStore();
} catch (RecordStoreException rse) {
throw new ApplicationException("Unable to close record store", rse);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
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);
}
}
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;
}
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 + -