Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 63 64 65 66 67 68 < 69 > 70 71 72 73 74 75 .. 166 >> Next

225: HttpConnection con = (HttpConnection) Connector.open(surl);
226: int respCode = con.getResponseCode();
227: System.out.println("Response code: " + respCode);
228:
229: InputStream in = con.openInputStream();
230: ByteArrayOutputStream baos = new ByteArrayOutputStream(); 231:
232: // read response
233: int b = 0;
234: while ( (b = in.read()) != -1)
235: {
Listing 22.4 BadMicroAmazonHttpGet.java (continued)
182 Item 22
236: baos.write(b);
237: }
238:
239: ticker.printStats("Method httpGet()");
240: return baos.toByteArray();
241: }
// - deleted main() method -- No change.
Listing 22.4 (continued)
In the BadMicroAmazonHttpGet class, all of the URL formatting methods remained unchanged (and thus were removed for brevity); however, the httpGet() method underwent significant changes. The porting changes are as follows:
There is no java.net package; instead, the networking classes are in javax.microedition.io (line 4). Not only does this confuse it with the J2ME version of java.io, this limits future differentiation between IO and networking support in the platform. This is a prime example of change for the sake of change that slows productivity by forcing a context switch without good reason.
There is no MalformedURLException or URL class; instead, the HttpCon-nection class accepts a String (line 225).
There is no URLConnection class; instead, you use an HttpConnection (line 225).
There is no getInputStream() method for the connection; instead, you use openInputStream(). Another example of useless incompatibility.
There was no BufferedReader class, so instead we read in bytes (instead of Strings) and wrote into a ByteArrayOutputStream (line 236). This then led to returing a byte array (line 240).
With the memory limit set at 128 KB, a run of BadMicroAmazonSearch produces:
Started Timer in doAmazonSearch(). Free Memory: 73076 Started Timer in httpGet(). Free Memory: 71628
url: http://xml.amazon.com/onca/xml?v=1.0&t=webservices-20&dev-t=D3AG4L7PI53LPH&KeywordSearch=daconta&mode=books&type=lite&page=1&f=xml Response code: 200
Method httpGet(): 5678. Free Memory: 63924 Have response. Size is: 8085 java.lang.OutOfMemoryError at
javax.microedition.lcdui.Display$DisplayAccessor.commandAction(+165)
at com.sun.kvem.midp.lcdui.EmulEventHandler$EventLoop.run(+459)
J2ME Performance and Pitfalls 183
When I increased the memory to 200 KB the program was able to run and produced the following:
Started Timer in doAmazonSearch(). Free Memory: 141096 Started Timer in httpGet(). Free Memory: 139648
url: http://xml.amazon.com/onca/xml?v=1.0&t=webservices-20&dev-t=D3AG4L7Pl53LPH&KeywordSearch=daconta&mode=books&type=lite&page=1&f=xml Response code: 200
Method httpGet(): 5718. Free Memory: 139152 Have response. Size is: 8085 # of products found: 8
Method doAmazonSearch(): 36082. Free Memory: 46240
Started Timer in doAmazonSearch(). Free Memory: 65044
Even though the BadMicroAmazonSearch ran within 200 KB, you should also notice the poor performance of the method (a noticeably long pause after selecting the Search command). The output of the run shows that the method took 36,082 milliseconds to run. The Wireless Toolkit also provides a Memory Monitor application, as shown in Figure 22.4. As you can see, the large peak in the memory graph occurs when the kxml package is parsing the XML document.
Unfortunately, we could not afford to allow the application to run within 200 KB in order to run it in the Palm emulator. At the time of this writing, the Palm emulator only allowed a Java application to have 64 KB of memory. Figure 22.5 shows our goal with the final code running under the Palm emulator.
Figure 22.4 The Wireless Toolkit Memory Monitor.
184 Item 22
Figure 22.5 MicroAmazonSearch running in a Palm emulator.
2002 Palm, Inc. All rights reserved.
Now we are ready to optimize our J2ME application to get it to have both adequate performance and memory consumption. Listing 22.5 is the optimized code for MicroAmazonSearch.java. We will not discuss the functionality of MicroAmazon-Search, since that has been covered in the preceding pages; instead, we will focus only on the optimizations.
001 /* MicroAmazonSearch.java */
002 package org.javapitfalls.item22;
003
004 import java.io.*;
005 import java.util.*;
006 import javax.microedition.midlet.*;
007 import javax.microedition.lcdui.*;
008
009 public class MicroAmazonSearch extends MIDlet implements
CommandListener
010 {
011 public static final int MAX_RECORDS = 10;
012
Listing 22.5 MicroAmazonSearch.java
J2ME Performance and Pitfalls 185
013 // commands
014 private Command searchCommand;
015 private Command detailsCommand;
// - removed additional Command references for brevity
019
020 // Alerts
021: Alert searchAlert;
022 Alert detailAlert;
// - removed other Alert references for brevity
025
026 // display
027 private Display display;
028
029 // screens
030 private Form searchScreen;
031 private List resultsScreen;
Previous << 1 .. 63 64 65 66 67 68 < 69 > 70 71 72 73 74 75 .. 166 >> Next