import javax.microedition.rms.*;
public class MyRecordStore
{
public RecordStore recordStore;
public String id;
public int numberOfRecords;
//constructor
public MyRecordStore(String aRecordStoreId)
{
id = aRecordStoreId;
}//end MyRecordStore
public void create()
{
try
{
// create a record store that can be shared with other
// MIDlet suites. The RecordStore is owned by the current MIDlet suite.
// the second argument indicates if the record store must be created
recordStore = RecordStore.openRecordStore( id,true );
}//end try
catch( Exception e )
{
System.out.println( "Could not create record store.." + e.toString() );
}//end catch
}//end create
public void open()
{
try
{
// Open a record store that can be shared with other
// MIDlet suites. The RecordStore is owned by the current MIDlet suite.
// the second argument indicates if the record store must be created
recordStore = RecordStore.openRecordStore( id,false );
}//end try
catch( Exception e )
{
System.out.println( "Could not open record store.." + e.toString() );
}//end catch
}//end open
public void addRecord(String recordContent)
{
byte[] recordContentBytes;
recordContentBytes = recordContent.getBytes();
try
{
//Adds a new record to the record store.
// The recordId for this new record is returned.
// This is a blocking atomic operation.
// The record is written to persistent storage before the method returns.
// the first parameter is the data to be stored in this record.
// If the record is to have zero-length data (no data),
// this parameter may be null.
// The second parameter is the index into the data buffer of
// the first relevant byte for this record (i.e. 0).
// The third parameter is the number of bytes of the data buffer
// to use for this record.
recordStore.addRecord( recordContentBytes, 0, recordContentBytes.length );
}//end try
catch (Exception e)
{
System.out.println("Could not add record " + e.toString());
}//end catch
}//end addRecord
public void deleteRecord(int recordId )
{
try
{
recordStore.deleteRecord( recordId );
}//end try
catch( Exception e )
{
System.out.println("Could not delete record..." + e.toString() );
}//end catch
}//end deleteRecord
public String getRecord(int recordId)
{
byte[] recordContentBytes = null;
String recordContent = null;
try
{
// Get the data stored in the given record.
// The first parameter is the ID of the record to use in this operation
// The second parameter is the byte array in which to copy the data
// The third parameter is the index into the buffer in which
// to start copying (i. e. 0).
recordContentBytes = recordStore.getRecord(recordId);
}//end try
catch (Exception e)
{
System.out.println("Could not get record " + e.toString());
}//end catch
recordContent = new String(recordContentBytes,0,recordContentBytes.length);
return recordContent;
}//end getRecord
public int getNumberOfRecords()
{
try
{
numberOfRecords = recordStore.getNumRecords();
}//end try
catch (Exception e)
{
System.out.println("Could not get number of records " + e.toString());
}//end catch
return numberOfRecords;
}//end getNumberOfRecords
public boolean hasRecords()
{
if( getNumberOfRecords() > 0 )
{
return true;
}//end if
else
{
return false;
}//end else
}//end hasRecords
public RecordEnumeration select(RMSFilterStartsWith filter,
RMSOrder comparator)
{
RecordEnumeration recordEnumeration = null;
try
{
// Create an enumeration for traversing a set of records
// in the record store in an optionally specified order.
// The first parameter, if non-null, will be used to determine what
// subset of the record store records will be used
// The second parameter is a comparator
// if non-null, will be used to determine the order in which the records
// are returned
// The third argument, if true, the enumerator will keep its enumeration
// current with any changes in the records of the record store.
recordEnumeration = recordStore.enumerateRecords(filter,
comparator,
false);
}//end try
catch(Exception e)
{
System.out.println("Could not create RecordEnumeration " +
e.toString());
}//end catch
return recordEnumeration;
}//end select
public void close()
{
try
{
// This method is called when the MIDlet requests to have
// the record store closed.
// Note that the record store will not actually be closed until
// closeRecordStore() is called as many times as openRecordStore() was called.
// In other words, the MIDlet needs to make a balanced number of
// close calls as open calls before the record store is closed.
recordStore.closeRecordStore();
}//end try
catch( Exception e )
{
System.out.println("Could not close record store... "+ e.toString() );
}//end catch
}//end close
public void destroy()
{
try
{
close();
// Deletes the named record store.
// MIDlet suites are only allowed to delete their own record stores.
// If the named record store is open (by a MIDlet in this suite or a MIDlet
// in a different MIDlet suite) when this method is called,
// a RecordStoreException will be thrown.
// If the named record store does not exist a RecordStoreNotFoundException
// will be thrown.
// Calling this method does NOT result in recordDeleted calls to any
// registered listeners of this RecordStore.
RecordStore.deleteRecordStore(id);
}//end try
catch( Exception e )
{
System.out.println("Could not delete record store... " + e.toString() );
}//end catch
}//end deleteRecordStore
}//end MyRecordStore
_______________
package hello;
import javax.microedition.rms.*;
public class RMSOrder implements RecordComparator
{
boolean isAscendingOrder;
// constructor
public RMSOrder(boolean isAscendingOrderIndicator)
{
isAscendingOrder = isAscendingOrderIndicator;
}//end RMSOrder
public int compare(byte[] recordContentBytes1, byte[] recordContentBytes2)
{
String recordContent1;
String recordContent2;
recordContent1 = new String(recordContentBytes1);
recordContent2 = new String(recordContentBytes2);
if (isAscendingOrder)
{
if ((recordContent1.compareTo(recordContent2)) > 0)
{
return (RecordComparator.FOLLOWS);
}//end if
else
{
if ((recordContent1.compareTo(recordContent2)) <>
{
return (RecordComparator.PRECEDES);
}//end if
else
{
return (RecordComparator.EQUIVALENT);
}//end else
}//end else
}//end if
else
{
if ((recordContent1.compareTo(recordContent2)) <>
{
return (RecordComparator.FOLLOWS);
}//end if
else
{
if ((recordContent1.compareTo(recordContent2)) > 0)
{
return (RecordComparator.PRECEDES);
}//end if
else
{
return (RecordComparator.EQUIVALENT);
}//end else
}//end else
}//end else
}//end compare
}//end classRMSOrder
______________
package hello;
import javax.microedition.rms.*;
public class RMSFilterStartsWith implements RecordFilter
{
String subString;
// constructor
public RMSFilterStartsWith(String aSubstring)
{
subString = aSubstring;
}//end constructor
public boolean matches(byte[] recordContentBytes)
{
String recordContent;
recordContent = new String(recordContentBytes);
if (recordContent.startsWith(subString))
{
return true;
}//end if
else
{
return false;
}//end else
}//end matches
}// end RMSFilterStartsWith
No hay comentarios:
Publicar un comentario