Persistent Storage  Record Stores  mini databases – represent data as byte records  Record Management System (RMS) API  Files and Directories  FileConnection.

Slides:



Advertisements
Similar presentations
A Guide to Advanced Java Faculty:Nguyen Ngoc Tu. 2 Operating System Application #1 Application #2 Java Virtual Machine #1 Local Memory Shared Memory Threads.
Advertisements

1 Streams and Input/Output Files Part I. 2 Introduction So far we have used variables and arrays for storing data inside the programs. This approach poses.
Bruce Scharlau, University of Aberdeen, 2010 Java ME Record Management System Mobile Computing.
Java ME persistence made easy! by Thiago Rossato Thiago Moreira.
The Package Statement Group related interfaces and classes together Purpose: encapsulation and reduces name conflicts –private package classes not visible.
Socket Programming ENTERPRISE JAVA. 2 Content  Sockets  Streams  Threads  Readings.
BA1 RMS - Record Management System BA2 Record Store En Record er et bytearray af vilkårlig størrelse En RecordStore er et antal.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Java I/O The Cheat Sheet. Reading and Writing The basic architecture of Java IO is pluggable. The idea is to have some very simple classes do very simple.
III. Streams. Introduction Often a program needs to bring in information from an external source or to send out information to an external destination.
Geoff Holmes Overview IO Zoo Stream I/O File I/O Buffering Random-Access Text Streams Examples Serialization Java IO – programs that start with import.
Files Files are used to store long term data. –Typically referred to as persistent data. –A file is a group of related records. –A list of friends addresses.
Cosc 4730 Blackberry: Record Store & SQLite. Introduction RecordStore – Comes from JavaME, MIDP 1.0 On some phone who where you may not have a filesystem.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L07 (Chapter 18) Binary I/O.
Algorithm Programming I/O via Java Streams Bar-Ilan University תשס"ח by Moshe Fresko.
File I/O in Java CS 311, Winter File Basics Recall that a file is block structured. What does this mean? What happens when an application opens.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 18 Binary I/O.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L08 (Chapter 18) Binary I/O.
Socket Communication Sockets provide an abstraction of two-point communication The two sides only read/write without concern for how data is translated.
Detecting Changes  ItemStateListener interface – detect changes in internal state of an Item  new selection made in a ChoiceGroup  adjusted value of.
Chapter 20 – Streams and Binary Input/Output Big Java Early Objects by Cay Horstmann Copyright © 2014 by John Wiley & Sons. All rights reserved.
CS203 Programming with Data Structures Input and Output California State University, Los Angeles.
CSC – Java Programming II Lecture 9 January 30, 2002.
Chapter 15: Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams F Use JFileChooser F Text Input.
Databases for Mobile Devices Introduction. Overview Different types of data storages Memory management Local database techniques.
Mobile Applications with Java ME & BlackBerry. Overview Java ME –Networking –Databases Java ME and the BlackBerry –The BlackBerry wireless device –Converting.
Chapter 17 Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams  Use JFileChooser F Text Input.
Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams F Text Input and Output on the Console.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
JAVA I/O © EnhanceEdu, IIIT Hyderabad. Contents 3/29/2010EnhanceEdu, IIIT - H 2  Command Line I/O  File Class  Streams  Byte Streams [Low level and.
BlackBerry Persistent Storage Models Persistent Storage APIs and Record Management System.
Java Programming: Advanced Topics 1 Input/Output and Serialization Chapter 3.
Applications Development Input and Output in Java Topics covered: Some input-output capabilities of Java Example input code Java.
L5: Input & Output COMP206, Geoff Holmes and Bernhard Pfahringer.
Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 15 IO Using http Rob Pooley
Java Input/Output. Reading standard input Surprisingly complicated (GUI focus) Old-fashioned way: BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
Java IO. Why IO ? Without I/O, your program is a closed box. Without I/O, your program is a closed box. I/O gives your Java program access to your hard.
모바일 자바 프로그래밍 MIDP RMS Ps lab 오민경. MIDP RMS  RMS (Record Management System)  MIDP 에서 정의하는 영속성을 지닌 자체 데이터 저장 공간  Record Store 의 집합으로 구성된 아주 간단한 데이터베이스.
Object Serialization.  When the data was output to disk, certain information was lost, such as the type of each value.  If the value "3" is read from.
PROG Mobile Java Application Development PROG Mobile Java Application Development Data Storage, Continued.
1 Chapter 19 Binary I/O. 2 Motivations F Data stored in a text file – is represented in human-readable form –Text file –Readable –Java source programs.
MIDP Programming Networking. Chapter Objectives The CLDC Streams Model Generic Connection Framework (GCF) Supported Protocols Creating a Connection Review.
Chapter 15: Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams F Parsing Text Files F Random.
Chapter 15: Input and Output
MIDP Database Programming Using RMS: Jingwu He Csc 3360.
1 Putting Streams to use. 2 Stream Zoo C++ gives you istream, ostream, iostream, ifstream, ofstream, fstream, wistream, wifstream, istrsteam… (18) Java.
Java Programming: Advanced Topics 1 Input/Output and Serialization.
Java Programming, Second Edition Chapter Sixteen File Input and Output.
Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection.
The Record Store ( ) Frank Ducrest. The Record Store 2 The Record Store provides persistence of data between MIDlet runs not quite a DBMS API.
The Java Platform Micro Edition Java ME
Chapter 10: I/O Streams Input Streams Output Streams
The Java IO System Different kinds of IO Different kinds of operations
Fundamental of Java Programming
IO in java.
Sequential files creation & writing
IO in java.
Chapter 17 Input and Output
Java- I/O, SMS etc N Amanquah.
Chapter 17 Binary I/O.
Object Writing in files
OBJECT ORIENTED PROGRAMMING II LECTURE 21_1 GEORGE KOUTSOGIANNAKIS
I/O Basics.
Chapter 17 Binary I/O 1.
Chapter 17 Binary I/O Dr. Clincy - Lecture.
URL in Java C343 Lab (Week 13).
OBJECT ORIENTED PROGRAMMING II LECTURE 11_1 GEORGE KOUTSOGIANNAKIS
CS 240 – Advanced Programming Concepts
Streams and Readers The stream hierarchy is for reading bytes, the reader/writer hierarchy is for reading characters Unicode characters can be used internationally,
David Davenport Spring 2005
Presentation 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 Stores int 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 ByteArray OutputStream Data OutputStream InputStream ByteArray InputStream Data InputStream

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)  Connector TypeConnection = (TypeConnection) Connector.open(url) Connection Input Connection Output Connection FileConnectionSocketConnectionHttpConnection 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();