Sometimes you want to use quicker GCF vs. java.net in OJEC 1.1 (Java ME CDC)

There are times that Java ME specific Generic Connection Framework (GCF) programming is quicker than Java SE style java.net.* programming. For example, when there's need for an HTTP connection, using the GCF HttpConnection in Java ME CDC (now called OJEC 1.1) was sometimes more convenient on a Java ME device.

Source code sample:


     import java.io.*;
     import java.net.*;
     import javax.microedition.io.*;

     HttpConnection c;
     InputStream is;
     BufferedInputStream bis;
     final static int    MAX_BUFFER = 204800;    // 200 KB buffer size

     try {

          String url = new String("http://oracle.com");
          
          System.out.println("Attempting to connect to: "+url);
          c = (HttpConnection)Connector.open(url);
          is = c.openInputStream();
          bis = new BufferedInputStream(is);

          // Initialize loop variables
          int index = 0;
          int numBytes = 0;
          byte buffer[] = new byte[MAX_BUFFER];

          // Loop: read to end of document
          while (numBytes != -1) {
            numBytes = bis.read(buffer, index, MAX_BUFFER-index);
            if (numBytes > 0) {
                String newStr = new String(buffer, 0, numBytes);
                System.out.println(index+": "+newStr);
                index += numBytes;
            }
          }

          // Close everything
          bis.close();
          is.close();
          c.close();
        } catch (IOException ioe) {
          // Handle the exception.
            ioe.printStackTrace();
        }

That's one of the differences from the past between Java ME technology and Java SE technology, which were two distinct legacy platforms that you could base Java technology on. Now moving to one combined Java EC governance model in the JCP will make it easier to take advantages of the combined Java ME and Java SE APIs and single Java language moving forward.

Comments:

Post a Comment:
Comments are closed for this entry.
About

Hinkmond Wong's blog on making Machine to Machine (M2M) and the Incredible Internet of Things (IoT) smarter with Java Embedded Technologies

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
2
3
4
5
6
7
8
9
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today