J2ME I/O Classes

                     Rohan Chandane
             rohan.chandane@indiatimes.com



This notes are created...
Basics
   CLDC-MIDP package for input/output
       java.io;
       javax.microedition.io;
   Contain classes
       ...
javax.microedition.io Package
   Networking support using
       The Generic Connection Framework
       Includes
     ...
The Generic Connection
Framework (GCF)
   Class hierarchy of the Generic
    Connection Framework




     GCF is a set o...
Continued…
   Connection Interface is at root
   Supported by
       datagram (packet)
       stream connections
     ...
Interfaces
   7 main Interfaces
       Connection
       InputConnection
       OutputConnection
       DatagramConne...
In brief - Interfaces
   The Connection interface
       Most basic connection type
       It can only be opened and cl...
Continued…
   The DatagramConnection interface
       For sending and receiving UDP packet
        oriented data
      ...
Continued…
   The StreamConncetion interface
       A two way stream connection to a device
       Include both input a...
Continued…
   The ContentConnection interface
       Subinterface of StreamConnection
       Provides access to some of...
Classes
   Connector class is used to open every
    type of connection in GCF
   open() method
       Connector Class ...
Continued…
   Few examples
       HTTP Connection
              Connector.open(quot;http://java.sun.com/developerquot;)...
Continued…
   CGF has 7 methods to open Connection
Connector (public class Connector)
  public static Connection open(Str...
Examples
// create ContentConnection
String url = quot;http://www.mysite.com/mypage.jspquot;
ContentConnection conn = (Con...
Read Image from server
// create ContentConnection
String url = quot;http://www.mysite.com/mypage.jspquot;
ContentConnecti...
HTTP support in MIDP
   GCF class support for HTTP
Continued…
// Create a HttpConnection
String url = quot;http://www.mysite.com/mypage.jspquot;
HttpConnection conn = (HttpC...
I/O Classes in J2ME
   I/O Class Library Package
       java.io
   11 Classes are mainly used
       InputStream
     ...
Continued…
    ByteArrayInputStream
        Input stream that reads data from the internal
         byte array
    Data...
Continued…
    Reader
        An abstract class that is used to enter stream
         of character
    OutputStream
   ...
Continued…
    DataOutputStram
         Stream that holds data written for primitive
          data type
    OutputStre...
InputStream
   Methods
       read()
            Reads data as byte integer, Returns -1 if end of
             input st...
OutputStream
   Methods
       write(int n)
            Used to write single byte of data
       write(byte b[])
     ...
Upcoming SlideShare
Loading in...5
×

J2ME IO Classes

7,313

Published on

J2ME IO Classes

Published in: Technology, Education
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
7,313
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
26
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

J2ME IO Classes

  1. 1. J2ME I/O Classes Rohan Chandane rohan.chandane@indiatimes.com This notes are created by me, Rohan Chandane as learning material while pursuing MSc (CA) from SICSR. (CC) 2005-07
  2. 2. Basics  CLDC-MIDP package for input/output  java.io;  javax.microedition.io;  Contain classes  For input/output (I/O)  For networking I/O  Contain Interface, (which MIDP adds)  Including HttpConnection  Defines methods and constants for HTTP connection
  3. 3. javax.microedition.io Package  Networking support using  The Generic Connection Framework  Includes  New socket  UDP  Serial connection types  Secure connection types  Push functionality
  4. 4. The Generic Connection Framework (GCF)  Class hierarchy of the Generic Connection Framework GCF is a set of interfaces defined within the javax.microedition.io package
  5. 5. Continued…  Connection Interface is at root  Supported by  datagram (packet)  stream connections  Input stream  Output stream
  6. 6. Interfaces  7 main Interfaces  Connection  InputConnection  OutputConnection  DatagramConnection  StreamConncetion  StreamConnectionNotifier  ContentConnection
  7. 7. In brief - Interfaces  The Connection interface  Most basic connection type  It can only be opened and closed  The InputConnection interface  Representation of stream based input connection  The OutputConnection interface  Representation of stream based output connection
  8. 8. Continued…  The DatagramConnection interface  For sending and receiving UDP packet oriented data  Following address scheme is used  {protocol}://[{host}]:[{port}]  Example  A datagram connection for accepting datagrams datagram://:1234  A datagram connection for sending to a server: datagram://123.456.789.12:1234
  9. 9. Continued…  The StreamConncetion interface  A two way stream connection to a device  Include both input and output connection  Combines the input and output connections  The StreamConncetionNotifier interface  Waits for a connection to be established  Returns a StreamConnection on which a communication link has been established.
  10. 10. Continued…  The ContentConnection interface  Subinterface of StreamConnection  Provides access to some of the basic meta data information provided by HTTP connections
  11. 11. Classes  Connector class is used to open every type of connection in GCF  open() method  Connector Class method  All connections are created using this static method  Following syntax used  Connector.Open(quot;protocol:address;parametersquot;);
  12. 12. Continued…  Few examples  HTTP Connection  Connector.open(quot;http://java.sun.com/developerquot;);  Datagram Connection  Connector.open(quot;datagram://address:port#quot;);  Communicate with a Port  Connector.open(quot;comm:0;baudrate=9600');  Open Files  Connector.open(quot;file:/myFile.txtquot;);  Socket Connection  Connector.open(quot;socket://localhost:9000quot;);
  13. 13. Continued…  CGF has 7 methods to open Connection Connector (public class Connector) public static Connection open(String name) public static Connection open(String name) public static Connection open(String name, int mode, boolean timeouts) public static DataInputStream openDataInputStream(String name) public static DataOutputStream openDataOutputStream(String name) public static InputStream openInputStream(String name) public static OutputStream openOutputStream(String name)
  14. 14. Examples // create ContentConnection String url = quot;http://www.mysite.com/mypage.jspquot; ContentConnection conn = (ContentConnection)Connector.open(url); // with connection open input stream InputStream is = conn.openInputStream();
  15. 15. Read Image from server // create ContentConnection String url = quot;http://www.mysite.com/mypage.jspquot; ContentConnection conn = (ContentConnection)Connector.open(url); // with connection open input stream InputStream is = conn.openInputStream(); // ContentConnection include lengh method int lenght = (int) Conn.getLength(); if(length != -1) { byte imagebyte[] = new byte[length]; is.read(imagebyte); }
  16. 16. HTTP support in MIDP  GCF class support for HTTP
  17. 17. Continued… // Create a HttpConnection String url = quot;http://www.mysite.com/mypage.jspquot; HttpConnection conn = (HttpConnection)Connector.open(url) // with connection open a input stream reader (java.io package class) InputSteamReader doc = new InputStreamReader(conn.openInputStream()); // Create a Secure Http Connection String url = quot;http://www.mysite.com/mypage.jspquot; HttpsConnection conn = (HttpsConnection)Connector.open(url) // with connection open a input stream reader (java.io package class) InputSteamReader doc = new InputStreamReader(conn.openInputStream());
  18. 18. I/O Classes in J2ME  I/O Class Library Package  java.io  11 Classes are mainly used  InputStream  Base class for inputstream  To obtain input stream from source  Input stream need to wait till web server delivers the data from web page
  19. 19. Continued…  ByteArrayInputStream  Input stream that reads data from the internal byte array  DataInputStream  Stream that enters data into primitive Java data type  InputStreamReader  Stream that reads data as text characters
  20. 20. Continued…  Reader  An abstract class that is used to enter stream of character  OutputStream  The base class for output stream  ByteArrayOutputStream  Output stream which write data into internal byte array
  21. 21. Continued…  DataOutputStram  Stream that holds data written for primitive data type  OutputStreamReader  Stream that writes data as text characters  Writer  An abstract class that is used to write streams of character  PrintStream  Output stream that enables the writing of primitive data types
  22. 22. InputStream  Methods  read()  Reads data as byte integer, Returns -1 if end of input stream  read(byte b[])  Takes array of byte as input and store it in an array  read(byte b[], int offset, int length)  Accept array of byte, offset is exact position in array where we want to store input data, length is max number of byte to be read
  23. 23. OutputStream  Methods  write(int n)  Used to write single byte of data  write(byte b[])  Allow to write array of bytes  write(byte b[], int offset, int length)  Accept array of byte, offset is exact position in array where we want to write data, length is max number of byte to be written

×