Java Reference
In-Depth Information
store, the
initialize
method obtains the first location from the store, giving the UI a
default location.
The default location returned by
get_location
is now an empty string, consonant
with the notion that this will only occur the first time you launch the application before
you enter any locations. Updating the list of locations—which
add_location
performs—
is no longer a matter of adding an element to a vector, but instead calling the
LocationStore
's
addLocation
method, with some additional error handling in case the
store throws an exception. This error handling relies on the new factory method
get_cannotAddLocationAlert
and the corresponding
Alert
, which informs you that the
application couldn't add a location to the list of locations.
The
get_locationList
—a method NetBeans originally created that is the factory
method to create the list of locations—now invokes the
LocationStore
's
getLocationStrings
method instead of converting the
Vector
of locations to an array
of
String
s.
Wrapping Up
In this chapter, you learned how to store persistent data using the MIDP's record-store
interface. As you now know, a record store is a collection of records, each of which are an
ordered collection of bytes. The MIDP implementation of the record-store interface
abstracts you from the underlying details of the target, which may store the record store
as records in a file, as bytes in flash in a protected area, or as any other medium that
meets the standard's requirements. By default, a MIDlet suite's record stores can only be
accessed by MIDlets in that suite, although you can export access to other MIDlet suites
by specifying an authorization mode when you create the record store.
The
javax.microedition.rms
package contains the record-store classes, which include
the
RecordStore
class itself. The
RecordStore
class is responsible for managing record
stores as well as inserting, enumerating, updating, and removing record stores. Each
record is a collection of bytes that the record store assigns a record ID, which you use to
subsequently refer to a record when fetching, updating, or removing the record using the
RecordStore
's
addRecord
,
setRecord
,
getRecord
, and
deleteRecord
methods. You can also
enumerate the records in a store, passing instances of classes that filter and order the
enumeration results.
When creating and parsing records in a record store, it helps to use the
java.io
classes
ByteArrayInputStream
,
ByteArrayOutputStream
,
DataInputStream
, and
DataOutputStream
. The
DataInputStream
and
DataOutputStream
let you read and write
data primitives, such as integers, floating-point numbers, and strings, to an underlying
stream. In conjunction with the
ByteArrayInputStream
and
ByteArrayOutputStream
classes, you can take the resulting streams and convert them to arrays of bytes, which
the
RecordStore
class can use immediately as records in the store.