Java Reference
In-Depth Information
createIfNecessary)
void deleteRecordStore (String
recordStoreName)
Deletes a complete record store from
the MIDlet suite.
The RecordStore methods can throw different types of Exception s in case of a failure. The
exceptions are listed in Table 5.2 . For example, a RecordStoreNotFoundException is thrown
if the desired record store does not exist in the MIDlet suite and the openRecordStore() method
is called without setting the create option. In case of success, the method returns an instance of the
RecordStore class.
Table 5.2. Pos s ible RecordStore Exceptions
Exception Name Reason
InvalidRecordIDException Thrown if the index that is passed to an operation like
deleteRecord() , getRecord() ,
getRecordSize() , or setRecord() is not a valid
index.
RecordStoreException Thrown when a general record store failure occurs.
RecordStoreFullException Thrown to indicate that the operation cannot be
completed because the complete storage space
available for the record store is consumed already.
RecordStoreNotFoundException Thrown by the methods openRecordStore() and
deleteRecordStore() to indicate that the record
s tore with the specified name does not exist.
RecordStoreNotOpenException Thrown when a method to access a record is called
after the record store was closed.
Manipulating Single Records
When the record store is successfully opened, you can add new records using the addRecord()
method. This method takes three parameters. The first is the byte array containing the data that should
be written to the record store. The second specifies the offset from which to start writing data of the
byte array. The third specifies how many bytes of the given array should be written. The method
returns the index of the newly created record. Like all RecordStore methods, addRecord() may
throw different types of RecordStoreException s in case of a failure. If you want to write the
complete byte array, set the second parameter to 0 and the third to the length of the byte array:
myRecords.addRecord (myBytes, 0, myBytes.length);
For replacing a record at a given index, RecordStore provides the method setRecord() . The
usage of the setRecord() method is similar to addRecord() , except that the index of the record
to be replaced must be given as the first parameter. In contrast to addRecord() , no index value is
returned. Please note that the first record in a record store has the index number 1, and not 0 as in most
other Java APIs dealing with indexed structures. Specifying a record ID of 0 or any other invalid record
ID will result in an InvalidRecordIDException . The following call replaces the first record of a
record store with the complete byte array given as the second parameter:
myRecords.setRecord (1, myBytes, 0, myBytes.length);
For reading records, RMS provides two different getRecord() methods. The simple version of
getRecord() takes a record ID as a parameter and returns a byte array containing the corresponding
record data. Using the following line of code, you can read the data of the first record in the
myRecords record store:
byte[] recordData = myRecords.getRecord (1);
 
Search WWH ::




Custom Search