Java Reference
In-Depth Information
what records satisfy the criteria for being included in the Record-
Enumeration returned by a call to RecordStore.enumerate-
Records() . Take this code as an example:
public class StringFilter implements RecordFilter {
public boolean matches(byte[] candidate) {
if(new String(candidate,0,16).equals("Employee")) {
return true;
} return false;
}
} StringFilter filter = new StringFilter();
RecordEnumeration enum =
someRecordStore.enumerateRecords(filter,null,false);
In this code, enum now contains only records whose String repre-
sentation of the first 16 bytes is 'Employee'.
Filters are simple to use, but you must remember that they are matched
against every single record in a RecordStore . If you know the store
won't grow much, it's OK to have complex filters that do comparisons
based on a big part of the byte array. However, if there's a slight chance
of your record store growing to hundreds or thousands of records, you
need to optimize your filters. The following code, handling about 2000
records in the RecordStore , takes about two minutes to complete on a
Nokia E61:
public class StringFilter implements RecordFilter {
...
public boolean matches(byte[] candidate) {
ByteArrayInputStream bais = new ByteArrayInputStream(candidate);
DataInputStream dais = new DataInputStream(bais);
int code = dais.readInt();
int category = dais.readInt();
if(code == product.getCode() && category == product.getCategory()) {
return true;
} return false;
}
}
The problem with the above code is not RMS related. For each
of the 2000 records present in the store, the code creates a new
ByteArrayInputStream of a 200-something byte array, then cre-
ates a DataInputStream on top of it, to read just a couple of integers
for comparison.
A much more efficient way to do the filtering is:
public class StringFilter implements RecordFilter {
...
public boolean matches(byte[] candidate)
{
int code = arrayToInt(candidate,0);
Search WWH ::




Custom Search