📄 objectstore.java
字号:
/*
* Copyright (C) 2006-2007 Funambol
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.funambol.storage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;
import com.funambol.util.Log;
/**
* This class uses the J2ME RMS to store and retrieve objects
* using the rms positional access.
*
* To persist an object using ObjectStore, it must implement the
* com.funambol.storage.Serializable interface.
*
* @param name is the name of the RecordStore to be managed
*/
public class ObjectStore {
// ---------------------------------------------- Attributes
private RecordStore rs;
private RecordEnumeration re;
private ObjectFilter of;
private ObjectComparator oc;
// ---------------------------------------------- Constructors
/**
* Creates a new instance of ObjectStore.
*/
public ObjectStore() {
rs = null;
re = null;
}
/**
* Get all object of the ObjectStore
* @param s Serializable Object to be returned
* @return Enumeration of Serializable object found
*/
public Enumeration getObjects(Serializable s) {
return new ObjectEnumeration(this.rs, this.of, this.oc, s);
}
/**
* @return Store name
*/
public String getName() throws RecordStoreNotOpenException {
return rs.getName();
}
/**
* Open an existing RecordStore, or throws an exception if not present.
* If the name is the same of the currently open one, no action is
* taken, otherwise the old one is closed.
*
* @param name is the name of the RecordStore to be managed
* @return true if the record store has been open or created
* false if it was cached
*/
public boolean open(String name) throws RecordStoreException {
return openStore(name, false);
}
/**
* Creates a new RecordStore, or open an existing one.
*
* @param name is the name of the RecordStore to be managed
* @return true if the record store has been open or created
* false if it was cached
*
*/
public boolean create(String name) throws RecordStoreException {
return openStore(name, true);
}
/**
* private method used by open and create to share code.
*/
private boolean openStore(String name, boolean create)
throws RecordStoreException {
// Check if is requested to open a new record store
if(rs != null){
if(rs.getName().equals(name)){
return false; // equal: keep the old one
}
else {
close();
}
}
rs = RecordStore.openRecordStore(name, create);
return true;
}
/**
* Close the current RecordStore, if open.
*
*/
public void close() throws RecordStoreException {
if(rs != null){
rs.closeRecordStore();
}
rs = null;
re = null;
}
/**
* Return the number of records in this ObjectStore
*
* @return the number of records present or -1 if the ObjectStore is not
* open.
*/
public int size() {
int ret = 0;
try {
if(rs != null) {
ret = rs.getNumRecords();
} else {
return -1;
}
} catch (RecordStoreNotOpenException ex) {
Log.error("Can't get size of ObjectStore: recordstore not open.");
ret = -1;
}
return ret;
}
/**
* Get the first valid index in the record store.
*
* @return the first valid index in the record store, or 0 if empty.
*/
public int getFirstIndex() throws RecordStoreException {
//TODO: could we use false here to improve performances?
re = rs.enumerateRecords(null, null, true);
return getNextIndex();
}
/**
* Get the next valid index in the record store.
*
* @return the next valid index in the record store, or 0 if no
* more record are present.
*/
public int getNextIndex() throws RecordStoreException {
if(re.hasNextElement()){
return re.nextRecordId();
}
else {
return 0;
}
}
/**
* Store the serializable object in a new record.
*
* @param obj the serializable object
* @return the index in the recordstore
*/
public int store(Serializable obj) throws RecordStoreException, IOException {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(byteStream);
obj.serialize(out);
byte[] data = byteStream.toByteArray();
int ret = rs.addRecord(data, 0, data.length);
obj = null;
data = null;
byteStream = null;
out = null;
return ret;
}
/**
* Store the serializable object in an existent record.
*
* @param obj the serializable object
* @return the index in the recordstore
*/
public int store(int index, Serializable obj)
throws RecordStoreException, IOException {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(byteStream);
obj.serialize(out);
byte[] data = byteStream.toByteArray();
rs.setRecord(index, data, 0, data.length);
obj = null;
return index;
}
/**
* Retrieve the serialize object from the record store.
*
* @param obj the serializable object
* @param index the index in the recordstore
*
* @return a reference to the object, or null if the object is
* empty in the store.
*/
public Serializable retrieve(int index, Serializable obj)
throws RecordStoreException, IOException {
byte data[] = rs.getRecord(index);
if (data != null) {
ByteArrayInputStream dataStream = new ByteArrayInputStream(data);
DataInputStream in = new DataInputStream(dataStream);
obj.deserialize(in);
}
else {
return null;
}
return obj;
}
/**
* Remove the object from the store.
*
* @param index the index in the recordstore
*/
public void remove(int index) throws RecordStoreException {
rs.deleteRecord(index);
}
/**
* Returns the amount of additional room (in bytes) available for this
* record store to grow.
*
* @return the amount of storage left for this store.
*/
public int getAvaliableStorage() {
try {
return rs.getSizeAvailable();
} catch(RecordStoreNotOpenException e) {
// Should not happen
e.printStackTrace();
Log.error("ObjectStore.getAvaliableStorage: "+e.toString());
return 0;
}
}
/**
* Add a RecordListener to the recordStore
*/
public void addStoreListener(ObjectStoreListener listener) {
rs.addRecordListener(listener);
}
/**
* Add a RecordListener to the recordStore
*/
public void removeStoreListener(ObjectStoreListener listener) {
rs.removeRecordListener(listener);
}
/**
* Set Filter for this ObjectStore
*/
public void setObjectFilter(ObjectFilter newOf) {
this.of = newOf;
}
/**
* Remove Filter after usage
*/
public void removeObjectFilter() {
this.of = null;
}
/**
* Set Comparator for this ObjectStore
*/
public void setObjectComparator(ObjectComparator newOc) {
this.oc = newOc;
}
/**
* Set Comparator for this ObjectStore
*/
public void removeObjectComparator() {
this.oc = null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -