RecordStoreHelper.java

/**
 * Copyright (c) 2013 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */

package com.nokia.example.statusshout.engine;

import java.util.Vector;

import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotFoundException;

/**
 * A utility class for reading from and write to a record store.
 */
public class RecordStoreHelper {
    // Members
    private RecordStore _recordStore = null;

    /**
     * Opens a record store with the given name and retrieves the handle to it.
     * @param recordStoreName The name of the record store to open.
     * @return True if successful, false otherwise.
     */
    public boolean open(final String recordStoreName) {
        try {
            _recordStore = RecordStore.openRecordStore(recordStoreName, true);
        }
        catch (RecordStoreException rse) {
            rse.printStackTrace();
            return false;
        }
        
        return true;
    }

    /**
     * Closes the open record store.
     */
    public void close() {
        try {
            _recordStore.closeRecordStore();
        }
        catch (RecordStoreException rse) {}
    }
    
    /**
     * Retrieves the record store data.
     * @return The record store data in a vector of strings.
     */
    public Vector getRecords() {
        Vector result = new Vector();
        
        try {
            RecordEnumeration recordEnum =
                    _recordStore.enumerateRecords(null, null, false);
            int index = 0;
            byte[] data = null;
            
            while (recordEnum.hasNextElement()) {
                index = recordEnum.nextRecordId();
                data = _recordStore.getRecord(index);
                
                if (data != null) {
                    String element = new String(data);
                    result.addElement(element);
                }
            }
        }
        catch (RecordStoreException rse) {
            rse.printStackTrace();
        }
        
        return result;
    }

    /**
     * Adds the given data into the record store.
     * @param data The data to store.
     * @return True if successful, false otherwise.
     */
    public boolean addRecord(byte[] data) {
        try {
            // addRecord() returns the ID for the new record but we don't need
            // it here
            _recordStore.addRecord(data, 0, data.length);
        }
        catch (RecordStoreException rse) {
            rse.printStackTrace();
            return false;
        }
        
        return true;
    }
    
    /**
     * Deletes the record store with the given name.
     * @param recordStoreName The name of the record store to delete.
     * @return True if successful, false otherwise.
     */
    public boolean clear(final String recordStoreName) {
        try {
            RecordStore.deleteRecordStore(recordStoreName);
            return true;
        }
        catch (RecordStoreNotFoundException e) {
            e.printStackTrace();
        }
        catch (RecordStoreException e) {
            e.printStackTrace();
        }
        
        return false;
    }
}

Last updated 9 October 2013

Back to top

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×