<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;CUMHRXg7cCp7ImA9WhRVEUU.&quot;"><id>tag:blogger.com,1999:blog-1425998245372764769</id><updated>2012-01-10T16:17:14.608+08:00</updated><category term="hibernate" /><category term="javascript" /><category term="java" /><category term="web" /><category term="php" /><category term="encoding" /><category term="cmd shell" /><category term="formatting" /><category term="enumeration" /><category term="google web toolkit" /><category term="cookie" /><category term="string" /><category term="objective-c" /><category term="internationalization" /><category term="android" /><category term="iphone" /><category term="context-menu" /><category term="annotation" /><category term="ScrollView" /><category term="python" /><category term="html" /><category term="optimization" /><category term="TextView" /><category term="windows" /><category term="performance" /><category term="database" /><category term="dalvik" /><title>Code and Help Code</title><subtitle type="html">What I learn today, may it be useful to you.</subtitle><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://biginteger.blogspot.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://biginteger.blogspot.com/" /><author><name>Yuku Sugianto</name><uri>https://profiles.google.com/117908749193966045672</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-xhNxoKzRFg8/AAAAAAAAAAI/AAAAAAAAybo/lCNLgVyCmog/s512-c/photo.jpg" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>22</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/Play-playWithCode" /><feedburner:info uri="play-playwithcode" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry gd:etag="W/&quot;CUcASHY8cCp7ImA9WhRVEUU.&quot;"><id>tag:blogger.com,1999:blog-1425998245372764769.post-8812043969238603872</id><published>2012-01-10T15:59:00.001+08:00</published><updated>2012-01-10T16:10:49.878+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-10T16:10:49.878+08:00</app:edited><title>Convert RGB to HSL and vice-versa in Java</title><content type="html">I wanted to add colored labels to my Android &lt;a href="http://androidbible.blogspot.com/"&gt;application&lt;/a&gt;&amp;nbsp;and I wanted to make it such that the users need only to select one color, the background color, and the foreground color would be the same color, only lighter or darker.&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
I thought I needed a RGB to HSL (Hue, Saturation, Lightness) converter, but Android SDK only provides RGB to HSV value, which &lt;a href="http://en.wikipedia.org/wiki/HSL_and_HSV"&gt;has some similarity but different&lt;/a&gt;.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Based on the above Wikipedia article, I wrote the Java code to convert between RGB and HSL. Hopefully this can be useful to your project.&lt;/div&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;rgb is an int (with alpha ignored) 0x&lt;i&gt;rrggbb&lt;/i&gt;&lt;/li&gt;
&lt;li&gt;hsl is an array of 3 floats:&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;[0] is hue, 0..360&lt;/li&gt;
&lt;li&gt;[1] is saturation, 0..1&lt;/li&gt;
&lt;li&gt;[2] is lightness, 0..1&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;pre&gt;
public static void rgbToHsl(int rgb, float[] hsl) {
 float r = ((0x00ff0000 &amp; rgb) &amp;gt;&amp;gt; 16) / 255.f;
 float g = ((0x0000ff00 &amp; rgb) &amp;gt;&amp;gt; 8) / 255.f;
 float b = ((0x000000ff &amp; rgb)) / 255.f;
 float max = Math.max(Math.max(r, g), b);
 float min = Math.min(Math.min(r, g), b);
 float c = max - min;
 
 float h_ = 0.f;
 if (c == 0) {
  h_ = 0;
 } else if (max == r) {
  h_ = (float)(g-b) / c;
  if (h_ &amp;lt; 0) h_ += 6.f;
 } else if (max == g) {
  h_ = (float)(b-r) / c + 2.f;
 } else if (max == b) {
  h_ = (float)(r-g) / c + 4.f;
 }
 float h = 60.f * h_;
 
 float l = (max + min) * 0.5f;
 
 float s;
 if (c == 0) {
  s = 0.f;
 } else {
  s = c / (1 - Math.abs(2.f * l - 1.f));
 }
 
 hsl[0] = h;
 hsl[1] = s;
 hsl[2] = l;
}

