⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rmshandler.java

📁 Java ME手机应用开发大全一书的配套光盘上的源码
💻 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 + -