Java Reference
In-Depth Information
readInt : Reads an integer with the high byte first
readLong : Reads a long integer with the high byte first
readShort : Reads a short integer with the high byte first
readUTF : Reads a string using Java-modified UTF-8 encoding (in a machine-
specific way)
Of course, you must read the fields in the same order as they were originally written.
Finally, you can process the fields into a new object using the object's constructor,
or whatever other data structure you choose to use to represent your data while in
memory. As with writing, it's a good idea to nil out references to the byte array bytes ,
the ByteArrayInputStream instance, and the DataInputStream instance.
Of course, you can take a more sophisticated approach if your application requires.
One common pattern is to tag each field you write with a field type that identifies the
field, so that your MIDlet can read fields out of order. I show you how to do that in the last
section of this chapter.
Adding a Record
Once you have an array of bytes in hand that you want to store as a record, creating a new
record is as easy as invoking the open record store's addRecord method. This method takes
three arguments:
• The array of bytes containing the data that consists of the record
• The offset to the first byte of the record in the array of data
• The number of bytes in the array that makes up the record
This approach—passing not just a bag of bytes but an offset into the same bag, and a
count of the number of bytes that make up the record—lets you construct multiple records
in one buffer if need be, and can help reduce thrash on the platform's memory manager.
The addRecord method can throw one of the following exceptions:
RecordStoreNotOpenException : Indicates that the record store you want to add a
record to isn't open
RecordStoreFullException : Indicates that the record store or the underlying
medium has no more room in which to complete the operation.
RecordStoreException : Indicates a general failure related to the open record store
SecurityException : Indicates that the MIDlet is not authorized to write to the
record store
 
Search WWH ::




Custom Search