public static int hslToRgb(float[] hsl) {
 float h = hsl[0];
 float s = hsl[1];
 float l = hsl[2];
 
 float c = (1 - Math.abs(2.f * l - 1.f)) * s;
 float h_ = h / 60.f;
 float h_mod2 = h_;
 if (h_mod2 &amp;gt;= 4.f) h_mod2 -= 4.f;
 else if (h_mod2 &amp;gt;= 2.f) h_mod2 -= 2.f;
 
 float x = c * (1 - Math.abs(h_mod2 - 1));
 float r_, g_, b_;
 if (h_ &amp;lt; 1)      { r_ = c; g_ = x; b_ = 0; }
 else if (h_ &amp;lt; 2) { r_ = x; g_ = c; b_ = 0; }
 else if (h_ &amp;lt; 3) { r_ = 0; g_ = c; b_ = x; }
 else if (h_ &amp;lt; 4) { r_ = 0; g_ = x; b_ = c; }
 else if (h_ &amp;lt; 5) { r_ = x; g_ = 0; b_ = c; }
 else             { r_ = c; g_ = 0; b_ = x; }
 
 float m = l - (0.5f * c); 
 int r = (int)((r_ + m) * (255.f) + 0.5f);
 int g = (int)((g_ + m) * (255.f) + 0.5f);
 int b = (int)((b_ + m) * (255.f) + 0.5f);
 return r &amp;lt;&amp;lt; 16 | g &amp;lt;&amp;lt; 8 | b;
}&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1425998245372764769-8812043969238603872?l=biginteger.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/agh7Hi4EKz8H93psjZ3EmW9nErA/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/agh7Hi4EKz8H93psjZ3EmW9nErA/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/agh7Hi4EKz8H93psjZ3EmW9nErA/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/agh7Hi4EKz8H93psjZ3EmW9nErA/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Play-playWithCode/~4/Jm323B2MsG4" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://biginteger.blogspot.com/feeds/8812043969238603872/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://biginteger.blogspot.com/2012/01/convert-rgb-to-hsl-and-vice-versa-in.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/8812043969238603872?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/8812043969238603872?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Play-playWithCode/~3/Jm323B2MsG4/convert-rgb-to-hsl-and-vice-versa-in.html" title="Convert RGB to HSL and vice-versa in Java" /><author><name>Yuku Sugianto</name><uri>https://profiles.google.com/117908749193966045672</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-xhNxoKzRFg8/AAAAAAAAAAI/AAAAAAAAybo/lCNLgVyCmog/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://biginteger.blogspot.com/2012/01/convert-rgb-to-hsl-and-vice-versa-in.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CE8GRng_fyp7ImA9WhRWGEk.&quot;"><id>tag:blogger.com,1999:blog-1425998245372764769.post-4322939454688050390</id><published>2012-01-06T17:34:00.001+08:00</published><updated>2012-01-06T17:40:27.647+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-06T17:40:27.647+08:00</app:edited><title>Add "screenSize" flag to android:configChanges when targetting API level 13 (Android 3.2) or newer</title><content type="html">If you're setting "keyboardHidden|orientation" to android:configChanges in order to prevent activity from restarting itself when the device is rotated, you might be surprised when you find your activity restarts itself when running on Android 3.2 or newer.&lt;br /&gt;
&lt;br /&gt;
The reason is, starting from Android 3.2 (API Level 13), rotated device will also trigger "screenSize" change. From attrs_manifest.xml:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;!-- The current available screen size has changed. &amp;nbsp;If applications don't
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;target at least {@link android.os.Build.VERSION_CODES#HONEYCOMB_MR2}
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;then the activity will always handle this itself (the change
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;will not result in a restart). &amp;nbsp;This represents a change in the
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;currently available size, so will change when the user switches
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;between landscape and portrait. --&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;flag name="screenSize" value="0x0400" /&amp;gt;
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div&gt;
It means, if your android:targetSdkVersion is 13 or more, your application will receive "screenSize" config change. Therefore, to prevent your activity from restarting, add "screenSize". E.g.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; android:configChanges="keyboardHidden|orientation|screenSize"&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Hope this helps you.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1425998245372764769-4322939454688050390?l=biginteger.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/0EbaAEotI8ysuZrfBNqZhTV5LNg/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/0EbaAEotI8ysuZrfBNqZhTV5LNg/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/0EbaAEotI8ysuZrfBNqZhTV5LNg/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/0EbaAEotI8ysuZrfBNqZhTV5LNg/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Play-playWithCode/~4/kIUIE5nwDMM" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://biginteger.blogspot.com/feeds/4322939454688050390/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://biginteger.blogspot.com/2012/01/add-screensize-flag-to.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/4322939454688050390?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/4322939454688050390?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Play-playWithCode/~3/kIUIE5nwDMM/add-screensize-flag-to.html" title="Add &quot;screenSize&quot; flag to android:configChanges when targetting API level 13 (Android 3.2) or newer" /><author><name>Yuku Sugianto</name><uri>https://profiles.google.com/117908749193966045672</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-xhNxoKzRFg8/AAAAAAAAAAI/AAAAAAAAybo/lCNLgVyCmog/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://biginteger.blogspot.com/2012/01/add-screensize-flag-to.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEUEQH04eip7ImA9WhRRFU4.&quot;"><id>tag:blogger.com,1999:blog-1425998245372764769.post-4006494241978095819</id><published>2011-11-29T10:02:00.001+08:00</published><updated>2011-11-29T10:03:21.332+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-29T10:03:21.332+08:00</app:edited><title>Convert UNIX timestamp to human date using command-line</title><content type="html">I'm working on a project where date/time are sent in UNIX timestamp. Sometimes when I'm debugging, I want to check whether the usually 10-digit number is correct.&lt;br /&gt;
&lt;br /&gt;
The easiest way I found is using the command-line terminal:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;~$ date -r 1322532029&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;Tue Nov 29 10:00:29 SGT 2011&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1425998245372764769-4006494241978095819?l=biginteger.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/e7utRVYAVbjwGWvLditB2YahF_0/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/e7utRVYAVbjwGWvLditB2YahF_0/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/e7utRVYAVbjwGWvLditB2YahF_0/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/e7utRVYAVbjwGWvLditB2YahF_0/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Play-playWithCode/~4/8RDgzj4Ty0c" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://biginteger.blogspot.com/feeds/4006494241978095819/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://biginteger.blogspot.com/2011/11/convert-unix-timestamp-to-human-date.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/4006494241978095819?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/4006494241978095819?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Play-playWithCode/~3/8RDgzj4Ty0c/convert-unix-timestamp-to-human-date.html" title="Convert UNIX timestamp to human date using command-line" /><author><name>Yuku Sugianto</name><uri>https://profiles.google.com/117908749193966045672</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-xhNxoKzRFg8/AAAAAAAAAAI/AAAAAAAAybo/lCNLgVyCmog/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://biginteger.blogspot.com/2011/11/convert-unix-timestamp-to-human-date.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEMHRXs5eyp7ImA9WhRTE0U.&quot;"><id>tag:blogger.com,1999:blog-1425998245372764769.post-8995225049246853786</id><published>2011-11-04T12:47:00.001+08:00</published><updated>2011-11-04T14:00:34.523+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-04T14:00:34.523+08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="dalvik" /><category scheme="http://www.blogger.com/atom/ns#" term="optimization" /><category scheme="http://www.blogger.com/atom/ns#" term="android" /><title>List of Inlined/intrinsic methods in Android's Dalvik VM</title><content type="html">Android's &lt;a href="http://developer.android.com/guide/practices/design/performance.html"&gt;Designing for Performance&lt;/a&gt; document, states:&lt;br /&gt;
&lt;blockquote class="tr_bq"&gt;&lt;div style="background-color: white; border-bottom-width: 0px; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #333333; font-family: arial, sans-serif; font-size: 13px; line-height: 1.3em; margin-bottom: 1em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;In addition to all the usual reasons to prefer library code over rolling your own, bear in mind that the system is at liberty to replace calls to library methods with hand-coded assembler, which may be better than the best code the JIT can produce for the equivalent Java. The typical example here is&amp;nbsp;&lt;code style="color: #007000; line-height: 1em;"&gt;String.indexOf&lt;/code&gt;&amp;nbsp;and friends, which Dalvik replaces with an inlined intrinsic. Similarly, the&amp;nbsp;&lt;code style="color: #007000; line-height: 1em;"&gt;System.arraycopy&lt;/code&gt;&amp;nbsp;method is about 9x faster than a hand-coded loop on a Nexus One with the JIT.&lt;/div&gt;&lt;/blockquote&gt;Unfortunately, as are other Android SDK documentation, this doesn't tell you much &lt;i&gt;which&lt;/i&gt;&amp;nbsp;methods gets inlined with machine code.&lt;br /&gt;
&lt;br /&gt;
Examining the Dalvik VM source code, I found dalvik/vm/InlineNative.c that starts with an introduction:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
&amp;nbsp;* Inlined native functions. &amp;nbsp;These definitions replace interpreted or&lt;br /&gt;
&amp;nbsp;* native implementations at runtime; "intrinsic" might be a better word.&lt;br /&gt;
&amp;nbsp;*/&lt;br /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;br /&gt;
So the inlined methods are:&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;class String:&lt;/li&gt;
&lt;ul&gt;&lt;li&gt;charAt(int)&lt;/li&gt;
&lt;li&gt;compareTo(String)&lt;/li&gt;
&lt;li&gt;equals(Object)&lt;/li&gt;
&lt;li&gt;fastIndexOf(int, int)&lt;/li&gt;
&lt;li&gt;isEmpty()&lt;/li&gt;
&lt;li&gt;length()&lt;/li&gt;
&lt;/ul&gt;&lt;li&gt;class Math:&lt;/li&gt;
&lt;ul&gt;&lt;li&gt;abs(int)&lt;/li&gt;
&lt;li&gt;abs(long)&lt;/li&gt;
&lt;li&gt;abs(float)&lt;/li&gt;
&lt;li&gt;abs(double)&lt;/li&gt;
&lt;li&gt;min(int, int)&lt;/li&gt;
&lt;li&gt;max(int, int)&lt;/li&gt;
&lt;li&gt;sqrt(double)&lt;/li&gt;
&lt;li&gt;cos(double)&lt;/li&gt;
&lt;li&gt;sin(double)&lt;/li&gt;
&lt;/ul&gt;&lt;li&gt;class Float:&lt;/li&gt;
&lt;ul&gt;&lt;li&gt;floatToIntBits(float)&lt;/li&gt;
&lt;li&gt;floatToRawIntBits(float)&lt;/li&gt;
&lt;li&gt;intBitsToFloat(int)&lt;/li&gt;
&lt;/ul&gt;&lt;li&gt;class Double:&lt;/li&gt;
&lt;ul&gt;&lt;li&gt;doubleToLongBits(double)&lt;/li&gt;
&lt;li&gt;doubleToRawLongBits(double)&lt;/li&gt;
&lt;li&gt;longBitsToDouble(long)&lt;/li&gt;
&lt;/ul&gt;&lt;/ul&gt;&lt;div&gt;That's it! Now don't be so scared to use those methods in your code.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;Bonus: fastIndexOf ? What is that? Java's String has indexOf, but never an fastIndexOf.&lt;br /&gt;
&lt;br /&gt;
Answer: The implementation of String's indexOf(int) or indexOf(int, int) that Android uses, calls fastIndexOf if the char to be searched is &amp;lt;= 0xffff (which most probably is). So, if your code has a call of indexOf('x'), the native implementation will still be used.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1425998245372764769-8995225049246853786?l=biginteger.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/i4oWDcJLobRhu0VRvecSqsrAQT8/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/i4oWDcJLobRhu0VRvecSqsrAQT8/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/i4oWDcJLobRhu0VRvecSqsrAQT8/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/i4oWDcJLobRhu0VRvecSqsrAQT8/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Play-playWithCode/~4/BvIn5drFMDQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://biginteger.blogspot.com/feeds/8995225049246853786/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://biginteger.blogspot.com/2011/11/androids-designing-for-performance.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/8995225049246853786?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/8995225049246853786?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Play-playWithCode/~3/BvIn5drFMDQ/androids-designing-for-performance.html" title="List of Inlined/intrinsic methods in Android's Dalvik VM" /><author><name>Yuku Sugianto</name><uri>https://profiles.google.com/117908749193966045672</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-xhNxoKzRFg8/AAAAAAAAAAI/AAAAAAAAybo/lCNLgVyCmog/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://biginteger.blogspot.com/2011/11/androids-designing-for-performance.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkcDQ3c7fip7ImA9WhdaEkk.&quot;"><id>tag:blogger.com,1999:blog-1425998245372764769.post-2419725524936838196</id><published>2011-10-22T08:40:00.001+08:00</published><updated>2011-10-22T08:41:12.906+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-10-22T08:41:12.906+08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="android" /><title>AnimationDrawable doesn't start after calling start()</title><content type="html">Frame animations (sometimes declared in xml as &amp;lt;animation&amp;gt;) produces an AnimationDrawable. However, in some situations, such as using it as one of the ListView items, the animation doesn't start. Even if you call &lt;b&gt;start()&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
The solution is simple. Just call &lt;b&gt;stop()&lt;/b&gt; and then &lt;b&gt;start()&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
In the source code of AnimationDrawable, the method &lt;b&gt;start()&lt;/b&gt; can sometimes do nothing.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;public void start() {
        if (!isRunning()) {
            run();
        }
    }
&lt;/pre&gt;&lt;br /&gt;
I have encountered this twice. I hope this helps you.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1425998245372764769-2419725524936838196?l=biginteger.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/Y8nJ7Buoe7LjjrP-CYtLHf5iLyo/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Y8nJ7Buoe7LjjrP-CYtLHf5iLyo/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/Y8nJ7Buoe7LjjrP-CYtLHf5iLyo/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Y8nJ7Buoe7LjjrP-CYtLHf5iLyo/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Play-playWithCode/~4/KKgnVbqA4IE" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://biginteger.blogspot.com/feeds/2419725524936838196/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://biginteger.blogspot.com/2011/10/animationdrawable-doesnt-start-after.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/2419725524936838196?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/2419725524936838196?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Play-playWithCode/~3/KKgnVbqA4IE/animationdrawable-doesnt-start-after.html" title="AnimationDrawable doesn't start after calling start()" /><author><name>Yuku Sugianto</name><uri>https://profiles.google.com/117908749193966045672</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-xhNxoKzRFg8/AAAAAAAAAAI/AAAAAAAAybo/lCNLgVyCmog/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://biginteger.blogspot.com/2011/10/animationdrawable-doesnt-start-after.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0YAR3oyeCp7ImA9WhdQEEg.&quot;"><id>tag:blogger.com,1999:blog-1425998245372764769.post-4763800638528157119</id><published>2011-08-11T16:25:00.000+08:00</published><updated>2011-08-11T16:25:46.490+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-08-11T16:25:46.490+08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="android" /><title>Don't use Adapter#isEnabled(int) to disable individual list items (Android)</title><content type="html">When we have items in a ListView, we may want to disable some of the items.&lt;br /&gt;
&lt;br /&gt;
Disabling some of the items can be done by overriding &lt;b&gt;isEnabled(int position)&lt;/b&gt; on the adapter, but unfortunately the list item dividers are not drawn anymore! Overriding &lt;b&gt;isEnabled(int position) &lt;/b&gt;was meant to be used to create some kind of "separator" or "list header".&lt;br /&gt;
&lt;br /&gt;
The correct way to disable an item in a ListView is to call &lt;b&gt;setEnabled(false)&lt;/b&gt; on the view of the item itself. In other words, call setEnabled(false) on the view that is returned from &lt;b&gt;getView(int position, View convertView, ViewGroup parent)&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
Reference: &lt;a href="http://groups.google.com/group/android-developers/browse_thread/thread/204448e36fa0aba3"&gt;Romain Guy's post on android-developers&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1425998245372764769-4763800638528157119?l=biginteger.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/FsayZj7je7_hTMHcwjZ13NRnHy4/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/FsayZj7je7_hTMHcwjZ13NRnHy4/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/FsayZj7je7_hTMHcwjZ13NRnHy4/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/FsayZj7je7_hTMHcwjZ13NRnHy4/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Play-playWithCode/~4/1Cj0LlNTK9M" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://biginteger.blogspot.com/feeds/4763800638528157119/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://biginteger.blogspot.com/2011/08/dont-use-adapterisenabledint-to-disable.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/4763800638528157119?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/4763800638528157119?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Play-playWithCode/~3/1Cj0LlNTK9M/dont-use-adapterisenabledint-to-disable.html" title="Don't use Adapter#isEnabled(int) to disable individual list items (Android)" /><author><name>Yuku Sugianto</name><uri>https://profiles.google.com/117908749193966045672</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-xhNxoKzRFg8/AAAAAAAAAAI/AAAAAAAAybo/lCNLgVyCmog/s512-c/photo.jpg" /></author><thr:total>1</thr:total><feedburner:origLink>http://biginteger.blogspot.com/2011/08/dont-use-adapterisenabledint-to-disable.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkcMQXczcCp7ImA9WhZXGUw.&quot;"><id>tag:blogger.com,1999:blog-1425998245372764769.post-6153419689678327889</id><published>2011-05-09T11:01:00.000+08:00</published><updated>2011-05-09T11:01:20.988+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-05-09T11:01:20.988+08:00</app:edited><title>Using convertView and returning the correct view in Adapter's getView in one line!</title><content type="html">Are you tired seeing or typing these lines over and over again?&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: java"&gt;public View getView(int position, View convertView, ViewGroup parent) {
    View v;
    if (convertView == null) {
        v = LayoutInflater.from(getApplicationContext()).inflate(R.layout.row, null);
    } else {
        v = convertView;
    }
    // later:
    return v;
}&lt;/pre&gt;&lt;br /&gt;
Do that in one line!&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: java"&gt;View v = convertView != null? convertView: LayoutInflater.from(getApplicationContext()).inflate(R.layout.row, null);&lt;/pre&gt;&lt;br /&gt;
This seems easy but at first I didn't think of that!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1425998245372764769-6153419689678327889?l=biginteger.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/dKLm4IIab8QR4MT95eq2PbCyIUw/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/dKLm4IIab8QR4MT95eq2PbCyIUw/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/dKLm4IIab8QR4MT95eq2PbCyIUw/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/dKLm4IIab8QR4MT95eq2PbCyIUw/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Play-playWithCode/~4/kihkR6NYqpk" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://biginteger.blogspot.com/feeds/6153419689678327889/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://biginteger.blogspot.com/2011/05/using-convertview-and-returning-correct.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/6153419689678327889?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/6153419689678327889?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Play-playWithCode/~3/kihkR6NYqpk/using-convertview-and-returning-correct.html" title="Using convertView and returning the correct view in Adapter's getView in one line!" /><author><name>Yuku Sugianto</name><uri>https://profiles.google.com/117908749193966045672</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-xhNxoKzRFg8/AAAAAAAAAAI/AAAAAAAAybo/lCNLgVyCmog/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://biginteger.blogspot.com/2011/05/using-convertview-and-returning-correct.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0INRn44eip7ImA9WhZQF0w.&quot;"><id>tag:blogger.com,1999:blog-1425998245372764769.post-7191495791451330763</id><published>2011-04-25T16:19:00.000+08:00</published><updated>2011-04-25T16:19:57.032+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-25T16:19:57.032+08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="java" /><category scheme="http://www.blogger.com/atom/ns#" term="android" /><title>Better findViewById that doesn't require you to type-cast the result</title><content type="html">If you started developing Android apps, you must have known the &lt;a href="http://developer.android.com/reference/android/view/View.html#findViewById(int)"&gt;findViewById&lt;/a&gt; method. That is the method that glues UI and code.&lt;br /&gt;
&lt;br /&gt;
However, findViewById returns the generic View, so you need to type-cast it to the specific view if you want to do widget-specific operations on it (e.g. setText).&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: java"&gt;TextView lAddress = (TextView) findViewById(R.id.lAddress);&lt;/pre&gt;&lt;br /&gt;
or,&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: java"&gt;ImageButton bSubmit = (ImageButton) findViewById(R.id.bSubmit);&lt;/pre&gt;&lt;br /&gt;
That's so annoying, even more if you change the type of the widget with similar but unrelated ones, like swapping a Button with an ImageButton.&lt;br /&gt;
&lt;br /&gt;
Solution: use a &lt;a href="http://download.oracle.com/javase/tutorial/extra/generics/methods.html"&gt;generic method&lt;/a&gt; that returns you an object of the type that you request.&lt;br /&gt;
&lt;br /&gt;
Paste the 2 methods below anywhere you like.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: java"&gt;/**
 * Convenience method of findViewById
 */
