persistent storage record stores mini databases – represent data as byte records record...
Post on 22-Dec-2015
225 views
TRANSCRIPT
Persistent Storage
Record Stores mini databases – represent data as byte records Record Management System (RMS) API
Files and Directories FileConnection API
Contacts and Calendars Personal Information Management (PIM) API
RMS vs FileConnection API
FileConnection familiar (simpler) to use provides access to images, sounds, etc.
needs security privileges may not be supported on older devices
RMS vs FileConnection API
FileConnection familiar (simpler) to use provides access to images, sounds, etc.
needs security privileges may not be supported on older devices
RMS available on all MIDP devices does not require security privileges may be more appropriate for some applications
more cumbersome to use
RecordStores Mini databases that consist of a collection of records (byte arrays)
Could be shared across MIDlets
RecordStores Mini databases that consist of a collection of records (byte arrays)
Could be shared across MIDlets
Creating Record Stores (via static methods)
RecordStore openRecordStore(String name, boolean create)
RecordStore openRecordStore(String name, boolean create, int auth, boolean writable)
auth = AUTHMODE_ANY – accessible by all midlets
AUTHMODE_PRIVATE – restricted to this midlet
RecordStores Mini databases that consist of a collection of records (byte arrays)
Could be shared across MIDlets
Creating Record Stores (via static methods)
RecordStore openRecordStore(String name, boolean create)
RecordStore openRecordStore(String name, boolean create, int auth, boolean writable)
auth = AUTHMODE_ANY – accessible by all midlets
AUTHMODE_PRIVATE – restricted to this midlet
Manipulating Record Storesint addRecord(byte[] data, int offset, int numBytes)
void deleteRecord(int recordID)
byte[] getRecord(int recordID)
void setRecord(int recordID, byte[] data, int offset, int numBytes)
int getNumRecords()
int getRecordSize(int recordID)
Writing to RecordStore
void saveRecords(String rsName)
{
RecordStore rs = RecordStore.openRecordStore(rsName, true);
String[] data = {“Mickey”, “Minnie”, “Donald”, “Tom”, “Jerry”};
for (int i = 0; i < data.length; ++i) {
byte[] bytes = data[i].getBytes();
rs.addRecord(bytes, 0, bytes.length);
}
rs.closeRecordStore();
}
// NOTE: The RecordStore methods throw exceptions – put try/catch.
Reading from RecordStore
void readRecords(String rsName)
{
RecordStore rs = RecordStore.openRecordStore(rsName, true);
for (int i = 1; i <= rs.getNumRecords(); ++i) {
byte[] record = rs.getRecord(i);
String name = new String(record);
mainScreen.append(name);
}
rs.closeRecordStore();
}
// NOTE: The RecordStore methods throw exceptions – put try/catch.
// Typically will use RecordEnumeration to traverse
Enumerating Records RecordEnumeration – an interface for sequential traversal of a RecordStore
interface RecordEnumeration
{
bool hasNextElement();
bool hasPreviousElement();
byte[] nextRecord();
byte[] previousRecord();
int numRecords();
... ... ...
}
Enumerating Records RecordEnumeration – an interface for sequential traversal of a RecordStore
interface RecordEnumeration
{
bool hasNextElement();
bool hasPreviousElement();
byte[] nextRecord();
byte[] previousRecord();
int numRecords();
... ... ...
}
To get an enumerator for a RecordStore:
RecordEnumeration enumerateRecords(RecordFilter f, RecordComparator c,
boolen keepUpdated);
ordering criterion
selection criterion
Enumerating Records
void processRecords(String rsName)
{
RecordStore rs = RecordStore.openRecordStore(rsName, true);
RecordEnumeration records = rs.enumerateRecords(null, null, true);
while (records.hasNextElement()) {
byte[] rawData = records.nextRecord();
String name = new String(rawData);
mainScreen.append(name);
}
rs.closeRecordStore();
}
RecordFilter Interface
Use to retrieve only those records that match a certain criterion
interface RecordFilter
{
boolean matches(byte[] record);
}
return true iff record satisfies the criterion
RecordFilter Interface
Use to retrieve only those records that match a certain criterion
interface RecordFilter
{
boolean matches(byte[] record);
}
Filter for records that start with CompSci:
class CompSciFilter implements RecordFilter
{
boolean matches(byte[] record)
{
String data = new String(record);
return data.startsWith(“CompSci”);
}
}
return true iff record satisfies the criterion
RecordComparator Interface
Use to create an ordering criterion for retrieving the records
interface RecordComparator
{
int compare(byte[] r1, byte[] r2)
}
return FOLLOWS if r1 comes after r2 in sorted order
PRECEDES if r1 comes before r2 in sorted order
EQUIVALENT if r1 and r2 are considered equal
RecordComparator Interface Retrieve by decreasing size
class DescSizeSorter implements RecordComparator
{
boolean compare(byte[] r1, byte[] r2)
{
String s1 = new String(r1);
String s2 = new String(r2);
if (s1.size() > s2.size()) {
return ???
}
else if (s1.size() < s2.size()) {
return ???
}
else {
return ???
}
}
Enumerating Records Traverse only CompSci records in decreasing order of length
void processRecords(String rsName)
{
RecordStore rs = RecordStore.openRecordStore(rsName, true);
CompSciFilter filter = new CompSciFilter();
DecSizeSorter order = new DecSizeSorter();
RecordEnumeration records = rs.enumerateRecords(filter, order, true);
while (records.hasNextElement()) {
byte[] rawData = records.nextRecord();
String name = new String(rawData);
mainScreen.append(name);
}
rs.closeRecordStore();
}
Input/Output Streams An abstract representation of stream of data
ByteArrayInputStream/ByteArrayOutputStream --- abstraction for
reading from / writing to a byte array
OutputStream
ByteArrayOutputStream
DataOutputStream
InputStream
ByteArrayInputStream
DataInputStream
ByteArrayInput/Output Streams Provide a mechanism for reading from / writing to a byte array (only bytes)
Allow the buffer to grow as needed (on writing to)
Selected methods (ByteArrayInput):
int size() – current size of buffer
void write(int b) – write a byte to the buffer
void write(byte[] b, int off, int len) – write len bytes starting off
byte[] toByteArray() – return the buffer in a byte array
Example:ByteArrayOutputStream os = new ByteArrayOutputStream();
os.write(‘C’);
os.write(‘S’);
os.write(1);
byte[] data = os.toByteArray();
DataInput/Output Stream Provide convenient mechanism for reading from / writing to a stream
(supports all of the primitive types)
Selected methods (DataOutputStream):
DataOutputStream(OutputStream os)
void writeInt(int v)
void writeDouble(double v)
void writeUTF(String s) -- write machine-independentt encoding of v
void flush() -- forces any written values to be written to stream
Selected methods (DataInputStream):
DataInputStream(InputStream os)
int readInt()
double readDouble()
String readUTF()
long skip(long n) – skip n bytes (return how many actually skipped)
int available() – how many bytes can be read without waiting
DataInput/Output Stream Example (writing)
ByteArrayOutputStream os = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(os);
output.writeUTF(‘Comp Sci’);
output.writeInt(391);
output.writeDouble(4.3);
byte[] = os.toByteArray();
Example (reading)
byte[] data = ... // data stored in somehow
ByteArrayInputStream is = new ByteArrayInputStream(data);
DataInputStream input = new DataOutputStream(is);
String name = input.readUTF();
int code = input.readInt();
double maxgrade = input.readDouble(4.3);
DataInput/Output Stream Example (writing)
ByteArrayOutputStream os = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(os);
output.writeUTF(‘Comp Sci’);
output.writeInt(391);
output.writeDouble(4.3);
byte[] = os.toByteArray();
Example (reading)
byte[] data = ... // data stored in somehow
ByteArrayInputStream is = new ByteArrayInputStream(data);
DataInputStream input = new DataOutputStream(is);
String name = input.readUTF();
int code = input.readInt();
double maxgrade = input.readDouble(4.3);
Files and Directories Generic Connection Framework (Connections and Connector classes)
ConnectorTypeConnection = (TypeConnection) Connector.open(url)
Connection
InputConnection
OutputConnection
FileConnection SocketConnection HttpConnection
DataInputStream openDataInputStream()
int getPort()String getHost()String getURL()String getResponseMessage()
DataOutputStream openDataOutputStream()OutputStream openOutputStream()
void close()
int getPort()String getAddress()String getLocalAddress()
InputStream openInputStream()
File Manipulation Example (writing)
FileConnection fc = (FileConnection) Connector.open( filename );
DataOutputStream output = fc.getDataOutputStream();
output.writeInt(2);
output.writeUTF(“CS 112”);
output.writeUTF(“CS 391”);
output.close();
fc.close();
Example (reading)FileConnection fc = (FileConnection) Connector.open( filename );
DataInputStream input = fc.getDataInputStream();
int numRecords = input.readInt();
String name1 = input.readUTF();
String name2 = input.readUTF();
output.close();
fc.close();