Java Reference
In-Depth Information
The second variant of getRecord() avoids allocating a new byte array by writing data to a byte
array given by the application. It is invoked with more parameters and provides a different return value.
As with the simple version, the first parameter is the record ID. The second parameter is a byte array
that is used for storing the data read. The third parameter is the offset at which to start writing the
record data in the given byte array. As a result, the method returns the number of bytes transferred to
the specified buffer. The application is required to provide a buffer that is large enough to store the
record with the given ID. If the buffer size is not sufficient, an
ArrayIndexOutOfBoundException is thrown. The following lines allocate a byte array as a
buffer and read the first record from the myRecords record store into the newly allocated buffer:
byte[] myBuffer = new byte [BIG_ENOUGH];
int numberOfBytesRead = myRecords.getRecord (1, myBuffer, 0);
Records that are no longer needed can be deleted using the deleteRecord() method. The only
parameter of deleteRecord() is the ID of the record to be deleted. After deletion, the record is
removed from the RecordStore irrevocably. Please note that the record ID of the removed record is
not reused and cannot be reset with a new record using the setRecord() method. In case of
resetting a previously deleted record, an InvalidRecordStoreIDException is thrown. All
further records keep their record IDs.
Meta Information
In addition to methods manipulating whole record stores or single records, the RMS API also provides
a set of methods for obtaining information about a record store or single records.
All methods to get record store meta information are listed in Table 5.3 . One important method needed,
for example, to iterate all records in a record store is getNumRecords() . This method is invoked on
a record store instance and returns the number of records stored in the record store as an integer value.
The following line shows how the method is called to get the number of records in the myRecords
record store:
int numberOfRecords = myRecords.getNumRecords();
Table 5.3. Method s for Accessing RecordStore Meta Information
Method Name Purpose
long getLastModified() Returns the last modification of the record store in the format
returned by System.currentTimeMillis() .
String getName() Returns the name of the current record store.
int getNextRecordID() Returns the record ID of the next record that will be added to
the record store.
int getRecordSize(int
recordID)
Returns the byte size of the record at the given ID.
int getSize() Returns the byte size of the complete record store.
int getSizeAvailable() Returns the storage space that is currently available for
storing records.
int getVersion()
Returns the integer value that represents the number of
modifications of the record store.
A Simple Diary Application Using RMS
Using the methods described in the previous section, you can now build a simple diary application. The
application will demonstrate how to store simple objects ( String s) in a record store.
 
 
Search WWH ::




Custom Search