@SuppressWarnings("unchecked")
public static &amp;lt;T extends View&amp;gt; T getView(View parent, int id) {
 return (T) parent.findViewById(id);
}

/**
 * Convenience method of findViewById
 */
@SuppressWarnings("unchecked")
public static &amp;lt;T extends View&amp;gt; T getView(Activity activity, int id) {
 return (T) activity.findViewById(id);
}
&lt;/pre&gt;&lt;br /&gt;
Now, you can just call them like:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: java"&gt;TextView lAddress = getView(this, R.id.lAddress);

// or, if not inside an activity, let's say in an Adapter's getView:
View res = convertView != null? convertView: getLayoutInflater().inflate(R.layout.item_person, null);
TextView lAddress = getView(res, R.id.lAddress);&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1425998245372764769-7191495791451330763?l=biginteger.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/XfRrtRtITVGjeyqtBrBOYWcI3_c/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/XfRrtRtITVGjeyqtBrBOYWcI3_c/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/XfRrtRtITVGjeyqtBrBOYWcI3_c/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/XfRrtRtITVGjeyqtBrBOYWcI3_c/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Play-playWithCode/~4/d6f-YH1sJWU" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://biginteger.blogspot.com/feeds/7191495791451330763/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://biginteger.blogspot.com/2011/04/better-findviewbyid-that-doesnt-require.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/7191495791451330763?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/7191495791451330763?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Play-playWithCode/~3/d6f-YH1sJWU/better-findviewbyid-that-doesnt-require.html" title="Better findViewById that doesn't require you to type-cast the result" /><author><name>Yuku Sugianto</name><uri>https://profiles.google.com/117908749193966045672</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-xhNxoKzRFg8/AAAAAAAAAAI/AAAAAAAAybo/lCNLgVyCmog/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://biginteger.blogspot.com/2011/04/better-findviewbyid-that-doesnt-require.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CE8HRHs9fip7ImA9WhZRGEs.&quot;"><id>tag:blogger.com,1999:blog-1425998245372764769.post-8919536552373524342</id><published>2011-04-15T18:20:00.000+08:00</published><updated>2011-04-15T18:20:35.566+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-15T18:20:35.566+08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="java" /><category scheme="http://www.blogger.com/atom/ns#" term="android" /><title>Call getItem instead of parent.getItemAtPosition</title><content type="html">On your list item click listener, usually you will need to get the item clicked. You can do it with:&lt;br /&gt;
&lt;style type="text/css"&gt;
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #942193}
span.s1 {color: #000000}
span.s2 {color: #ff2f92}
span.s3 {color: #0096ff}
span.s4 {color: #ff9300}
span.Apple-tab-span {white-space:pre}
&lt;/style&gt;   &lt;br /&gt;
&lt;div class="p1"&gt;News&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="s2"&gt;news&lt;/span&gt;&lt;span class="s1"&gt; = (&lt;/span&gt;News&lt;span class="s1"&gt;) &lt;/span&gt;&lt;b&gt;&lt;span class="s3"&gt;parent&lt;/span&gt;&lt;span class="s1"&gt;.&lt;/span&gt;&lt;span class="s4"&gt;getItemAtPosition&lt;/span&gt;&lt;/b&gt;&lt;span class="s1"&gt;(&lt;/span&gt;&lt;span class="s3"&gt;position&lt;/span&gt;&lt;span class="s1"&gt;);&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
However, I don't recommend that, mainly because of the cast. If you change the adapter to operate on items of different type, the compiler will not catch the error.&lt;br /&gt;
&lt;br /&gt;
Instead, access the adapter directly.&lt;br /&gt;
&lt;br /&gt;
&lt;style type="text/css"&gt;
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco}
span.s1 {color: #942193}
span.s2 {color: #ff2f92}
&lt;/style&gt;   &lt;br /&gt;
&lt;div class="p1"&gt;&lt;span class="s1"&gt;News&lt;/span&gt; &lt;span class="s2"&gt;news&lt;/span&gt; = &lt;b&gt;adapter.getItem&lt;/b&gt;(position);&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
This is possible only if you make the adapter return the correct item type instead of Object:&lt;br /&gt;
&lt;style type="text/css"&gt;
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #942193}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco}
span.s1 {color: #87872b}
span.s2 {color: #000000}
span.s3 {color: #ff9300}
span.s4 {color: #0096ff}
span.s5 {color: #931a68}
span.s6 {color: #0326cc}
span.Apple-tab-span {white-space:pre}
&lt;/style&gt;   &lt;br /&gt;
&lt;div class="p1"&gt;&lt;span class="s1"&gt;public&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;b&gt;News&lt;/b&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="s3"&gt;getItem&lt;/span&gt;&lt;span class="s2"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;int&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="s4"&gt;position&lt;/span&gt;&lt;span class="s2"&gt;) {&lt;/span&gt;&lt;/div&gt;&lt;div class="p2"&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;span class="s5"&gt;return&lt;/span&gt; &lt;span class="s6"&gt;list&lt;/span&gt; == &lt;span class="s1"&gt;null&lt;/span&gt; || &lt;span class="s4"&gt;position&lt;/span&gt; &amp;gt;= &lt;span class="s6"&gt;list&lt;/span&gt;.&lt;span class="s6"&gt;items&lt;/span&gt;.&lt;span class="s3"&gt;size&lt;/span&gt;()? &lt;span class="s1"&gt;null&lt;/span&gt;: &lt;span class="s6"&gt;list&lt;/span&gt;.&lt;span class="s6"&gt;items&lt;/span&gt;.&lt;span class="s3"&gt;get&lt;/span&gt;(&lt;span class="s4"&gt;position&lt;/span&gt;);&lt;/div&gt;&lt;div class="p2"&gt;}&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
(for long-time Java user, you may not have noticed that that was possible starting Java 5.)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1425998245372764769-8919536552373524342?l=biginteger.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/Qok5kuqhffiQ8E4hYVgvHVwSf-4/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Qok5kuqhffiQ8E4hYVgvHVwSf-4/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/Qok5kuqhffiQ8E4hYVgvHVwSf-4/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Qok5kuqhffiQ8E4hYVgvHVwSf-4/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Play-playWithCode/~4/jvgPssBI2mM" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://biginteger.blogspot.com/feeds/8919536552373524342/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://biginteger.blogspot.com/2011/04/call-getitem-instead-of.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/8919536552373524342?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/8919536552373524342?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Play-playWithCode/~3/jvgPssBI2mM/call-getitem-instead-of.html" title="Call getItem instead of parent.getItemAtPosition" /><author><name>Yuku Sugianto</name><uri>https://profiles.google.com/117908749193966045672</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-xhNxoKzRFg8/AAAAAAAAAAI/AAAAAAAAybo/lCNLgVyCmog/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://biginteger.blogspot.com/2011/04/call-getitem-instead-of.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0MHQXYzeCp7ImA9Wx5WEEk.&quot;"><id>tag:blogger.com,1999:blog-1425998245372764769.post-7487659730226574912</id><published>2010-09-21T13:03:00.001+08:00</published><updated>2010-09-21T13:03:50.880+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-09-21T13:03:50.880+08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="java" /><category scheme="http://www.blogger.com/atom/ns#" term="context-menu" /><category scheme="http://www.blogger.com/atom/ns#" term="android" /><title>Solution to: onContextItemSelected not called after selecting a context menu</title><content type="html">Here is a solution to Android development problem, where the &lt;a href="http://developer.android.com/reference/android/app/Activity.html#onContextItemSelected(android.view.MenuItem)"&gt;onContextItemSelected&lt;/a&gt; method is not called even after you see the context menu popup and selecting one of the items there.&lt;br /&gt;
&lt;br /&gt;
1. Make sure that&amp;nbsp;&lt;a href="http://developer.android.com/reference/android/app/Activity.html#registerForContextMenu(android.view.View)"&gt;registerForContextMenu&lt;/a&gt; has been called, with the parameter the view you have the context menu on. For ListActivity with a context menu on the items, call&amp;nbsp;&lt;a href="http://developer.android.com/reference/android/app/Activity.html#registerForContextMenu(android.view.View)"&gt;registerForContextMenu&lt;/a&gt;(&lt;a href="http://developer.android.com/reference/android/app/ListActivity.html#getListView()"&gt;getListView&lt;/a&gt;()).&lt;br /&gt;
&lt;br /&gt;
2. If you have an &lt;i&gt;options&lt;/i&gt; menu on your activity (the one that pops up from the bottom of the screen when you press the hardware/softshell MENU button, you may be&amp;nbsp;interrupting&amp;nbsp;the event flow when you override &lt;a href="http://developer.android.com/reference/android/app/Activity.html#onMenuItemSelected(int, android.view.MenuItem)"&gt;onMenuItemSelected&lt;/a&gt; method. Overriding the &lt;a href="http://developer.android.com/reference/android/app/Activity.html#onMenuItemSelected(int, android.view.MenuItem)"&gt;onMenuItemSelected&lt;/a&gt; was taught in the &lt;a href="http://developer.android.com/resources/tutorials/notepad/notepad-ex2.html"&gt;Notepad v2 tutorial&lt;/a&gt; source code. That was partially &lt;i&gt;wrong&lt;/i&gt;.&amp;nbsp;You &lt;i&gt;should not &lt;/i&gt;override it, instead you should override &lt;a href="http://developer.android.com/reference/android/app/Activity.html#onOptionsItemSelected(android.view.MenuItem)"&gt;onOptionsItemSelected&lt;/a&gt;, and return false when the menu item is not handled. Example:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: java"&gt;@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.menuImpor) {
        // do your work...
        return true;
    } else if (item.getItemId() == R.id.menuEkspor) {

        // do your work...
        return true;
    }
    return false; // no need to call super.onOptionsItemSelected(item)
}&lt;/pre&gt;&lt;br /&gt;
&lt;div class="p3"&gt;That way, your &lt;a href="http://developer.android.com/reference/android/app/Activity.html#onContextItemSelected(android.view.MenuItem)"&gt;onContextItemSelected&lt;/a&gt; should now be called correctly.&lt;br /&gt;
&lt;br /&gt;
In a nutshell, "Menu" is the generic term for both "Context" and "Options" menu. Make sure you pay attention to the naming of the methods.&lt;/div&gt;&lt;div class="p4"&gt;&lt;span class="Apple-tab-span"&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1425998245372764769-7487659730226574912?l=biginteger.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/QiWAL0SMNEznpznkCVG-EcJTh7g/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/QiWAL0SMNEznpznkCVG-EcJTh7g/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/QiWAL0SMNEznpznkCVG-EcJTh7g/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/QiWAL0SMNEznpznkCVG-EcJTh7g/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Play-playWithCode/~4/VmjUwWeq7Ig" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://biginteger.blogspot.com/feeds/7487659730226574912/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://biginteger.blogspot.com/2010/09/solution-to-oncontextitemselected-not.html#comment-form" title="5 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/7487659730226574912?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/7487659730226574912?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Play-playWithCode/~3/VmjUwWeq7Ig/solution-to-oncontextitemselected-not.html" title="Solution to: onContextItemSelected not called after selecting a context menu" /><author><name>Yuku Sugianto</name><uri>https://profiles.google.com/117908749193966045672</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-xhNxoKzRFg8/AAAAAAAAAAI/AAAAAAAAybo/lCNLgVyCmog/s512-c/photo.jpg" /></author><thr:total>5</thr:total><feedburner:origLink>http://biginteger.blogspot.com/2010/09/solution-to-oncontextitemselected-not.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkYMSHk5fyp7ImA9WxFXF0Q.&quot;"><id>tag:blogger.com,1999:blog-1425998245372764769.post-4631022713628490439</id><published>2010-05-25T22:36:00.001+08:00</published><updated>2010-05-25T22:36:29.727+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-05-25T22:36:29.727+08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="enumeration" /><category scheme="http://www.blogger.com/atom/ns#" term="iphone" /><category scheme="http://www.blogger.com/atom/ns#" term="objective-c" /><title>Setting value (object) while enumerating an NSMutableDictionary</title><content type="html">Let's say you have an NSMutableDictionary instance which maps strings to numbers. For example, a list of stage names and the number of times the stage has been completed:&lt;br /&gt;
&lt;br /&gt;
"level1" =&amp;gt; 10&lt;br /&gt;
"level2" =&amp;gt; 2&lt;br /&gt;
"final" =&amp;gt; 1&lt;br /&gt;
&lt;br /&gt;
I wanted to create a "clear data" function, so I enumerate the dictionary using the new foreach loop and set the value to 0 without adding or removing the key:&lt;br /&gt;
&lt;br /&gt;
for (NSString *key in dictionary) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;[dictionary setObject:[NSNumber numberWithInt:0] forKey:key];&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
This compiles fine, but a runtime error will occur:&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Collection &lt;nscfdictionary: 0x393b2f0=""&gt; was mutated while being enumerated.&lt;/nscfdictionary:&gt;&lt;/b&gt;&lt;br /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;Unfortunately there is no API to modify the value even without adding or removing the key.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;But, there is a solution: &lt;i&gt;enumerate the keys instead of the dictionary&lt;/i&gt;. This works:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;for (NSString *key in [dictionary allKeys]) {&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;[dictionary setObject:[NSNumber numberWithInt:0] forKey:key];&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;}&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1425998245372764769-4631022713628490439?l=biginteger.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/wWqWENyj7zTAjwbBNoMFXlmqgfw/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/wWqWENyj7zTAjwbBNoMFXlmqgfw/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/wWqWENyj7zTAjwbBNoMFXlmqgfw/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/wWqWENyj7zTAjwbBNoMFXlmqgfw/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Play-playWithCode/~4/vEQTTUL-c3Y" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://biginteger.blogspot.com/feeds/4631022713628490439/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://biginteger.blogspot.com/2010/05/setting-value-object-while-enumerating.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/4631022713628490439?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/4631022713628490439?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Play-playWithCode/~3/vEQTTUL-c3Y/setting-value-object-while-enumerating.html" title="Setting value (object) while enumerating an NSMutableDictionary" /><author><name>Yuku Sugianto</name><uri>https://profiles.google.com/117908749193966045672</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-xhNxoKzRFg8/AAAAAAAAAAI/AAAAAAAAybo/lCNLgVyCmog/s512-c/photo.jpg" /></author><thr:total>1</thr:total><feedburner:origLink>http://biginteger.blogspot.com/2010/05/setting-value-object-while-enumerating.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkQCQX0ycSp7ImA9WxFTGE0.&quot;"><id>tag:blogger.com,1999:blog-1425998245372764769.post-6069155151161880265</id><published>2010-04-09T15:53:00.001+08:00</published><updated>2010-04-09T16:06:00.399+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-04-09T16:06:00.399+08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="TextView" /><category scheme="http://www.blogger.com/atom/ns#" term="ScrollView" /><category scheme="http://www.blogger.com/atom/ns#" term="android" /><title>Preventing TextView with links inside a ScrollView from dimming</title><content type="html">I wrote an Android application that shows a scrollable TextView because I put it inside a ScrollView. When I added links to the TextView, I had to execute this so that the links can be clicked with proper highlighting:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: java"&gt;textView.setLinksClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());&lt;/pre&gt;&lt;br /&gt;
It works:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/_ODdyLCCXPpQ/S77bEAWFjdI/AAAAAAAAp2I/-tkglGmSxTY/s1600/device.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/_ODdyLCCXPpQ/S77bEAWFjdI/AAAAAAAAp2I/-tkglGmSxTY/s320/device.png" wt="true" /&gt;&lt;/a&gt;&lt;/div&gt;However, when I use my finger to scroll it, the text other than links are dimmed, very, very dark until we can almost see nothing.&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_ODdyLCCXPpQ/S77bf93McXI/AAAAAAAAp2Q/Il8oBKdTAyE/s1600/untitled.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/_ODdyLCCXPpQ/S77bf93McXI/AAAAAAAAp2Q/Il8oBKdTAyE/s320/untitled.PNG" wt="true" /&gt;&lt;/a&gt;&lt;/div&gt;Calling &lt;strong&gt;setClickable(false)&lt;/strong&gt; and &lt;strong&gt;setLongClickable(false)&lt;/strong&gt; fixed the issue, but the link itself is not highlighted anymore when "hovered", and the user may think that the link is not clickable.&lt;br /&gt;
&lt;br /&gt;
I found a solution, which is not perfect, but works. Just set the color of the TextView. The links will stay on the same color, but the normal text will change color, and it does not dim anymore!&lt;br /&gt;
&lt;br /&gt;
Here is what I get using &lt;span style="font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace; font-size: x-small;"&gt;#fff&lt;/span&gt; (a.k.a. &lt;span style="font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace; font-size: x-small;"&gt;0xffffffff&lt;/span&gt;) color.&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/_ODdyLCCXPpQ/S77cqYjRq7I/AAAAAAAAp2Y/X8uj0WZnYEs/s1600/1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/_ODdyLCCXPpQ/S77cqYjRq7I/AAAAAAAAp2Y/X8uj0WZnYEs/s320/1.png" wt="true" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1425998245372764769-6069155151161880265?l=biginteger.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/Ja5Qdp4qWUo56d6IWjPw7R0EmKg/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Ja5Qdp4qWUo56d6IWjPw7R0EmKg/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/Ja5Qdp4qWUo56d6IWjPw7R0EmKg/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Ja5Qdp4qWUo56d6IWjPw7R0EmKg/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Play-playWithCode/~4/0EArEB6dc7A" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://biginteger.blogspot.com/feeds/6069155151161880265/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://biginteger.blogspot.com/2010/04/preventing-textview-with-links-inside.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/6069155151161880265?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/6069155151161880265?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Play-playWithCode/~3/0EArEB6dc7A/preventing-textview-with-links-inside.html" title="Preventing TextView with links inside a ScrollView from dimming" /><author><name>Yuku Sugianto</name><uri>https://profiles.google.com/117908749193966045672</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-xhNxoKzRFg8/AAAAAAAAAAI/AAAAAAAAybo/lCNLgVyCmog/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/_ODdyLCCXPpQ/S77bEAWFjdI/AAAAAAAAp2I/-tkglGmSxTY/s72-c/device.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://biginteger.blogspot.com/2010/04/preventing-textview-with-links-inside.html</feedburner:origLink></entry><entry gd:etag="W/&quot;Ak8FSXc4fCp7ImA9WxFTF0Q.&quot;"><id>tag:blogger.com,1999:blog-1425998245372764769.post-8668086525751212191</id><published>2010-04-06T18:58:00.003+08:00</published><updated>2010-04-09T15:40:18.934+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-04-09T15:40:18.934+08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="performance" /><category scheme="http://www.blogger.com/atom/ns#" term="java" /><category scheme="http://www.blogger.com/atom/ns#" term="formatting" /><category scheme="http://www.blogger.com/atom/ns#" term="string" /><category scheme="http://www.blogger.com/atom/ns#" term="android" /><title>String.format in Android is extremely slow!</title><content type="html">I'm used to using &lt;a href="http://developer.android.com/reference/java/lang/String.html#format(java.lang.String, java.lang.Object...)"&gt;String.format&lt;/a&gt; to construct messages. Even when I don't need to specify width or number of decimal places. It just looks neater to the eyes.&lt;br /&gt;
&lt;br /&gt;
For example, on the Alkitab (Bible) application, I wrote this for debugging:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: java"&gt;Log.d("alki", String.format("tebakKitab fase 3: dengan %s:%d skor %d", ref.pendek, ref.pos, skor));&lt;/pre&gt;&lt;br /&gt;
Each of "my operation", involving about 70 calls to the above line, takes about 300 ms. I thought that was acceptable.&lt;br /&gt;
&lt;br /&gt;
But when I tried to build a game engine, I found a bottleneck somewhere that drags the fps. I found it to be the String.format call, which takes about 6ms for just one call! That's ridiculously slow.&lt;br /&gt;
&lt;br /&gt;
Guess what: when I change the above line to:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: java"&gt;Log.d("alki", "tebakKitab fase 3: dengan " + ref.pendek + ":" + ref.pos + " skor " + skor);&lt;/pre&gt;&lt;br /&gt;
"my operation" takes only 22 ms!&lt;br /&gt;
&lt;br /&gt;
Okay, once again: &lt;b&gt;Don't use String.format in Android applications!&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Note:&amp;nbsp;When I traced method calls to know what makes it slow, String.format apparently calls a very deep code, something with DecimalFormatter, even CurrencyFormatter even though I didn't use it. It also calls &lt;a href="http://www.ibm.com/software/globalization/icu"&gt;com.ibm.icu.**&lt;/a&gt; packages. It really has a huge logic inside.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1425998245372764769-8668086525751212191?l=biginteger.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/PrTZNCF143Oh6Qc-l0fCcwxewqE/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/PrTZNCF143Oh6Qc-l0fCcwxewqE/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/PrTZNCF143Oh6Qc-l0fCcwxewqE/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/PrTZNCF143Oh6Qc-l0fCcwxewqE/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Play-playWithCode/~4/SEH7RDKZPk4" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://biginteger.blogspot.com/feeds/8668086525751212191/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://biginteger.blogspot.com/2010/04/stringformat-in-android-is-extremely.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/8668086525751212191?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/8668086525751212191?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Play-playWithCode/~3/SEH7RDKZPk4/stringformat-in-android-is-extremely.html" title="String.format in Android is extremely slow!" /><author><name>Yuku Sugianto</name><uri>https://profiles.google.com/117908749193966045672</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-xhNxoKzRFg8/AAAAAAAAAAI/AAAAAAAAybo/lCNLgVyCmog/s512-c/photo.jpg" /></author><thr:total>1</thr:total><feedburner:origLink>http://biginteger.blogspot.com/2010/04/stringformat-in-android-is-extremely.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkIDR3w5cCp7ImA9WxBSEEk.&quot;"><id>tag:blogger.com,1999:blog-1425998245372764769.post-9008097152156438478</id><published>2009-12-17T17:00:00.001+08:00</published><updated>2009-12-17T17:02:56.228+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-12-17T17:02:56.228+08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="java" /><category scheme="http://www.blogger.com/atom/ns#" term="encoding" /><category scheme="http://www.blogger.com/atom/ns#" term="android" /><title>Faster reading UTF-8 encoded file in Android</title><content type="html">I created an Android application which reads some text files from a raw resource. The text files are encoded in UTF8. Therefore, I straight away wrote the code to convert bytes from the file into characters.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: java"&gt;InputStreamReader in = new InputStreamReader(new BufferedInputStream(resources.openRawResource(R.raw.textfile)))
int c = in.read(); // read a character, and so on.&lt;/pre&gt;&lt;br /&gt;
But, reading a 10KB file takes almost a minute on the Android 1.5 emulator! I wondered what made that so slow, in my Nokia phone, the same program written in Java ME takes less than a second to do the same thing.&lt;br /&gt;
&lt;br /&gt;
By using &lt;a href="http://developer.android.com/guide/developing/tools/traceview.html"&gt;Traceview&lt;/a&gt;, I found out that most of the time is spent on the UTF-8 decoding from bytes to characters. Android's Java implementation uses &lt;a href="http://www-01.ibm.com/software/globalization/icu/index.jsp"&gt;IBM ICU&lt;/a&gt; for character encoding. And it seems to be overkill to just decode UTF-8. Hence, the solution is to create own implementation if UTF-8 decoder. (Some concept taken from &lt;a href="http://golang.org/src/pkg/utf8/utf8.go"&gt;Go source&lt;/a&gt;, less the error-checking overhead and only look for max 16-bit characters.)&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: java"&gt;public class Utf8Reader implements Closeable {
    private InputStream in_;
    public static final char replacementChar = 0xFFFD;

    public Utf8Reader(InputStream in) {
        in_ = in;
    }

    public int read() throws IOException {
        int c0 = in_.read();

        if (c0 == -1) {
            // EOF
            return -1;
        }

        if (c0 &amp;lt; 0x80) {
            // input 1 byte, output 7 bit
            return c0;
        }

        int c1 = in_.read();

        if (c1 == -1) {
            // partial EOF
            return -1;
        }

        if (c0 &amp;lt; 0xe0) {
            // input 2 byte, output 5+6 = 11 bit
            return ((c0 &amp;amp; 0x1f) &amp;lt;&amp;lt; 6) | (c1 &amp;amp; 0x3f);
        }

        int c2 = in_.read();

        if (c2 == -1) {
            // partial EOF
            return -1;
        }

        // input 3 byte, output 4+6+6 = 16 bit
        return ((c0 &amp;amp; 0x0f) &amp;lt;&amp;lt; 12) | ((c1 &amp;amp; 0x3f) &amp;lt;&amp;lt; 6) | (c2 &amp;amp; 0x3f);
    }

    @Override
    public void close() throws IOException {
        in_.close();
    }
}&lt;/pre&gt;&lt;br /&gt;
(Please add the required import by yourself.) The result is satisfying: the 10KB file is now loaded in about 1 second in the emulator, and almost instantly on the device.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1425998245372764769-9008097152156438478?l=biginteger.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/on2paWZrSWJt2sG5MqVg2RwBK0k/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/on2paWZrSWJt2sG5MqVg2RwBK0k/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/on2paWZrSWJt2sG5MqVg2RwBK0k/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/on2paWZrSWJt2sG5MqVg2RwBK0k/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Play-playWithCode/~4/4aD1d5Ll6AY" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://biginteger.blogspot.com/feeds/9008097152156438478/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://biginteger.blogspot.com/2009/12/faster-reading-utf-8-encoded-file-in.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/9008097152156438478?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/9008097152156438478?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Play-playWithCode/~3/4aD1d5Ll6AY/faster-reading-utf-8-encoded-file-in.html" title="Faster reading UTF-8 encoded file in Android" /><author><name>Yuku Sugianto</name><uri>https://profiles.google.com/117908749193966045672</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-xhNxoKzRFg8/AAAAAAAAAAI/AAAAAAAAybo/lCNLgVyCmog/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://biginteger.blogspot.com/2009/12/faster-reading-utf-8-encoded-file-in.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUAMRno_eip7ImA9WxNUF0s.&quot;"><id>tag:blogger.com,1999:blog-1425998245372764769.post-7758762018663954278</id><published>2009-11-09T18:49:00.000+08:00</published><updated>2009-11-09T18:49:47.442+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-11-09T18:49:47.442+08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="encoding" /><category scheme="http://www.blogger.com/atom/ns#" term="python" /><category scheme="http://www.blogger.com/atom/ns#" term="php" /><title>Base64 in PHP and Python</title><content type="html">Today I calculated a hash value based on strings encoded with Base64 encoding.&lt;br /&gt;
&lt;br /&gt;
One in PHP, and one in Python. Both of them should return the same value, because the hashes were compared for verification.&lt;br /&gt;
&lt;br /&gt;
So, in PHP, the code is&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: php"&gt;myHashFunction(base64_encode('original string')) &lt;/pre&gt;&lt;br /&gt;
And in Python, the code is&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: python"&gt;myHashFunction(base64.encodestring('original string'))&lt;/pre&gt;&lt;br /&gt;
Dangerous! The results are different! Since the 'original string' was not as simple as that, I thought I had passed the wrong data. But after some checking, the results of base64_encode and base64.encodestring were different.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;base64_encode('original string')&lt;/b&gt; returns &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;"b3JpZ2luYWwgc3RyaW5n"&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
whereas &lt;b&gt;base64.encodestring('original string')&lt;/b&gt; returns &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;"b3JpZ2luYWwgc3RyaW5n\n"&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
More precisely, &lt;b&gt;base64.encodestring &lt;/b&gt;added new-line character at the end (and every 76 chars I think), suitable for email attachment, whereas &lt;b&gt;base64_encode &lt;/b&gt;does not.&lt;br /&gt;
&lt;br /&gt;
An easy solution to make them identical is to add replace function to the Python version, to become: &lt;b style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;base64.encodestring('original string').replace('\n', '')&lt;/b&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1425998245372764769-7758762018663954278?l=biginteger.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/fuzi2CCld4OON1Kg7Wtm2UdGDS4/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/fuzi2CCld4OON1Kg7Wtm2UdGDS4/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/fuzi2CCld4OON1Kg7Wtm2UdGDS4/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/fuzi2CCld4OON1Kg7Wtm2UdGDS4/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Play-playWithCode/~4/o7DPGU4A4Y4" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://biginteger.blogspot.com/feeds/7758762018663954278/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://biginteger.blogspot.com/2009/11/base64-in-php-and-python.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/7758762018663954278?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/7758762018663954278?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Play-playWithCode/~3/o7DPGU4A4Y4/base64-in-php-and-python.html" title="Base64 in PHP and Python" /><author><name>Yuku Sugianto</name><uri>https://profiles.google.com/117908749193966045672</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-xhNxoKzRFg8/AAAAAAAAAAI/AAAAAAAAybo/lCNLgVyCmog/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://biginteger.blogspot.com/2009/11/base64-in-php-and-python.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A08MRHg9cCp7ImA9WxNWE04.&quot;"><id>tag:blogger.com,1999:blog-1425998245372764769.post-8229760343759161328</id><published>2009-10-12T17:31:00.000+08:00</published><updated>2009-10-12T17:31:25.668+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-12T17:31:25.668+08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="cmd shell" /><title>find -exec equivalent for Windows cmd</title><content type="html">I was looking for replacement of the shell (bash) command:&lt;br /&gt;
&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;span style="font-size: x-small;"&gt;find -name '.svn' -exec rm -rf {} \;&lt;/span&gt;&lt;br /&gt;
&lt;/div&gt;&lt;br /&gt;
for Windows cmd.exe.&lt;br /&gt;
&lt;br /&gt;
The purpose is to remove all .svn directories from a directory recursively.&lt;br /&gt;
&lt;br /&gt;
In cmd you can do dir /b /s to list directories in plain format including its subdirectories. For example:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;span style="font-size: x-small;"&gt;C:\WINDOWS\system32\config&amp;gt;&lt;b&gt;dir /s /b&lt;/b&gt;&lt;br /&gt;
C:\WINDOWS\system32\config\AppEvent.Evt&lt;br /&gt;
C:\WINDOWS\system32\config\system.sav&lt;br /&gt;
C:\WINDOWS\system32\config\systemprofile&lt;br /&gt;
C:\WINDOWS\system32\config\userdiff&lt;br /&gt;
C:\WINDOWS\system32\config\systemprofile\Desktop&lt;br /&gt;
C:\WINDOWS\system32\config\systemprofile\Favorites&lt;br /&gt;
C:\WINDOWS\system32\config\systemprofile\My Documents&lt;br /&gt;
C:\WINDOWS\system32\config\systemprofile\Start Menu&lt;/span&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;span style="font-size: x-small;"&gt;...&lt;/span&gt;&lt;br /&gt;
&lt;/div&gt;&lt;br /&gt;
That format is already similar to what find command does. So how to execute a program with arguments taken from this list?&lt;br /&gt;
&lt;br /&gt;
The answer is, use the FOR command with /F "usebackq" switch.&lt;br /&gt;
&lt;br /&gt;
So, we put the command in backquotes, like this:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;b&gt;for /F "usebackq" %i in (`dir /s /b *.svn`) do rmdir /s /q %i&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;/div&gt;&lt;br /&gt;
Problem solved. Remember to double the percent sign if you do this in a batch file.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1425998245372764769-8229760343759161328?l=biginteger.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/1U2aK0nua0YeGuUr94ffahiftyc/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/1U2aK0nua0YeGuUr94ffahiftyc/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/1U2aK0nua0YeGuUr94ffahiftyc/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/1U2aK0nua0YeGuUr94ffahiftyc/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Play-playWithCode/~4/FTdLGETjbTw" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://biginteger.blogspot.com/feeds/8229760343759161328/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://biginteger.blogspot.com/2009/10/find-exec-equivalent-for-windows-cmd.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/8229760343759161328?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/8229760343759161328?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Play-playWithCode/~3/FTdLGETjbTw/find-exec-equivalent-for-windows-cmd.html" title="find -exec equivalent for Windows cmd" /><author><name>Yuku Sugianto</name><uri>https://profiles.google.com/117908749193966045672</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-xhNxoKzRFg8/AAAAAAAAAAI/AAAAAAAAybo/lCNLgVyCmog/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://biginteger.blogspot.com/2009/10/find-exec-equivalent-for-windows-cmd.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkYFQH45fCp7ImA9WxNQGEs.&quot;"><id>tag:blogger.com,1999:blog-1425998245372764769.post-821531332244143537</id><published>2009-09-25T15:35:00.000+08:00</published><updated>2009-09-25T15:35:11.024+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-09-25T15:35:11.024+08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="windows" /><title>How to copy error message or anything from message box</title><content type="html">If you get an error message, warning, status information in a message dialog like this:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/_ODdyLCCXPpQ/SrxyhSUhxTI/AAAAAAAAZLE/vflh6Y1IStQ/s1600-h/bbb210.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/_ODdyLCCXPpQ/SrxyhSUhxTI/AAAAAAAAZLE/vflh6Y1IStQ/s400/bbb210.png" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;/div&gt;&lt;br /&gt;
How can you copy the error message and inform more expert people?&lt;br /&gt;
&lt;br /&gt;
The answer is simple: Press Ctrl + C when the message box appears. Although you can't select the text with your mouse, just press Ctrl + C and you will get the title, message, and buttons copied to your clipboard.&lt;br /&gt;
&lt;br /&gt;
Sample output:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;---------------------------&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;notped&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;---------------------------&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;Windows cannot find 'notped'. Make sure you typed the name correctly, and then try again. To search for a file, click the Start button, and then click Search.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;---------------------------&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;OK &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;---------------------------&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;I have been using Windows XP since 2003 and this I just knew a month ago! What a shame.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1425998245372764769-821531332244143537?l=biginteger.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/nqAqBVIPIz4REuFOwmQP7qLnQ_w/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/nqAqBVIPIz4REuFOwmQP7qLnQ_w/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/nqAqBVIPIz4REuFOwmQP7qLnQ_w/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/nqAqBVIPIz4REuFOwmQP7qLnQ_w/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Play-playWithCode/~4/oc45o70htAA" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://biginteger.blogspot.com/feeds/821531332244143537/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://biginteger.blogspot.com/2009/09/how-to-copy-error-message-or-anything.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/821531332244143537?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/821531332244143537?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Play-playWithCode/~3/oc45o70htAA/how-to-copy-error-message-or-anything.html" title="How to copy error message or anything from message box" /><author><name>Yuku Sugianto</name><uri>https://profiles.google.com/117908749193966045672</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-xhNxoKzRFg8/AAAAAAAAAAI/AAAAAAAAybo/lCNLgVyCmog/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/_ODdyLCCXPpQ/SrxyhSUhxTI/AAAAAAAAZLE/vflh6Y1IStQ/s72-c/bbb210.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://biginteger.blogspot.com/2009/09/how-to-copy-error-message-or-anything.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkUAQHk4eip7ImA9WxNQEE0.&quot;"><id>tag:blogger.com,1999:blog-1425998245372764769.post-7066579267962102271</id><published>2009-09-15T15:35:00.002+08:00</published><updated>2009-09-15T15:37:21.732+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-09-15T15:37:21.732+08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="php" /><category scheme="http://www.blogger.com/atom/ns#" term="web" /><category scheme="http://www.blogger.com/atom/ns#" term="html" /><title>Extract required parameters to variables with a single line of code! (PHP)</title><content type="html">[&lt;i&gt;Note: this is for those not using sophisticated PHP frameworks (ZF, CakePHP, etc.), but using plain old PHP (Smarty is still OK and great!)&lt;/i&gt;]&lt;br /&gt;
&lt;br /&gt;
How many times have you wanted to extract selected GET or POST or COOKIE variables into local/global variables to be used easily? Something like:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: php"&gt;$submit = (int) $_REQUEST['submit'];
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$preference = (int) $_REQUEST['preference'];

if ($submit) {
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (!$name and !$email) {
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; $error = 'Please fill in your name and email';
&amp;nbsp;&amp;nbsp;&amp;nbsp; } else {
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; sql("insert into POST values (?, ?, ?)", $name, $email, $preference);
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
}&lt;/pre&gt;&lt;br /&gt;
And then at another page, you need to do the same thing over and over again (and you don't actually care whether it's GET or POST or...)&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: php"&gt;$start = (int) $_GET['start'];
$length = (int) $_GET['length'];
$locale = $_GET['locale'];&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;Solution:&lt;/b&gt; I have always been using this function to extract request variables to the global scope:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: php"&gt;// first example
getVars('submit name email preference');
echo "Your name is $name"; // the variable $name is now available
// second example
getVars('start length locale');&lt;/pre&gt;&lt;br /&gt;
Isn't it convenient?&lt;br /&gt;
&lt;br /&gt;
How does &lt;b&gt;getVars()&lt;/b&gt; function look like?&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: php"&gt;function getVars($vars) {
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (is_string($vars)) {
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; $vars = split(' ', $vars);
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; foreach ($vars as $var) {
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; $GLOBALS[$var] = $_REQUEST[$var];
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
}&lt;/pre&gt;&lt;br /&gt;
The trick is to move the variables from &lt;b&gt;$_REQUEST&lt;/b&gt; to &lt;b&gt;$GLOBALS&lt;/b&gt;. &lt;br /&gt;
Now let's enhance it so that it converts the variables to int when needed:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: php"&gt;function getVars($vars) {
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (is_string($vars)) {
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; $vars = split(' ', $vars);
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; foreach ($vars as $var) {
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; $modifier = '';
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if (strpos($var, '/') !== false) {
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; $tp = split('/', $var);
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; $var = $tp[0];
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; $modifier = $tp[1];
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; $value = $_REQUEST[$var];
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if ($modifier == 'hex') {
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; $value = pack("H*", $value);
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; } elseif ($modifier == 'int') {
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; $value = (int)$value;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; $GLOBALS[$var] = $value;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
}&lt;/pre&gt;&lt;br /&gt;
We go back to the examples, we now have:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: php"&gt;// first example
getVars('submit/int name email preference/int');
// second example
getVars('start/int length/int locale');&lt;/pre&gt;&lt;br /&gt;
Such a useful function, &lt;b&gt;Let us use it!&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Tip: you can also write '/hex' to convert "414243" to "ABC"&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1425998245372764769-7066579267962102271?l=biginteger.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/3hz7rtZAeJjrGyu67r4b7N9BxLE/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/3hz7rtZAeJjrGyu67r4b7N9BxLE/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/3hz7rtZAeJjrGyu67r4b7N9BxLE/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/3hz7rtZAeJjrGyu67r4b7N9BxLE/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Play-playWithCode/~4/Zg_3gy4ZjMs" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://biginteger.blogspot.com/feeds/7066579267962102271/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://biginteger.blogspot.com/2009/09/extract-required-parameters-to.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/7066579267962102271?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/7066579267962102271?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Play-playWithCode/~3/Zg_3gy4ZjMs/extract-required-parameters-to.html" title="Extract required parameters to variables with a single line of code! (PHP)" /><author><name>Yuku Sugianto</name><uri>https://profiles.google.com/117908749193966045672</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-xhNxoKzRFg8/AAAAAAAAAAI/AAAAAAAAybo/lCNLgVyCmog/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://biginteger.blogspot.com/2009/09/extract-required-parameters-to.html</feedburner:origLink></entry><entry gd:etag="W/&quot;Dk8CR389eSp7ImA9WxNTGEk.&quot;"><id>tag:blogger.com,1999:blog-1425998245372764769.post-2757754463845382188</id><published>2009-08-21T16:41:00.005+08:00</published><updated>2009-08-21T16:54:26.161+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-08-21T16:54:26.161+08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="hibernate" /><category scheme="http://www.blogger.com/atom/ns#" term="database" /><category scheme="http://www.blogger.com/atom/ns#" term="annotation" /><title>Escaping database column name when using Hibernate</title><content type="html">&lt;div&gt;Changing the database engine when using Hibernate turned out to be not as easy as changing the &lt;b&gt;.hbm.xml&lt;/b&gt; file. &lt;/div&gt;&lt;div&gt;
&lt;/div&gt;&lt;div&gt;At first I used Apache Derby (client-server) as the database engine, just for testing purposes. I tried to change it to Microsoft SQL Server, by using the JDBC driver provided and changing the &lt;b&gt;.hbm.xml&lt;/b&gt; file. A problem happened, a String field whose length I set to 40000, cannot be handled, so I need to truncate it to 8000. Later when I change it to Oracle, there cannot be 2 columns that have &lt;b&gt;LONG VARCHAR2&lt;/b&gt; type in a single table, so I need to further reduce it to 4000 characters.&lt;/div&gt;&lt;div&gt;
&lt;/div&gt;&lt;div&gt;The problem does not stop there. I use Hibernate Annotations, so every field is configured in the source code (instead of in the &lt;b&gt;.hbm.xml &lt;/b&gt;file). I can define the column names if I want:&lt;/div&gt;&lt;div&gt;
&lt;/div&gt;&lt;div&gt;&lt;pre class="brush: java"&gt;@Entity
public class Entry {
  String message;

  @Column(name = "log_time")
  Date time;
}&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;
&lt;/div&gt;&lt;div&gt;In that case, the &lt;b&gt;message &lt;/b&gt;field will be stored in a database column called &lt;b&gt;message&lt;/b&gt;, but the &lt;b&gt;time &lt;/b&gt;field will be stored in a database column called &lt;b&gt;log_time &lt;/b&gt;since I have defined a &lt;b&gt;@Column&lt;/b&gt; annotation. &lt;/div&gt;&lt;div&gt;
&lt;/div&gt;&lt;div&gt;The sad truth is that Hibernate does not escape column names mentioned in queries, so some DBMS'es regard them as keywords. Example include &lt;b&gt;exception&lt;/b&gt; in Oracle, &lt;b&gt;size&lt;/b&gt; in Derby, and so on. &lt;/div&gt;&lt;div&gt;
&lt;/div&gt;&lt;div&gt;We can force Hibernate to escape identifier names on the SQL queries by putting backticks (`) on the column name. The backticks will be converted to different identifier escaper for different database systems. For example, `column` in MySQL, [column] in Microsoft SQL, and "column" in Apache Derby.&lt;/div&gt;&lt;div&gt;
&lt;/div&gt;&lt;div&gt;So, our entity class becomes something like:&lt;/div&gt;&lt;div&gt;

&lt;pre class="brush: java"&gt;@Entity
public class LogEntry {
  @Column(name = "`level`")
  String level;

  @Column(name = "`exception`", length = 4000)
  String exception;

  // etc.
}&lt;/pre&gt;
&lt;/div&gt;&lt;div&gt;Unfortunately, we must duplicate the name (one on the field, and one on the annotations), so it's not so easy to maintain (e.g. we do a rename-refactoring to the field name, we may not notice that the annotation's column name is not changed together).&lt;/div&gt;&lt;div&gt;
&lt;/div&gt;&lt;div&gt;I still wonder why doesn't Hibernate &lt;i&gt;always &lt;/i&gt;escape the column name.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1425998245372764769-2757754463845382188?l=biginteger.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/PnQ5mm4foIdrJwTEwwykJ7QdO1A/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/PnQ5mm4foIdrJwTEwwykJ7QdO1A/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/PnQ5mm4foIdrJwTEwwykJ7QdO1A/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/PnQ5mm4foIdrJwTEwwykJ7QdO1A/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Play-playWithCode/~4/pJjVRiiCuYs" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://biginteger.blogspot.com/feeds/2757754463845382188/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://biginteger.blogspot.com/2009/08/changing-database-engine-when-using.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/2757754463845382188?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/2757754463845382188?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Play-playWithCode/~3/pJjVRiiCuYs/changing-database-engine-when-using.html" title="Escaping database column name when using Hibernate" /><author><name>Yuku Sugianto</name><uri>https://profiles.google.com/117908749193966045672</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-xhNxoKzRFg8/AAAAAAAAAAI/AAAAAAAAybo/lCNLgVyCmog/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://biginteger.blogspot.com/2009/08/changing-database-engine-when-using.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0EARHw7eip7ImA9WxJaGUo.&quot;"><id>tag:blogger.com,1999:blog-1425998245372764769.post-4545825268418904413</id><published>2009-08-11T15:41:00.004+08:00</published><updated>2009-08-11T16:34:05.202+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-08-11T16:34:05.202+08:00</app:edited><title>Excluding specific folder from directory crawlers</title><content type="html">&lt;div&gt;&lt;i&gt;Shortly: I want to have a directory D:\bekap\mybackup that cannot be traversed.&lt;/i&gt;&lt;/div&gt;&lt;div&gt;
&lt;/div&gt;&lt;div&gt;I made a backup program that backs up data from a specified directory to another directory. (The program has much more features such as detecting duplicate data and to have efficient incremental backup.) I wanted to backup the whole D: drive, but that is the only partition available for the backup destination. Therefore I can only backup D: to let's say D:\bekap\mybackup.&lt;/div&gt;&lt;div&gt;
&lt;/div&gt;&lt;div&gt;The problem is, when the program traverses or walks through D: to find available files, in the end it will also go to D:\bekap\mybackup and it would try to back up files inside D:\bekap\mybackup to itself. (It's a similar problem with &lt;i&gt;tar -cf archive.tar .&lt;/i&gt;).&lt;/div&gt;&lt;div&gt;
&lt;/div&gt;&lt;div&gt;So I want to make D:\bekap\mybackup not detectable except by directly accessing it through the path name. It is similar as if you have a non-linked web page at http://example.com/private_photos/xyz/; only people you give the address will be able to access it.&lt;/div&gt;&lt;div&gt;
&lt;/div&gt;&lt;div&gt;After trying through several options, I found a way to do that:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;Go to the Security properties of D:\bekap (in Explorer: right click, properties, you may need to disable the &lt;i&gt;Use simple file sharing&lt;/i&gt; in Folder Options)

&lt;br&gt;&lt;img src="http://img405.imageshack.us/img405/4884/20824175.png" border="0" alt="" style="display: block; margin-top: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto; text-align: center;" /&gt;&lt;/li&gt;

&lt;li&gt;Click Advanced button, then you will see entries similar to these:

&lt;br&gt;&lt;img src="http://img33.imageshack.us/img33/3804/74374806.png" border="0" alt="" style="display: block; margin-top: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto; text-align: center;" /&gt;&lt;/li&gt;

&lt;li&gt;Click Add, fill in your username, then click Check Names, then OK.&lt;/li&gt;&lt;li&gt;Tick the &lt;b&gt;Deny&lt;/b&gt; column for the entry &lt;b&gt;List Folder / Read Data&lt;/b&gt;.

&lt;br&gt;&lt;img src="http://img38.imageshack.us/img38/2737/96748594.png" border="0" alt="" style="display: block; margin-top: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto; text-align: center;" /&gt;&lt;/li&gt;

&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;Now you cannot access D:\bekap, but if you type D:\bekap\mybackup, you can open the contents!&lt;/div&gt;&lt;/div&gt;
&lt;pre class="brush: plain"&gt;Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

D:\&gt;cd bekap

D:\bekap&gt;dir
Volume in drive D is Dadu
Volume Serial Number is 1293-1777

Directory of D:\bekap

File Not Found

D:\bekap&gt;cd mybackup

D:\bekap\mybackup&gt;dir
Volume in drive D is Dadu
Volume Serial Number is 1293-1777

Directory of D:\bekap\mybackup

2009-08-11  15:22    &amp;lt;DIR&gt;          .
2009-08-11  15:22    &amp;lt;DIR&gt;          ..
2009-08-11  15:22                 0 .bekapkeren2
2009-08-11  15:22        35,616,973 .cache.ser
2009-08-07  17:43    &amp;lt;DIR&gt;          data
2009-08-11  15:21    &amp;lt;DIR&gt;          fs
          2 File(s)     35,616,973 bytes
          4 Dir(s)  82,628,956,160 bytes free&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1425998245372764769-4545825268418904413?l=biginteger.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/L8Xzfh-_BwP9kn2CqvjdtC3IGms/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/L8Xzfh-_BwP9kn2CqvjdtC3IGms/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/L8Xzfh-_BwP9kn2CqvjdtC3IGms/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/L8Xzfh-_BwP9kn2CqvjdtC3IGms/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Play-playWithCode/~4/ys2c6ajexp8" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://biginteger.blogspot.com/feeds/4545825268418904413/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://biginteger.blogspot.com/2009/08/excluding-specific-folder-from.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/4545825268418904413?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/4545825268418904413?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Play-playWithCode/~3/ys2c6ajexp8/excluding-specific-folder-from.html" title="Excluding specific folder from directory crawlers" /><author><name>Yuku Sugianto</name><uri>https://profiles.google.com/117908749193966045672</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-xhNxoKzRFg8/AAAAAAAAAAI/AAAAAAAAybo/lCNLgVyCmog/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://biginteger.blogspot.com/2009/08/excluding-specific-folder-from.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkMNQXYzcSp7ImA9WxJaFUk.&quot;"><id>tag:blogger.com,1999:blog-1425998245372764769.post-2828852782834560828</id><published>2009-08-06T15:15:00.007+08:00</published><updated>2009-08-06T15:41:30.889+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-08-06T15:41:30.889+08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="java" /><title>Multiple Return Values in Java</title><content type="html">&lt;p&gt;How can we have multiple return values in Java? We cannot formally have it, since each method can only have no return value (void) or single return value (int, long, Object, String, etc.)

&lt;/p&gt;&lt;p&gt;One of the solution to emulate it is to create a class that contains two fields, one for each return value. But that will make you tired because you need to create it every time you need it (not including the time used to think what the class name should be).

&lt;/p&gt;&lt;p&gt;In my case, I solved this problem by using a &lt;span style="font-style: italic;"&gt;Pointer&lt;/span&gt; class. It works similar to C in which you can have multiple return values by passing a pointer to a value that will be modified to the function arguments like: &lt;span style="font-weight: bold;"&gt;size&lt;/span&gt; = fread(&lt;span style="font-weight: bold;"&gt;pbuf&lt;/span&gt;, size, count, pfile);

&lt;/p&gt;&lt;p&gt;The pointer class is as follows:

&lt;/p&gt;&lt;pre class="brush: java"&gt;public class Pointer&amp;lt;T&amp;gt; {
  public T value;
  public static &amp;lt;T&amp;gt; Pointer&amp;lt;T&amp;gt; create() {
    return new Pointer&amp;lt;T&amp;gt;();
  }
}&lt;/pre&gt;

&lt;p&gt;Let's say you have a method that needs to return two values: result and error code. The method for our example will be:

&lt;/p&gt;&lt;pre class="brush: java"&gt;byte[] downloadFile(String url, Pointer&amp;lt;Integer&amp;gt; errorCode) {
  ...(really download)...
  if (errorCode != null) {
    errorCode.value = 200; // example only
  }
}&lt;/pre&gt;

&lt;p&gt;To use the method, we first create the pointer to store the error code as follows:

&lt;/p&gt;&lt;pre class="brush: java"&gt;Pointer&amp;lt;Integer&amp;gt; errorCode = Pointer.create();
byte[] file = downloadFile("http://biginteger.blogspot.com/", errorCode);
System.out.printf("File downloaded (%d bytes) with error code %d", file.length, errorCode.value);&lt;/pre&gt;

&lt;p&gt;The reason of the create() is to eliminate repeated typing of the type parameter:

&lt;/p&gt;&lt;p&gt;Pointer&amp;lt;Integer&gt; errorCode = new Pointer&amp;lt;Integer&gt;();&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1425998245372764769-2828852782834560828?l=biginteger.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/_naGcYdrjy7JtPSG02EZNscXAxg/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/_naGcYdrjy7JtPSG02EZNscXAxg/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/_naGcYdrjy7JtPSG02EZNscXAxg/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/_naGcYdrjy7JtPSG02EZNscXAxg/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Play-playWithCode/~4/-ZluNh5i36I" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://biginteger.blogspot.com/feeds/2828852782834560828/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://biginteger.blogspot.com/2009/08/multiple-return-values-in-java.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/2828852782834560828?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/2828852782834560828?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Play-playWithCode/~3/-ZluNh5i36I/multiple-return-values-in-java.html" title="Multiple Return Values in Java" /><author><name>Yuku Sugianto</name><uri>https://profiles.google.com/117908749193966045672</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-xhNxoKzRFg8/AAAAAAAAAAI/AAAAAAAAybo/lCNLgVyCmog/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://biginteger.blogspot.com/2009/08/multiple-return-values-in-java.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEUFSH4_cSp7ImA9WxJaFUk.&quot;"><id>tag:blogger.com,1999:blog-1425998245372764769.post-1371231521894317370</id><published>2009-08-05T17:36:00.005+08:00</published><updated>2009-08-06T15:03:39.049+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-08-06T15:03:39.049+08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="javascript" /><category scheme="http://www.blogger.com/atom/ns#" term="internationalization" /><category scheme="http://www.blogger.com/atom/ns#" term="cookie" /><category scheme="http://www.blogger.com/atom/ns#" term="google web toolkit" /><category scheme="http://www.blogger.com/atom/ns#" term="web" /><category scheme="http://www.blogger.com/atom/ns#" term="html" /><title>Setting GWT locale using cookies</title><content type="html">&lt;div&gt;The &lt;a href="http://code.google.com/webtoolkit/doc/1.6/DevGuideI18nAndA11y.html#DevGuideSpecifyingLocale"&gt;Google Web Toolkit Developer Guide&lt;/a&gt; says, &lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style="line-height: 16px;font-family:Helvetica,Arial,sans-serif;font-size:small;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style="line-height: 16px;font-family:Helvetica,Arial,sans-serif;font-size:small;"&gt;GWT represents &lt;tt&gt;locale&lt;/tt&gt; as a client property whose value can be set either &lt;b&gt;using a meta tag embedded in the &lt;/b&gt;&lt;a href="http://code.google.com/webtoolkit/doc/1.6/DevGuideOrganizingProjects.html#DevGuideHostPage" style="color: rgb(0, 0, 204);"&gt;&lt;b&gt;host page&lt;/b&gt;&lt;/a&gt; or in &lt;b&gt;the query string&lt;/b&gt; of the host page's URL. &lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;/blockquote&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:Helvetica,Arial,sans-serif;"&gt;&lt;span class="Apple-style-span"  style="line-height: 16px;font-size:small;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;I wanted to set the locale of my GWT application stored somewhere, so that I don't need to always append &lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;?locale=ja&lt;/span&gt;&lt;/span&gt; or &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&amp;amp;locale=ja&lt;/span&gt;&lt;/span&gt; to the URL for every page on the site. Putting the meta tag every time the page is requested is also not an option, since you still need to let the server know the preferences of the user (session management).&lt;div&gt;
&lt;/div&gt;&lt;div&gt;For example, if I have &lt;a href="http://www.ngambek.com/"&gt;http://www.ngambek.com/&lt;/a&gt; page in Indonesian, but I want to set it to Japanese, I need to set the address to &lt;a href="http://www.ngambek.com/?locale=ja"&gt;http://www.ngambek.com/?locale=ja&lt;/a&gt; to let the GWT system use the Japanese version of the page. Later when there is a link to &lt;a href="http://www.ngambek.com/12345678"&gt;http://www.ngambek.com/12345678&lt;/a&gt;, I would need to make it &lt;a href="http://www.ngambek.com/12345678?locale=ja"&gt;http://www.ngambek.com/12345678?locale=ja&lt;/a&gt;. What a burden.&lt;/div&gt;&lt;div&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span&gt;&lt;span&gt;I decided to use cookie to store the preferred locale, then let a javascript snippet run to set a meta tag dynamically. When the GWT module is loaded, the locale will be detected because the appropriate meta tag will have been written (in browser's memory).&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;
&lt;/div&gt;&lt;div&gt;&lt;span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;So, before the GWT module script is loaded, I put these lines onto the HTML file:&lt;/div&gt;&lt;div&gt;&lt;pre class="brush: js"&gt;var getCookie = function(c_name) {
  if (document.cookie.length &amp;gt; 0) {
    c_start = document.cookie.indexOf(c_name + "=");
    if (c_start != -1) {
      c_start = c_start + c_name.length+1;
      c_end = document.cookie.indexOf(";", c_start);
      if (c_end == -1) c_end = document.cookie.length;
      return unescape(document.cookie.substring(c_start,c_end));
    }
  }
  return "";
}

var locale = getCookie("locale");
if (! locale) {
  locale = "id";
}

document.write("&amp;lt;meta name='gwt:property' content='locale=" + locale + "' /&amp;gt;");&lt;/pre&gt;&lt;div&gt;Then, in order to set the locale, it's just a matter of setting the locale cookie and reloading the page:&lt;/div&gt;&lt;/div&gt;&lt;div&gt;
&lt;/div&gt;

&lt;pre class="brush: js"&gt;
function setLocale(locale) {
  document.cookie = "locale=" + escape(locale);
  document.location.href = document.location.href;
}
&lt;/pre&gt;
&lt;div&gt;&lt;div&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div&gt;By the way, the &lt;a href="http://code.google.com/webtoolkit/doc/1.6/DevGuideI18nAndA11y.html"&gt;internationalization feature of GWT&lt;/a&gt; is great! Try it if you haven't tried it before.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1425998245372764769-1371231521894317370?l=biginteger.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/Vpliq_vXx6JUzT_rDxvtS4am_ZQ/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Vpliq_vXx6JUzT_rDxvtS4am_ZQ/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/Vpliq_vXx6JUzT_rDxvtS4am_ZQ/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Vpliq_vXx6JUzT_rDxvtS4am_ZQ/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Play-playWithCode/~4/8pv0BMNEyl0" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://biginteger.blogspot.com/feeds/1371231521894317370/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://biginteger.blogspot.com/2009/08/setting-gwt-locale-using-cookies.html#comment-form" title="4 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/1371231521894317370?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1425998245372764769/posts/default/1371231521894317370?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Play-playWithCode/~3/8pv0BMNEyl0/setting-gwt-locale-using-cookies.html" title="Setting GWT locale using cookies" /><author><name>Yuku Sugianto</name><uri>https://profiles.google.com/117908749193966045672</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-xhNxoKzRFg8/AAAAAAAAAAI/AAAAAAAAybo/lCNLgVyCmog/s512-c/photo.jpg" /></author><thr:total>4</thr:total><feedburner:origLink>http://biginteger.blogspot.com/2009/08/setting-gwt-locale-using-cookies.html</feedburner:origLink></entry></feed>

