Java Reference
In-Depth Information
First and foremost, just because a device has a certain amount of storage available
does not guarantee that your MIDlets can use that much storage. The device's software
(including the Java applications and native applications such as messaging,
contacts/personal information management, web browser, and so forth) shares the per-
sistent store, so the Java ME world only gets a subset of the persistent store in which to
store its data. How devices handle managing storage limitations differs from device to
device, as some devices may implement per-MIDlet limitations, while others may give
the Java AMS a private chunk of the persistent store in which to work. Worse, invoking a
RecordStore 's getSizeAvailable method causes the RecordStore to behave differently on
different devices; some devices may report the total amount of space left in the currently
allocated record store, while other devices may report the total amount of space left in
the persistent storage for all Java ME record stores.
The second common limitation is a limit on the size of an individual record; this
typically stems from the underlying implementation of a given record store. On some
devices, writing records that consist of more than some arbitrary maximum size simply
fails, throwing an exception, even if there's space in the store for the record. Worse, you
can't predict this in advance: there's no way to determine if a specific handset has this
limitation, or query the record store implementation to determine what this cap may be.
To avoid this, keep your records as short as possible, and if you encounter the problem,
break your record up into smaller subrecords and write them individually, chaining them
again when you read from the record store.
Putting the Record Store to Work
The WeatherWidget example needs to store the list of locations you entered, as well as the
forecast data at each location, so that you don't have to hit the network as you pick loca-
tions. The MIDP record store is ideal for this purpose.
WeatherWidget divides its use of the record store up into two classes: the Location
class, which represents a record in the store once it's loaded into memory, and the
LocationStore class, which wraps the store in methods more friendly to the application
itself. Listing 6-5 shows the Location class, which is responsible for storing a location and
the weather forecast for a location.
Listing 6-5. The Implementation of the Location Class
package com.apress.rischpater.weatherwidget;
import javax.microedition.rms.*;
import java.io.*;
 
Search WWH ::




Custom Search