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 .. 66 67 68 69 70 71 < 72 > 73 74 75 76 77 78 .. 166 >> Next

308: // GET it via static method
309: 310: xmlBuf = MicroAmazonHttpGet.httpGet(sURL);
311: // very lazy instantiation
312: if (resultsScreen == null)
313: {
314: // results list
315: resultsScreen = new List("Results", List.EXCLUSIVE);
// removed adding Commands to resultsScreen no change.
321: 322: }
323: String [] productNames = null;
324: if (products == null)
325: {
326: products = new Vector(10); // Amazon returns 10 entries
327: }
328: else
329: {
330: products.setSize(0);
331: int rcnt = resultsScreen.size();
332: if (rcnt > 0)
333: {
334: // clear it
335: for (int i=rcnt - 1; i >= 0; i)
336: resultsScreen.delete(i);
337: }
338: 339: }
340: int index = 0;
Listing 22.5 (continued)
192 Item 22
341: String productName = null;
342: while ( (index = xmlBuf.indexOf(ELEMENT_PRODUCT_NAME, 2
index)) > 0)
343: {
344: int endIdx = xmlBuf.indexOf(ELEMENT_PRODUCT_NAME, 2
index + 1) ;
345: if (endIdx > index)
346: {
347: productName = xmlBuf.substring(index + 2
ELEMENT_ _PRODUCT_NAME.length() + 1, endIdx - 2);
348: products.addElement(productName);
349: }
350: index = endIdx + 1;
351: }
352: productName = null;
353:
354: int productCount = products.size();
355: if (products != null && productCount > 0)
356: {
357: // populate the list
358: for (int i=productCount - 1; i >= 0; i)
359: resultsScreen.append((String)products.elementAt(i), 2
null);
360:
361: // set the display to the results
362: display.setCurrent(resultsScreen);
363: }
// - removed the else block for brevity
370: ticker.printStats("Method doAmazonSearch()");
371: }
372: }
Listing 22.5 (continued)
The method doAmazonSearch() contains five optimizations:
Line 291 declares the method final for faster access.
Lines 293 and 294 use local variables.
Line 326 sets the size of the Vector.
Line 330 avoids instantiating a new Vector by reusing it.
Set References to Null. This will assist the garbage collector in more efficiently reclaiming unused memory. Line 352 makes the productName String available for reclamation.
Listing 22.6 presents the optimized version of BadMicroAmazonHttpGet. This version has changed drastically to increase performance and conserve memory. There are even further improvements available, like ensuring that the data returned from the Web server can be stored in memory (or pared down to do so).
J2ME Performance and Pitfalls 193
001: /* MicroAmazonHttpGet.java */
002: package org.javapitfalls.item22;
003:
// - removed Import statements -- no change.
007:
008: public class MicroAmazonHttpGet
009: {
010: public static final String DEVTAG = "D3AG4L7PI53LPH"; 011: static Timer ticker = new Timer();
012:
013: // Memory saving but not thread safe 014: private static StringBuffer urlBuf;
015: private static ByteArrayOutputStream baos;
016: private static byte [] buf;
017:
Listing 22.6 MicroAmazonHttpGet.java
The Class definition of MicroAmazonHttpGet has two optimizations. First, most of the static Strings have been eliminated to conserve memory. Second, all of the data members have been declared as class data members (to eliminate instantiation) but declared as static for fast access. This means that the class is no longer thread safe, but this is okay because only a single thread uses it. In fact, another improvement may be to just eliminate the class and roll the methods into MicroAmazonSearch:
018 public static final String createURL(String operation, String 2
target, String mode, String type,
019 String page) throws Exception
020 {
021: final String KEYWORD_MODE = "mode";
022 final String KEYWORD_TYPE = "type";
023 final String KEYWORD_PAGE = "page";
024
025 if (urlBuf == null)
026 {
027 urlBuf = new
StringBuffer("http://xml.amazon.com/onca/xml?v=1.0&t=webservices-20& 2
dev- -t=");
028 urlBuf.append(DEVTAG);
029 }
030 else
031 {
032 urlBuf.setLength(0);
033
urlBuf.append("http://xml.amazon.com/onca/xml?v=1.0&t=webservices- 2
20&dev-t=");
034 urlBuf.append(DEVTAG);
Listing 22.6 (continued)
194 Item 22
035 }
036
037 urlBuf.append('k');
038 urlBuf.append(operation);
039 urlBuf.append('=');
040 if (target != null)
041 {
042 target.trim();
043 target = replaceString(target, " ", "%20 , 0);
044
045 urlBuf.append(target);
046 }
047 else
048 throw new Exception("Invalid target");
049
050 // add Mode
051 urlBuf.append('&');
052 urlBuf.append(KEYWORD_MODE);
053 urlBuf.append('=');
054 urlBuf.append(mode);
055
// removed code for adding Type and Page -- no change, just Inlined
069 }
Listing 22.6 (continued)
The createURL method is a brand-new method that replaced all of the addXXX methods in the previous class. This class demonstrates six optimizations:
Line 18 demonstrates declaring a method both final and static for the fastest access. Lines 21 to 23 demonstrate using local variables.
Lines 25 to 29 demonstrate lazy instantiation.
Previous << 1 .. 66 67 68 69 70 71 < 72 > 73 74 75 76 77 78 .. 166 >> Next