﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-Look into it ~-随笔分类-Tips, Tricks, Hints &amp; Code</title><link>http://www.blogjava.net/lukewange-hit1983/category/33793.html</link><description>present</description><language>zh-cn</language><lastBuildDate>Fri, 12 Sep 2008 17:13:14 GMT</lastBuildDate><pubDate>Fri, 12 Sep 2008 17:13:14 GMT</pubDate><ttl>60</ttl><item><title>Serializing an Image </title><link>http://www.blogjava.net/lukewange-hit1983/archive/2008/09/11/228407.html</link><dc:creator>LukeW</dc:creator><author>LukeW</author><pubDate>Thu, 11 Sep 2008 09:09:00 GMT</pubDate><guid>http://www.blogjava.net/lukewange-hit1983/archive/2008/09/11/228407.html</guid><wfw:comment>http://www.blogjava.net/lukewange-hit1983/comments/228407.html</wfw:comment><comments>http://www.blogjava.net/lukewange-hit1983/archive/2008/09/11/228407.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lukewange-hit1983/comments/commentRss/228407.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lukewange-hit1983/services/trackbacks/228407.html</trackback:ping><description><![CDATA[<h2>Serializing an Image</h2>
<p>Creating an image from an array of data is an easy task, but to
create a byte-array of data from an image is a little more complicated.
But it's required if you want to send a modified image to a server.</p>
<p>To create a byte-array of data from an image, we can use the
getRGB(..) method in the image class in MIDP 2.0. From the getRGB
method we get an int-array of data containing all the ARGB values of
each pixel in the image. Integers in java are four bytes, so we need to
split each int value into four byte values.</p>
<p>To mask out each of the bytes in the int we can use the 'AND &amp;'
operator and the 'shift-right &gt;&gt;' operator. Here's an example:</p>
<pre><font color="#6b8e23">int ARGB = 0xFFFFFFFF;&nbsp; // AARRGGBB <br />
int a = (ARGB &amp; 0xFF000000); <br />
int r = (ARGB &amp; 0x00FF0000); <br />
int g = (ARGB &amp; 0x0000FF00); <br />
int b = (ARGB &amp; 0x000000FF);</font></pre>
<pre><font color="#6b8e23">// Here we move each bit to the right.<br />
a = (a &gt;&gt; 24); // a = 0x000000FF <br />
r = (r &gt;&gt; 16); // r = 0x000000FF <br />
g = (g &gt;&gt; 8); // g = 0x000000FF<br />
b = b;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // b = 0x000000FF</font></pre>
<p>When we convert the integer to a byte, there are some problems since
there are only signed bytes in Java. A byte may contain values between
&#8211;128 and 127 and from our integer we'll have a value between 0 and 255.</p>
<p>Here are some conversion examples:</p>
<pre><font color="#6b8e23">Integer val 127 = byte val 127. <br />
Integer val 128 = byte val &#8211;128. <br />
Integer val 255 = byte val &#8211;1.<br />
byte ba = (byte)(a);&nbsp; // if a=0x000000FF (255), then ba = -1<br />
byte br = (byte)(r); <br />
byte bg = (byte)(g); <br />
byte bb = (byte)(b);</font></pre>
<p>We have to loop though each pixel in the image and get each pixel
value to our byte-array. When that's done, we can send the image to a
server where we can convert the byte-array back to a integer-array and
then show our picture.</p>
<p>So, when we convert the byte back to an integer we have to check if the byte value is less than zero. </p>
<pre><font color="#6b8e23">Int a, r, g, b;<br />
If(ba&lt;0)<br />
&nbsp;&nbsp;&nbsp;&nbsp; a = 256 &#8211; a;</font></pre>
<p>Now our new integer value will be between 0 and 255 and we just have
to use the 'shift-left &lt;&lt;' operator and add the values together
to get our new int-array.</p>
<pre><font color="#6b8e23">a = (a &lt;&lt; 24);<br />
r = (r &lt;&lt; 16);<br />
g = (g &lt;&lt; 8);<br />
b = (b);<br />
&nbsp; 0xFF000000 (a)<br />
+ 0x00FF0000 (r)<br />
+ 0x0000FF00 (g)<br />
+ 0x000000FF (b)<br />
= 0xFFFFFFFF (argb)</font></pre>
<pre><font color="#6b8e23">int ARGB = a+r+g+b;</font></pre>
<p>After the integer-array is recreated we can use the createRGBImage(..) method in the Image class to create our image.</p>
<p>Be aware that the byte-array is quite large as it contains all of
the ARGB values for each pixel. If an image is 100*60 px, where each
pixel is four bytes, the byte array will be 24kb.</p>
<h2><a href="/Files/lukewange-hit1983/ImageBytearryConvert.rar">ImageBytearryConvert.rar</a></h2>
<img src ="http://www.blogjava.net/lukewange-hit1983/aggbug/228407.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lukewange-hit1983/" target="_blank">LukeW</a> 2008-09-11 17:09 <a href="http://www.blogjava.net/lukewange-hit1983/archive/2008/09/11/228407.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>J2me的List总结</title><link>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222261.html</link><dc:creator>LukeW</dc:creator><author>LukeW</author><pubDate>Fri, 15 Aug 2008 07:20:00 GMT</pubDate><guid>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222261.html</guid><wfw:comment>http://www.blogjava.net/lukewange-hit1983/comments/222261.html</wfw:comment><comments>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222261.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lukewange-hit1983/comments/commentRss/222261.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lukewange-hit1983/services/trackbacks/222261.html</trackback:ping><description><![CDATA[List控件是使用频率非常高的显示控件之一了。但是最近发现它的一点不足。<br />
那就是getSelectedIndex()函数，一般情况下它都能正常工作。唯有在List处于复选模式（MULTIPLE）时，使用该函数无法获得当前高亮条选中索引，而总是返回-1。<br />
<br />
参考List控件的源代码，可以发现，List类实现了Choice接口，并且包含一个ChoiceGroup成员。它才是实现List大部分功能的大功臣。<br />
List类的getSelectedIndex()方法，实际上就是ChoiceGroup的getSelectedIndex()方法。大家可以参考Doc中关于ChoiceGroup的getSelectedIndex()方法的如下部分。<br />
&#8220;For ChoiceGroup objects of type MULTIPLE, this always returns -1
because no single value can in general represent the state of such a
ChoiceGroup.&#8221;<br />
<br />
就是说，List在多选模式（MULTIPLE）下，我们是无法获取当前高亮条所在项的索引值的。<br />
当然，你可以自己数数。<br />
<br />
也许你觉得这个问题不算严重，也许吧。但我觉得对于程序员来说，最严重的问题就是理解发生偏差。<br />
比如我，在发现这个问题之前。凭借多年的编程经验，我很确定很确定的认为，这个getSelectedIndex()永远能够获得当前光标所指项的索引值。<br />
&#8230;&#8230;当错误来临时，我百思不得其解。最后，花了好些时间去调试才发现，那个值总等于-1。操！花了太多时间去猜测原本正确的代码。<br />
我只想告诉大家，真正耗费时间最多，让人最恶心的错误，往往就是这样的问题。再操！<br />
<br />
那么有什么简单的解决办法么？<br />
我可以很负责任的告诉你，自己写一个多选的List控件吧，记住不要让getSelectedIndex()总返回-1。即使是多选，有时候也是需要这个值的。<br />
<br />
也许你想继承List,然后重载getSelectedIndex()方法。但你无法重载List的keyPressed方法（其实List等Screen控件都用Canvas写的）。<br />
&#8230;&#8230;或许还能想到别的什么好办法。不过我的选择是自己写一个List控件代替它（如果对它感兴趣，请回复，改天我在弄出来吧）。<br />
<br />
至于List的其它功能，就没什么重复的必要的，看看文档吧。
<img src ="http://www.blogjava.net/lukewange-hit1983/aggbug/222261.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lukewange-hit1983/" target="_blank">LukeW</a> 2008-08-15 15:20 <a href="http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222261.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Vector 总结</title><link>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222260.html</link><dc:creator>LukeW</dc:creator><author>LukeW</author><pubDate>Fri, 15 Aug 2008 07:19:00 GMT</pubDate><guid>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222260.html</guid><wfw:comment>http://www.blogjava.net/lukewange-hit1983/comments/222260.html</wfw:comment><comments>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222260.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lukewange-hit1983/comments/commentRss/222260.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lukewange-hit1983/services/trackbacks/222260.html</trackback:ping><description><![CDATA[Vector是在java编程中比较常用的动态数组。一直以为它是个数组的链表，当内存不够用了，就新申请一个capacityIncrement大小的数组，连到原来的链表上。<br />
在仔细阅读源代码后发现，Vector并没有任何链表的性质。它是一个纯粹的数组。当内存不够用时，就重新初始化一个容量较大新数组，然后使用System.arraycopy()函数将原有的数组copy到新的数组当中。<br />
<br />
System.arraycopy()是一个由系统平台来实现的函数，这样的系统调用性能是比较高的。<br />
即使如此，我们在写程序时，注意initialCapacity（初始容量）和capacityIncrement（增量）的设置，将会有效的减少重新定义数组并且拷贝数组的次数。<br />
<br />
例如：<br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #000000;">Vector&nbsp;v&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;Vector(</span><span style="color: #000000;">10</span><span style="color: #000000;">,&nbsp;</span><span style="color: #000000;">10</span><span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;">初始容量为10，增量10</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">Integer[]&nbsp;ints&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;Integer11;<br />
</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;i&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;&nbsp;i&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;ints.length;&nbsp;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)&nbsp;{<br />
ints&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;Integer(i);<br />
v.addElement(ints);<br />
}</span></div>
<em><em><br />
</em></em>
<p>
在这里，当v添加第11个Integer的时候，v就会自动创建一个长度为20的数组（当前容量+增量），以后每次装满数组，都会重新按增量追加数组长度。<br />
所以，设置一个合适的初始容量和增量，将会提高Vector的效率。千万别像我一样，把它当做链表。因为链表在增长空间时是不会影响到以前使用的空间和数据的。<br />
<br />
我想指出一点，因为Vector中存储的，实际上都是Object变量，大家可以把它理解为指针。重新初始化Vector内置的数组并且拷贝，相当于对一个指针数组进行操作，并不等同于对输入类型的重新分配内存。<br />
就是说Vector的重新分配，不论进行多少次都不涉及到Integer对象的创建，拷贝等工作。它只是重新创建并拷贝&#8220;指针&#8221;数组，也就是Object数组。<br />
此外，java中有一个容易被忽视的基本概念。当某个对象再没有指向该对象的引用时，垃圾回收器才会自动将其自动释放。不小心使用，很容易给程序造成内存问题。<br />
例如：<br />
<br />
</p>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #000000;">Vector&nbsp;v&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;Vector(</span><span style="color: #000000;">10</span><span style="color: #000000;">,&nbsp;</span><span style="color: #000000;">10</span><span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;">初始容量为10，增量10</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">Integer[]&nbsp;ints&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;Integer11;<br />
</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;i&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;&nbsp;i&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;ints.length;&nbsp;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)&nbsp;{<br />
ints&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;Integer(i);<br />
v.addElement(ints);<br />
}<br />
ints&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">null</span><span style="color: #000000;">;</span><span style="color: #008000;">//</span><span style="color: #008000;">对它的释放将造成数组元素的释放。但是由于v中还保存有元素的引用，所以这些Integer对象并不会被回收。</span></div>
我强调这个问题，是因为曾经写过一段代码：<br />
Image img = Image.createImage(&#8221;/xxx.png&#8221;);<br />
Image img2 = img;<br />
这张图片非常大，在使用完img之后，我释放掉它，然后重新申请另一个图片。于是，内存爆了。因为img2仍然持有xxx.png的引用，所以无法释放。<br />
Vectro中存储的也是引用，所以在使用时应该更加注意编程规范，以免发生类似的问题。其实应该尽量避免使用多个引用。<br />
<br />
上面说了这些题外话，正是想警告各位程序员，Vector是一个可变的Object数组，一个引用数组。所以请大家使用时要小心，别想当然的以为它是一个容器，它里面存储的可不是对象，而是引用。<br />
而且，在释放曾经加入到vector的对象时，对象本身并不会被真正释放，得到回收。只是原有的引用无法再使用罢了。<br />
<br />
最后还有一点技巧。<br />
应该尽量使用索引获取对象，避免使用IndexOf()方法。把它当做堆栈来使用时更应该注意，要避免使用insertElementAt()方法。<br />
此外，j2me中的Stack类是基于Vect<em><em><em><em>or实现的，使用时也要留心。</em></em></em></em>
<img src ="http://www.blogjava.net/lukewange-hit1983/aggbug/222260.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lukewange-hit1983/" target="_blank">LukeW</a> 2008-08-15 15:19 <a href="http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222260.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>汉字转拼音</title><link>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222256.html</link><dc:creator>LukeW</dc:creator><author>LukeW</author><pubDate>Fri, 15 Aug 2008 07:15:00 GMT</pubDate><guid>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222256.html</guid><wfw:comment>http://www.blogjava.net/lukewange-hit1983/comments/222256.html</wfw:comment><comments>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222256.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lukewange-hit1983/comments/commentRss/222256.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lukewange-hit1983/services/trackbacks/222256.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 在网上参考了一些汉字转换到拼音的资料。思路应该只有以下两种。1，查表法。这样做需要一个庞大的映射表，在j2me环境下不大合适。不过效果好，有些还支持多音字。2，使用GB字库的映射关系。因为GB2312及其扩展GBK的汉字编码都根据区位于拼音存在映射关系。实际上网络上的大部分文章都是根据第二种方法来实现的。我也是采用这种方法，因为它基本上可以利用GB2312字库，直接映射成拼音。...&nbsp;&nbsp;<a href='http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222256.html'>阅读全文</a><img src ="http://www.blogjava.net/lukewange-hit1983/aggbug/222256.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lukewange-hit1983/" target="_blank">LukeW</a> 2008-08-15 15:15 <a href="http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222256.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>GB2312转换Unicode</title><link>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222250.html</link><dc:creator>LukeW</dc:creator><author>LukeW</author><pubDate>Fri, 15 Aug 2008 06:58:00 GMT</pubDate><guid>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222250.html</guid><wfw:comment>http://www.blogjava.net/lukewange-hit1983/comments/222250.html</wfw:comment><comments>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222250.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lukewange-hit1983/comments/commentRss/222250.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lukewange-hit1983/services/trackbacks/222250.html</trackback:ping><description><![CDATA[之前的文章介绍了在j2me环境下GB2312转换为UTF-8的方法。<br />
后来继续对编码及char类型进行学习，发现一些有趣的问题。<br />
首先java环境下的char类型变量，实际上就是以unicode方式存储的。<br />
所以以下方法有效：<br />
<br />
输入unicode编码的byte数组，即可两两拼接成一个char。<br />
而String类型实际上就是在char数组的基础上衍生出来的。大家可以参考cldc的源代码。<br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;String&nbsp;read_Uni(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[]&nbsp;word_unicode)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StringBuffer&nbsp;stringbuffer&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;StringBuffer(</span><span style="color: #000000;">""</span><span style="color: #000000;">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;j&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;&nbsp;j&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;word_unicode.length;)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;l&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;word_unicode[j</span><span style="color: #000000;">++</span><span style="color: #000000;">];<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;h&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;word_unicode[j</span><span style="color: #000000;">++</span><span style="color: #000000;">];<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;c&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">char</span><span style="color: #000000;">)&nbsp;((l&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0xff</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;((h&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">8</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0xff00</span><span style="color: #000000;">));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stringbuffer.append(c);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;stringbuffer.toString();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
</span></div>
<br />
j2me环境下也是如此。<br />
所以在第一次给出的转换类中，提供的gb2312到utf-8直接转换的快速方法。现在看来是画蛇添足了。<br />
<br />
根据以上经验，更新转换类如下：<br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;HGB2312&nbsp;{<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[]&nbsp;map&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[</span><span style="color: #000000;">15228</span><span style="color: #000000;">];<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;HGB2312()&nbsp;</span><span style="color: #0000ff;">throws</span><span style="color: #000000;">&nbsp;Exception&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InputStream&nbsp;is&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;getClass().getResourceAsStream(</span><span style="color: #000000;">"</span><span style="color: #000000;">/gb2u.dat</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;is.read(map);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;is.close();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;String&nbsp;gb2utf8(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[]&nbsp;gb)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StringBuffer&nbsp;sb&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;StringBuffer();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;c,&nbsp;h,&nbsp;l,&nbsp;ind;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;i&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;&nbsp;i&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;gb.length;)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(gb[i]&nbsp;</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sb.append((</span><span style="color: #0000ff;">char</span><span style="color: #000000;">)&nbsp;gb[i</span><span style="color: #000000;">++</span><span style="color: #000000;">]);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">256</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;gb[i</span><span style="color: #000000;">++</span><span style="color: #000000;">];<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;l&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">256</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;gb[i</span><span style="color: #000000;">++</span><span style="color: #000000;">];<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;h&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0xA0</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;l&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;l&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0xA0</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(h&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">9</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ind&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(h&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">94</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;l)&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(byte2Int(map[ind])&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">8</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;byte2Int(map[ind&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">]));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sb.append((</span><span style="color: #0000ff;">char</span><span style="color: #000000;">)&nbsp;c);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(h&nbsp;</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">9</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;h&nbsp;</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">14</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sb.append((</span><span style="color: #0000ff;">char</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(h&nbsp;</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">14</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h&nbsp;</span><span style="color: #000000;">-=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">6</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ind&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(h&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">94</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;l)&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(byte2Int(map[ind])&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">8</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;byte2Int(map[ind&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">]));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sb.append((</span><span style="color: #0000ff;">char</span><span style="color: #000000;">)&nbsp;c);<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sb.append((</span><span style="color: #0000ff;">char</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;sb.toString();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;byte2Int(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">&nbsp;b)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(b&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">256</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;b;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;b;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
</span></div>
<br />
这个方法明显要比第一次快很多了，直接查表，然后拼接成String，不需要转换成utf-8编码。<br />
<br />
数据文件请在<a href="http://download.csdn.net/source/263609%E8%8E%B7%E5%8F%96">http://download.csdn.net/source/263609获取</a><br />
<br />
总之，java中的char类型实际上是存储了unicode编码。目前在nokia 5300上测试通过。<br />
我觉得其它机器也应该是这样。如果哪位大侠知道这方面的资料，请赐教。
<img src ="http://www.blogjava.net/lukewange-hit1983/aggbug/222250.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lukewange-hit1983/" target="_blank">LukeW</a> 2008-08-15 14:58 <a href="http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222250.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>MIDP2.0及MIDP数字签名</title><link>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222247.html</link><dc:creator>LukeW</dc:creator><author>LukeW</author><pubDate>Fri, 15 Aug 2008 06:53:00 GMT</pubDate><guid>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222247.html</guid><wfw:comment>http://www.blogjava.net/lukewange-hit1983/comments/222247.html</wfw:comment><comments>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222247.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lukewange-hit1983/comments/commentRss/222247.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lukewange-hit1983/services/trackbacks/222247.html</trackback:ping><description><![CDATA[本文档是 WoTrust 根据 Forum Nokia 提供的技术文档《MIDP 2.0: Tutorial On Signed
MIDlets》翻译整理的，请同时参考此英文原文文档。请用户在编写 MIDlet 和签名 MIdlet 之前阅读此文档，以便对 MIDP2.0
的安全机制有一个深刻的理解，有助于用户能用好 MIDlet 代码签名证书。<br />
<br />
一、概述 <br />
<br />
MIDP2.0 采用了全新的安全机制，这对于需要调用一个敏感的(重要的)函数和 API 的 MIDlet 开发者来讲是必须了解的，如：网络连接 API 、消息 API 和推 (Push) 函数等，还有一些可选的 MIDP 包也有许多受限制的 API 。 <br />
<br />
虽然购买代码签名证书需要费 用，但签名 MIDlet 对开发者来讲是收益非浅的，因为许多受保护的 API
都是需要签名的，以保护开发者和用户的利益。当然，有些应用是不需要签名的，如有些不需要联网的仅用到一些图形 API
的小游戏软件。但一些重要的应用，如：连接网络、发送短消息 ( 短信和彩信 ) 或访问移动终端 ( 智能手机、 PDA 等，以下简称为手机 )
上的 PIM( 个人信息管理 ) 数据等等都需要签名。 <br />
<br />
数字签名 MIDlet 的好处包括： <br />
<br />
(1) 基于 MIDlet 的安全策略，某些功能是必须签名才能使用的，而有些功能虽然不签名也可以使用，但必须要求用户在使用时确认和修改其安全策略，如：写用户数据缺省是不允许没有签名的 MIDlet 操作的； <br />
<br />
(2) 基于手机的系统安全和移动网络的安全考虑，某些手机制造商、移动运营商等可能拒绝没有签名的 MIDlet 在手机上安装和运行； <br />
<br />
(3) 大大改善用户体验，让用户使用方便，使得用户不会遭遇调用受保护 API 时的安全警告的烦恼； <br />
<br />
(4) 出于安全考虑，安装没有签名的 MIDlet 是会有安全警告的，而相反，安装已经签名的 MIDlet 则不会出现烦人的警告，手机会自动验证签名而顺利地安装成功； <br />
<br />
(5) 已经签名的 MIDlet 将使得用户能改善其低安全策略设置，提高手机的安全性； <br />
<br />
(6) 确保已经签名的 MIDlet 不会被非法篡改和非法盗用。 <br />
<br />
二、 MIDP 2.0 安全机制 <br />
<br />
MIDP 是一个开放的平台，使得任何人都可以为支持 MIDP 的设备开发各种应用软件，一般都是移动终端设备。 MIDlet
套件可以以匿名方式通过网络下载，非常方便，但这也会带来许多安全问题和隐私信息保护问题，用户会问： MIDlet
能把用户的个人信息发给不知道的服务器吗？会自动产生没有授权的呼叫或短消息而给用户带来费用吗？恶意软件会破坏手机？等等。 <br />
<br />
除了 Java 语言的安全特性外， MIDP 还增加了许多安全考虑。 MIDP 2.0 比 MIDP 1.0 增强了安全策略，把 API
分为普通 API 和敏感 API ，如：通过 HTTP 协议访问移动网络，由于会给用户产生费用， 所以被列为 敏感 API 。 MIDlet
2.0 推出了可信任 MIDlet(trusted) 和不可信任 MIDlet(untrusted) 的概念，一个不可信任 MIDlet
只能访问有限的 API ，同时还需要用户手动确认并修改其安全策略；而可信任 MIDlet 则自动继承系统中的安全策略而获得访问许可。 <br />
<br />
许可 (Permissions) 用于需要身份认证的 敏感 API 。 MIDP 2.0 要求调用 敏感 API
之前必须获得必要的许可，这些许可包的命名同 J2SE 许可，如： HTTP 连接许可同样称为：
javax.microedition.io.Connector.http 。 有关许可的文档同意归类在受保护 API 中。 <br />
<br />
2.1 Protection Domains( 保护域 ) <br />
<br />
保护域是 MIDP 2.0
中一个非常重要的安全概念，一个保护域就是一个许可集和一种交互模式，这些许可既可以是自己继承的，也可能是用户设置的，前者称为允许
(allowed) ，而后者称为用户允许 (user permission) 。当一个 MIDlet
被安装后，它被分配到一个指定的保护域而获得它的许可和交互模式。 <br />
<br />
而用户允许则需要用户自己决定是否同意，用户既拒绝一个许可，也可以同意。用户允许有 3 种交互模式： blanket( 普遍适用 ) 、
session( 短期适用 ) 和 oneshot( 本次适用 ) ， 普遍适用 模式就是 MIDlet
安装时获得的许可一直有效，除非用户取消这些许可；而 短期适用 模式则是指第一次调用 API 时需要用户允许，有效期到此 MIDlet
套件运行结束；而 本次适用 模式则在每次调用 API 时都要求用户允许。保护域为用户许可定义了缺省的交互模式。 <br />
<br />
一个 MIDlet 套件使用 MIDlet-Permissions 和 MIDlet-Permissions-Opt
属性来明确地定义其许可，可以是在 JAD 文件中定义，也可以在 manifest 文件中定义。其中： MIDlet-Permissions
定义了 MIDlet 套件中必须具有的许可，而 MIDlet-Permissions-Opt
则定义希望具有的许可。如：一个应用软件的基本要求是要有 http 连接才能正常工作，同时，也可以使用 https 连接 ( 服务器部署了
SSL 证书 ) 来增强安全性，但不是必须的，这样，这个应用软件的应用描述可以是这样： <br />
<br />
MIDlet-Permissions: javax.microedition.io.Connector.http <br />
<br />
MIDlet-Permissions-Opt: javax.microedition.io.Connector.https <br />
<br />
请注意：一个 MIDlet 所要求的许可必须是安装时分配的保护域所具有的许可的子集。如： Nokia S60 MIDP Emulator
Prototype 2.0 (SDK) 有一个叫做&#8220; minimum &#8221;的域，此域没有任何许可。所以，如果一个含有许多许可的已经签名的
MIDlet
如果被安装到此域，则会安装失败，因为此域不支持这些许可。同样，如果一个许可的名称有拼写错误，则一样会导致安装失败，因为域中没有此拼写错误的许可。
<br />
<br />
MIDP 2.0 为 GSM/UTMS 设备定义了 4 种保护域： manufacturer( 设备制造商 ) , operator(
移动运营商 ) , trusted third party( 可信任的第三方 ) , and untrusted( 不受信任域 ) ， 除了
untrusted 域外，每个保护域都对应一组根证书，用于签名 MIDlet
的签名证书的根证书必须包含在这些根证书中，使用不同的签名证书签名的 MIDlet
将被自动归类予根证书所属的保护域，根证书与保护域的关系是：一个保护域可以有许多个根证书，而一个根证书只能对应于一个保护域。 <br />
<br />
具体来讲， manufacturer 域属于设备制造商，其根证书是设备制造商自己的根证书；而 operator 域运营商，一般使用其 SIM
卡中的根证书；而 trusted third party 域则预置了全球知名的数字证书颁发机构 (CA) 的根证书，用于验证由 CA 颁发的
MIDlet 签名证书；而 untrusted 域没有根证书，将用于没有签名的 MIDlet 和 MIDP 1.0 。 <br />
<br />
Thawte 和 VeriSign 的根证书已经预置在 trusted third party 域 中，其 Java 代码签名证书可以用于签名
MIDlet 。当然，用户也可以选择使用设备制造商和移动运营商颁发的证书，只要其根证书已经包含在手机的 4 个保护域中。据 WoTrust
了解，大多数摩托罗拉 (Motorola) 手机只支持设备制造商域，所以，只能向 Motorola 申请签名服务了。 <br />
<br />
请注意：由于 MIDP 2.0 也在不断地修改和增补，所以，可能不用的移动网络运营商有不同的保护域和许可，用户可能需要向移动运营商了解详细信息。而最简单的方法是检查目标用户所使用的手机的根证书是否有计划购买的 MIDlet 签名证书的根证书。 <br />
<br />
2.2 Untrusted MIDlet ( 不受信任的 MIDlet) <br />
<br />
MIDP 2.0 定义了那些 API 是 untrusted 的，这些 Jar 文件的来源和完整性是不能被手机验证的。但这并不意味着这些
MIDlet 不能被安装和运行，而是运行这些 MIDlet 需要用户人工确认允许。而所有 MIDP 1.0 的 MIDlets 都被定义为
untrusted 。 <br />
<br />
untrusted 的 MIDlets 只能调用一个不需要许可保护的 API ，如： <br />
java.util <br />
java.lang <br />
java.io <br />
javax.microedition.rms <br />
javax.microedition.midlet <br />
javax.microedition.lcdui <br />
javax.microedition.lcdui.game <br />
javax.microedition.media <br />
javax.microedition.media.control <br />
<br />
如果 untrusted MIDlet 套件试图调用一个被保护的 API 而且没有被人工允许，则会产生一个 SecurityException
而被 MIDlet 按照安全策略处理。请注意： Nokia 的 UI API 是不被保护的，包括类： com.nokia.mid.sound
和 com.nokia.mid.ui 。 <br />
<br />
2.3 Trusted MIDlets ( 可信任的 MIDlets) <br />
<br />
如果手机能验证 MIDlet 的身份和完整性 ( 也就是已经数字签名 ) ，则会自动分配一个合适的保护 域这种 MIDlet
套件就称为可信任的 MIDlet 。一个可信任的 MIDlet 套件所要求的许可将被准许，只要所属的保护域拥有这种许可，假如许可：
javax.microedition.io.Connector.http 已经在所属保护域中是允许的，则 MIDlet 在打开一个 http
连接时是不需要用户确认的。 <br />
<br />
请不要混淆了可信任的 MIDlet 套件和可信任的保护域的不同，每个可信任的 MIDlet 套件依据安全策略被分配到一个特定的保护域。 <br />
<br />
您需要使用一个手机中已经预置的根证书的证书颁发机构颁发的代码签名证书来签名 MIDlet ，否则将不能通过身份验证。成功签名后的 JAD
文件中一定会包含有整个签名证书的证书链，属性名称为： MIDlet-Certificate-1-1 就是您的签名证书，而
MIDlet-Certificate-1-2 就是 CA 的中级根证书，而 MIDlet-Certificate-1-3 就是 CA
的顶级根证书。同时还会有一个 MIDlet-Jar-RSA-SHA1 属性就是 JAR 文件的摘要。 <br />
<br />
当一个 MIDlet 被下载或被安装时， MIDlet 应用管理器首先会检查 JAD 文件中是否包含了 MIDlet-Jar-RSA-SHA1
属 性，如果有，则启动如下验证过程：首先会读出 MIDlet-Certificate-1-1 、 MIDlet-Certificate-1-2
和 MIDlet-Certificate-1-3
属性中的证书，并与已经预置的根证书相比较，如果证书链能被根证书验证，则表明开发者身份已经被验证。接着就会使用用户证书来解密
MIDlet-Jar-RSA-SHA1 属 性的摘要，再计算出已经下载的 Jar 文件的摘要，比较两个摘要是否相等，如果相等，则表明
MIDlet 代码自签名后没有被修改。这样，既验证了身份又检查了完整性的 MIDlet 会被分配到所属根证书所对应的保护域中。但是，如果
MIDlet 中的许可属性 ( MIDlet-Permissions ) 中有一个或多个不属于所属的保护域，则仍然不允许安装。而如果
MIDlet 中的可选许可属性 ( MIDlet-Permissions-Opt )
中有一个或多个不属于所属的保护域，会允许安装。可见，正确设置许可属性和可选许可属性非常重要。 <br />
<br />
2.4 Function Groups ( 功能分组 ) <br />
<br />
为了简化用户管理操作， MIDlet 把一些类似功能分组，这样，用户只需对功能组设置许可即可。如：许可 &#8220;Net Access&#8221;( 网络访问
) 组来代替许可 javax.microedition.io.Connector.http ，这对于简化手机的交互操作非常有用。 <br />
<br />
MIDP 2.0 和 JTWI 定义了如下 7 个功能组： <br />
<br />
(1) Net Access: 包括所有网络连接许可； <br />
<br />
(2) Messaging: 包括所有与发送和接收短消息 ( 短信和彩信 等 ) 相关的许可； <br />
<br />
(3) Auto Invocation : 包括与自动启动 MIDlet 相关的许可，如： Push Registration <br />
<br />
(4) Local Connectivity : 包括与本地连接相关的许可，如： IrDA 或 蓝牙； <br />
<br />
(5) Multimedia Recording : 包括与允许录音、照相、摄像等相关的许可； <br />
<br />
(6) Read User Data : 包括读取用户数据相关的许可，如：通讯录、日程表等； <br />
<br />
(7) Write User Data : 包括写用户数据相关的许可。 <br />
<br />
不同的手机支持不同的功能组，如： Multimedia Recording 就不会包含在没有摄录装置的手机中。当然，也有可能将来会增加更多的功能组。 <br />
<br />
功能组也同时定义了不同的域的不同交互方式，如：在不信任域， &#8220;Net Access&#8221; ( 网络访问 ) 被设置为 session( 短期适用
) 或 denied( 拒绝 ) ，而在可信任域则可以设置为 oneshot 、 blanket 和 denied 的。 <br />
<br />
三、仿真器和手机的缺省安全设置<br />
<br />
让我们来看看具体的使用 Thawte 或 VeriSign 代码签名证书签名后的 MIDlet 在 trusted third party
域中的所有缺省许可，如下图 1 所示，点击 NDS 3.0 的&#8220; Config Emulators &#8221;就可以看到仿真器在 trusted
third party 域的缺省安全设置是&#8220; Ask first time &#8221;，即第 1 次使用是需要确认：<br />
<p>
<br />
如下图 2 所示，您可以下拉所有功能组的许可设置，如&#8220; Network Access &#8221;就有 4 个选项可以修改： Ask first time 、 Ask every time 、 Always allowed 和 Not allowed ： <br />
</p>
<br />
而如下图 3 所示，在&#8220; Real Life &#8221;模式，也就是实际手机的运行模式，可以看出：定义的 7 个功能组都是&#8220; Always
allowed &#8221; ( 总是允许 ) ，这就显示出 MIDlet
签名对于开发商来讲是多么的重要，将大大方便了用户的使用，再也不需要用户操作烦人的系列确认了。
<img src ="http://www.blogjava.net/lukewange-hit1983/aggbug/222247.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lukewange-hit1983/" target="_blank">LukeW</a> 2008-08-15 14:53 <a href="http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222247.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>树形结构</title><link>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222244.html</link><dc:creator>LukeW</dc:creator><author>LukeW</author><pubDate>Fri, 15 Aug 2008 06:51:00 GMT</pubDate><guid>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222244.html</guid><wfw:comment>http://www.blogjava.net/lukewange-hit1983/comments/222244.html</wfw:comment><comments>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222244.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lukewange-hit1983/comments/commentRss/222244.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lukewange-hit1983/services/trackbacks/222244.html</trackback:ping><description><![CDATA[树形结构（tree）是比较常用的数据结构了，MIDP中没有它的身影，不然我就不用写这篇文章了。<br />
代码如下:<br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008000;">/**</span><span style="color: #008000;"><br />
&nbsp;*<br />
&nbsp;*&nbsp;</span><span style="color: #808080;">@author</span><span style="color: #008000;">&nbsp;hunhun1981<br />
&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br />
</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;HTree&nbsp;{<br />
&nbsp;<br />
&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;HNode&nbsp;root;<br />
&nbsp;<br />
&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;HNode&nbsp;current;<br />
&nbsp;<br />
&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;currDepth;<br />
&nbsp;<br />
&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;maxDepth;<br />
&nbsp;<br />
&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;HTree(Object&nbsp;rootValue)&nbsp;{<br />
&nbsp;&nbsp;root&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;HNode(</span><span style="color: #0000ff;">null</span><span style="color: #000000;">,&nbsp;rootValue);<br />
&nbsp;&nbsp;current&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;root;<br />
&nbsp;}<br />
&nbsp;<br />
&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;goRoot()&nbsp;{<br />
&nbsp;&nbsp;current&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;root;<br />
&nbsp;&nbsp;currDepth&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br />
&nbsp;}<br />
&nbsp;<br />
&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">boolean</span><span style="color: #000000;">&nbsp;goChild(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;index)&nbsp;{<br />
&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(current.childList&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">null</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(current.childList.size()&nbsp;</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;index&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;current.childList.size())&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;current&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(HNode)&nbsp;current.childList.elementAt(index);<br />
&nbsp;&nbsp;&nbsp;&nbsp;currDepth</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(currDepth&nbsp;</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;maxDepth)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;maxDepth&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;currDepth;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">true</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">false</span><span style="color: #000000;">;<br />
&nbsp;}<br />
&nbsp;<br />
&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;goBack()&nbsp;{<br />
&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(current.father&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">null</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;current&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;current.father;<br />
&nbsp;&nbsp;&nbsp;currDepth&#8211;;<br />
&nbsp;&nbsp;}<br />
&nbsp;}<br />
&nbsp;<br />
&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;Object&nbsp;getCurrent()&nbsp;{<br />
&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;current.value;<br />
&nbsp;}<br />
&nbsp;<br />
&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;getCurrentDepth()&nbsp;{<br />
&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;currDepth;<br />
&nbsp;}<br />
&nbsp;<br />
&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;getMaxDepth()&nbsp;{<br />
&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;maxDepth;<br />
&nbsp;}<br />
&nbsp;<br />
&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;Object[]&nbsp;getChilds()&nbsp;{<br />
&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(current.childList&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">null</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(current.childList.size()&nbsp;</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;Object[]&nbsp;ret&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;Object[current.childList.size()];<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;i&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;&nbsp;i&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;ret.length;&nbsp;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ret[i]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;((HNode)&nbsp;current.childList.elementAt(i)).value;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;ret;<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">null</span><span style="color: #000000;">;<br />
&nbsp;}<br />
&nbsp;<br />
&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;Object&nbsp;getChild(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;index)&nbsp;{<br />
&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(current.childList&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">null</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(current.childList.size()&nbsp;</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;index&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;current.childList.size())&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;((HNode)&nbsp;current.childList.elementAt(index)).value;<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">null</span><span style="color: #000000;">;<br />
&nbsp;}<br />
&nbsp;<br />
&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;addChild(Object&nbsp;obj)&nbsp;{<br />
&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(current.childList&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">null</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;current.childList&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;Vector();<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;current.childList.addElement(</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;HNode(current,&nbsp;obj));<br />
&nbsp;}<br />
&nbsp;<br />
&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;addChilds(Object[]&nbsp;objs)&nbsp;{<br />
&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(current.childList&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">null</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;current.childList&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;Vector();<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;i&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;&nbsp;i&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;objs.length;&nbsp;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;current.childList.addElement(</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;HNode(current,&nbsp;objs[i]));<br />
&nbsp;&nbsp;}<br />
&nbsp;}<br />
&nbsp;<br />
&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;hasChild()&nbsp;{<br />
&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(current.childList&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">null</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">||</span><span style="color: #000000;">&nbsp;current.childList.size()&nbsp;</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;{<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;current.childList.size();<br />
&nbsp;&nbsp;}<br />
&nbsp;}<br />
&nbsp;<br />
&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;HNode&nbsp;{<br />
&nbsp;<br />
&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;Vector&nbsp;childList;<br />
&nbsp;<br />
&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;HNode&nbsp;father;<br />
&nbsp;<br />
&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;Object&nbsp;value;<br />
&nbsp;<br />
&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;HNode(HNode&nbsp;father,&nbsp;Object&nbsp;value)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.value&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;value;<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.father&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;father;<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.childList&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">null</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;}<br />
&nbsp;}<br />
}<br />
<br />
</span></div>
<br />
这个类实现简单，没有包含复杂的功能，仅仅用来做树形数据的存储还是不错的。比如游戏中用来管理场景，管理资源；应用中用来作分类数据的表现等等。完全足以胜任。<br />
使用方法如下：<br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #000000;">HTree&nbsp;tree&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;HTree(&#8221;root&#8221;);</span><span style="color: #008000;">//</span><span style="color: #008000;">会自动创建一个默认的根节点</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">tree.addChild(&#8221;天才&#8221;);</span><span style="color: #008000;">//</span><span style="color: #008000;">在根节点添加新的节点</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">tree.addChild(&#8221;白痴&#8221;);<br />
tree.goChild(</span><span style="color: #000000;">0</span><span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;">进入到当前节点的第一个节点（天才）。</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">tree.addChild(&#8221;天才1号&#8221;);</span><span style="color: #008000;">//</span><span style="color: #008000;">在当前节点（天才）添加新的节点</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">tree.addChild(&#8221;天才2号&#8221;);<br />
tree.goBack();</span><span style="color: #008000;">//</span><span style="color: #008000;">返回当前节点（天才）的父节点（根）</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">tree.goChild(</span><span style="color: #000000;">1</span><span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;">进入到当前节点的第二个节点（白痴）。</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">tree.addChild(&#8221;白痴1号&#8221;);</span><span style="color: #008000;">//</span><span style="color: #008000;">在当前节点（白痴）添加新的节点</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">tree.addChild(&#8221;白痴2号&#8221;);<br />
tree.goRoot();</span><span style="color: #008000;">//</span><span style="color: #008000;">完成创建后将当前节点设置为根节点。</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&#8230;</span></div>
<br />
上面的代码创建了一棵完整的树，当然，您可以使用任何对象代替这里存储的String对象。<br />
还有一些方法，一看函数名大概都能明白，就不再唠叨了。<br />
遍历的方法于上面创建树的方法相似，总之，要注意当前节点的位置，以免下次使用时处在错误的位置。<br />
有兴趣的朋友可以扩展一下遍历方法。不过我觉得没必要。因为J2ME环境下更需要的是树形结构，而不是强大的tree对象。<br />
<br />
总之，我比较倾向于简单实现，希望它不太让人觉得简陋就好。从实用出发，它还是能够满足大部分受限平台的需求的。
<img src ="http://www.blogjava.net/lukewange-hit1983/aggbug/222244.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lukewange-hit1983/" target="_blank">LukeW</a> 2008-08-15 14:51 <a href="http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222244.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>URLEncoding</title><link>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222242.html</link><dc:creator>LukeW</dc:creator><author>LukeW</author><pubDate>Fri, 15 Aug 2008 06:49:00 GMT</pubDate><guid>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222242.html</guid><wfw:comment>http://www.blogjava.net/lukewange-hit1983/comments/222242.html</wfw:comment><comments>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222242.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lukewange-hit1983/comments/commentRss/222242.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lukewange-hit1983/services/trackbacks/222242.html</trackback:ping><description><![CDATA[URLEncoding是用于解决链接字符串中包含中文字符的一种转换编码。各种编程环境下几乎带有它的库函数。<br />
<br />
不过，J2ME除外。<br />
<br />
好在JAVA的源代码中带有这个类，我们把它拷贝到J2ME环境下编译到我们的应用当中就可以了。<br />
<br />
该文件位于JDK的目录下src.zip文件中，名叫URLEncoder.java。<br />
<br />
但是，这个文件还需要做很多修改才能使用在J2ME环境中。<br />
<br />
先警告大家，有几个真机（其中一个就是索爱的，好像是k500c），不管输入什么样的Encodeing都会出错，甚至是&#8220;UTF-8&#8221;。所以我一怒之下
去除了Encodeing参数。（这可是在实际应用中得出的结论，不去掉的话可以在大部分情况下正常使用，但是，现实总是有点缺陷）<br />
<br />
修改后的代码如下，大家请放心使用。如果有兴趣，可以比较两个代码，看看我改动了什么地方。<br />
<span style="font-family: monospace;"><br />
<br />
</span>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;java.io.ByteArrayOutputStream;<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;java.io.DataOutputStream;<br />
</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;HURLEncoder&nbsp;{<br />
&nbsp;<br />
&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">boolean</span><span style="color: #000000;">[]&nbsp;dontNeedEncoding;<br />
&nbsp;<br />
&nbsp;</span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;{<br />
&nbsp;&nbsp;dontNeedEncoding&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">boolean</span><span style="color: #000000;">[</span><span style="color: #000000;">256</span><span style="color: #000000;">];<br />
&nbsp;<br />
&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;i&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;&nbsp;i&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">256</span><span style="color: #000000;">;&nbsp;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">boolean</span><span style="color: #000000;">&nbsp;b&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;((i&nbsp;</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">&nbsp;&#8216;</span><span style="color: #000000;">0</span><span style="color: #000000;">&#8242;)&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;(i&nbsp;</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">&nbsp;&#8216;</span><span style="color: #000000;">9</span><span style="color: #000000;">&#8242;))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">||</span><span style="color: #000000;">&nbsp;((i&nbsp;</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">&nbsp;&#8216;A&#8217;)&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;(i&nbsp;</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">&nbsp;&#8216;Z&#8217;))&nbsp;</span><span style="color: #000000;">||</span><span style="color: #000000;">&nbsp;((i&nbsp;</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">&nbsp;&#8216;a&#8217;)&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;(i&nbsp;</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">&nbsp;&#8216;z&#8217;));<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;dontNeedEncoding[i]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;b;<br />
&nbsp;&nbsp;}<br />
&nbsp;<br />
&nbsp;&nbsp;dontNeedEncoding[&#8217;&nbsp;&#8216;]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">true</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;dontNeedEncoding[&#8217;</span><span style="color: #000000;">-</span><span style="color: #000000;">'</span><span style="color: #000000;">]&nbsp;=&nbsp;true;</span><span style="color: #000000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;dontNeedEncoding[&#8217;_</span><span style="color: #000000;">'</span><span style="color: #000000;">]&nbsp;=&nbsp;true;</span><span style="color: #000000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;dontNeedEncoding[&#8217;.</span><span style="color: #000000;">'</span><span style="color: #000000;">]&nbsp;=&nbsp;true;</span><span style="color: #000000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;dontNeedEncoding[&#8217;</span><span style="color: #000000;">*</span><span style="color: #000000;">'</span><span style="color: #000000;">]&nbsp;=&nbsp;true;</span><span style="color: #000000;"><br />
</span><span style="color: #000000;">&nbsp;}<br />
&nbsp;<br />
&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;String&nbsp;encode(String&nbsp;s)&nbsp;{<br />
&nbsp;<br />
&nbsp;&nbsp;</span><span style="color: #0000ff;">boolean</span><span style="color: #000000;">&nbsp;wroteUnencodedChar&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">false</span><span style="color: #000000;">;<br />
&nbsp;<br />
&nbsp;&nbsp;StringBuffer&nbsp;writer&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;StringBuffer();<br />
&nbsp;<br />
&nbsp;&nbsp;StringBuffer&nbsp;out&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;StringBuffer(s.length());<br />
&nbsp;<br />
&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;i&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;&nbsp;i&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;s.length();&nbsp;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;c&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;s.charAt(i);<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;((c&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">256</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;dontNeedEncoding[c])&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(c&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;&#8216;&nbsp;&#8216;)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;&#8216;</span><span style="color: #000000;">+</span><span style="color: #000000;">&#8217;;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;out.append((</span><span style="color: #0000ff;">char</span><span style="color: #000000;">)&nbsp;c);<br />
&nbsp;&nbsp;&nbsp;&nbsp;wroteUnencodedChar&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">true</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">try</span><span style="color: #000000;">&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(wroteUnencodedChar)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writer&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;StringBuffer();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wroteUnencodedChar&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">false</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writer.append(c);<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(c&nbsp;</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0xD800</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;c&nbsp;</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0xDBFF</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;((i&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;s.length())&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;d&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">)&nbsp;(s.charAt(i&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">));<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(d&nbsp;</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0xDC00</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;d&nbsp;</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0xDFFF</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writer.append(d);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">catch</span><span style="color: #000000;">&nbsp;(Exception&nbsp;e)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writer&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;StringBuffer();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">continue</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;str&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;writer.toString();<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;ByteArrayOutputStream&nbsp;baos&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;ByteArrayOutputStream();<br />
&nbsp;&nbsp;&nbsp;&nbsp;DataOutputStream&nbsp;dos&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;DataOutputStream(baos);<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">try</span><span style="color: #000000;">&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dos.writeUTF(str);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dos.flush();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">catch</span><span style="color: #000000;">&nbsp;(Exception&nbsp;e)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[]&nbsp;temp&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;baos.toByteArray();<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[]&nbsp;ba&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[temp.length&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">2</span><span style="color: #000000;">];<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;ix&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;&nbsp;ix&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;ba.length;&nbsp;ix</span><span style="color: #000000;">++</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ba[ix]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;temp[ix&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">2</span><span style="color: #000000;">];<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;j&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;&nbsp;j&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;ba.length;&nbsp;j</span><span style="color: #000000;">++</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.append(&#8217;</span><span style="color: #000000;">%</span><span style="color: #000000;">'</span><span style="color: #000000;">);</span><span style="color: #000000;"><br />
</span><span style="color: #000000;">&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;ch&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;forDigit((ba[j]&nbsp;</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">4</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0xF</span><span style="color: #000000;">,&nbsp;</span><span style="color: #000000;">16</span><span style="color: #000000;">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.append(ch);<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ch&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;forDigit(ba[j]&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0xF</span><span style="color: #000000;">,&nbsp;</span><span style="color: #000000;">16</span><span style="color: #000000;">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.append(ch);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;writer&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;StringBuffer();<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">try</span><span style="color: #000000;">&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dos.close();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;baos.close();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">catch</span><span style="color: #000000;">&nbsp;(Exception&nbsp;e)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;}<br />
&nbsp;<br />
&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;out.toString();<br />
&nbsp;}<br />
&nbsp;<br />
&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;forDigit(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;digit,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;radix)&nbsp;{<br />
&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;((digit&nbsp;</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">&nbsp;radix)&nbsp;</span><span style="color: #000000;">||</span><span style="color: #000000;">&nbsp;(digit&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">))&nbsp;{<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;&#8216;</span><span style="color: #000000;">0</span><span style="color: #000000;">&#8242;;<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(digit&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">10</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">char</span><span style="color: #000000;">)&nbsp;(&#8217;</span><span style="color: #000000;">0</span><span style="color: #000000;">&#8242;&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;digit);<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">char</span><span style="color: #000000;">)&nbsp;(&#8217;A&#8217;&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;digit&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">10</span><span style="color: #000000;">);<br />
&nbsp;}<br />
}&nbsp;<br />
<br />
</span></div>
<img src ="http://www.blogjava.net/lukewange-hit1983/aggbug/222242.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lukewange-hit1983/" target="_blank">LukeW</a> 2008-08-15 14:49 <a href="http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222242.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>GB2312转换为UTF-8</title><link>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222241.html</link><dc:creator>LukeW</dc:creator><author>LukeW</author><pubDate>Fri, 15 Aug 2008 06:47:00 GMT</pubDate><guid>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222241.html</guid><wfw:comment>http://www.blogjava.net/lukewange-hit1983/comments/222241.html</wfw:comment><comments>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222241.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lukewange-hit1983/comments/commentRss/222241.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lukewange-hit1983/services/trackbacks/222241.html</trackback:ping><description><![CDATA[摩托罗拉的部分手机（a1200,e60等），不支持gb2312编码。曾经给我造成了不少麻烦。现在，大家可以分享解决这个问题的一些经验。<br />
<br />
关于gb2312,unicode,utf-8的一些资料，大家请自行搜索。一下列举几个比较好的资源网址。<br />
<a href="http://baike.baidu.com/view/25492.htm">http://baike.baidu.com/view/25492.htm</a><br />
<a href="http://www.utf.com.cn/article/s45">http://www.utf.com.cn/article/s45</a><br />
<a href="http://www.utf.com.cn/article/s74">http://www.utf.com.cn/article/s74</a><br />
<a href="http://www.haiyan.com/steelk/navigator/ref/gb2312/gbindex.htm">http://www.haiyan.com/steelk/navigator/ref/gb2312/gbindex.htm</a><br />
<br />
要点：<br />
1,gb2312于unicode或者utf-8之间并不存在直接的映射关系。所以我们只能通过查表法来进行转换。<br />
2,utf-8是unicode用于网络传输的一种形式，它与unicode之间是可以通过运算来进行转换的。<br />
3,j2me环境使用的都是utf-8编码，但是请注意，j2me中的utf-8编码比较特殊，在整个编码前面对了两个字节，用于存放字符串的长度。<br />
<br />
过程：<br />
1，制作映射表gb2312-unicode，应为汉字的unicode比utf-8要小，这样做出的表也会小一些，而且对于unicode的可扩展性也强一些。<br />
2，先将gb2312编码串通过查表，转换为unicode。<br />
3，然后通过运算，将unicode转换为utf-8,以便在j2me环境下使用。<br />
<br />
我修改了Herong Yang大侠的一个映射表生成函数，原文请参考<a href="http://www.herongyang.com/gb2312/gb2312_unicode.html">http://www.herongyang.com/gb2312/gb2312_unicode.html</a><br />
它的作用是生成一个二进制的gb2312到unicode的查找表，它按照gb2312的分区，分块特性，将其对应的unicode按顺序存入指定的位置。<br />
这样我们只需要根据gb2312的编码，计算出索引就可以获取编码对应的unicode了。<br />
由于是修改的代码，没脸贴出来，大家有需求可以直接参考Herong Yang的文章，然后根据自己需求修改并生成自己的映射表。<br />
<br />
这里我把自己这个转换表文件以及访问代码公开。<br />
<a href="http://download.csdn.net/source/263609">http://download.csdn.net/source/263609</a><br />
转帖请注明。这是个傻瓜化的代码，在java中给它gb2312的byte数组，它就给你构造出字符串。<br />
用在不支持gb2312的手机上非常方便。这个转换表的大小是15228byte，对j2me来说还是可以接受的。<br />
<br />
如果有朋友需要沟通，可以发邮件到hunhun1981@hotmail.com<br />
<br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;java.io.InputStream;<br />
&nbsp;<br />
</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;HGB2312&nbsp;{<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[]&nbsp;map&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[</span><span style="color: #000000;">15228</span><span style="color: #000000;">];<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[]&nbsp;buffer;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;index;<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;HGB2312()&nbsp;</span><span style="color: #0000ff;">throws</span><span style="color: #000000;">&nbsp;Exception&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InputStream&nbsp;is&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;getClass().getResourceAsStream(</span><span style="color: #000000;">"</span><span style="color: #000000;">/gb2u.dat</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;is.read(map);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;is.close();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;String&nbsp;gb2utf8(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[]&nbsp;gb)&nbsp;</span><span style="color: #0000ff;">throws</span><span style="color: #000000;">&nbsp;Exception&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[gb.length&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;gb.length&nbsp;</span><span style="color: #000000;">/</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">2</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">3</span><span style="color: #000000;">];<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;index&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;c,&nbsp;h,&nbsp;l,&nbsp;ind;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;i&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;&nbsp;i&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;gb.length;)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(gb[i]&nbsp;</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fillBuffer(gb[i</span><span style="color: #000000;">++</span><span style="color: #000000;">]);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">256</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;gb[i</span><span style="color: #000000;">++</span><span style="color: #000000;">];<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;l&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">256</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;gb[i</span><span style="color: #000000;">++</span><span style="color: #000000;">];<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;h&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0xA0</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;l&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;l&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0xA0</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(h&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">9</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ind&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(h&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">94</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;l)&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(byte2Int(map[ind])&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">8</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;byte2Int(map[ind&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">]));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fillBuffer(c);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(h&nbsp;</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">9</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;h&nbsp;</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">14</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fillBuffer(</span><span style="color: #000000;">0</span><span style="color: #000000;">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(h&nbsp;</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">14</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h&nbsp;</span><span style="color: #000000;">-=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">6</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ind&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(h&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">94</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;l)&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(byte2Int(map[ind])&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">8</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;byte2Int(map[ind&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">]));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fillBuffer(c);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fillBuffer(</span><span style="color: #000000;">0</span><span style="color: #000000;">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;ind&nbsp;=&nbsp;index&nbsp;-&nbsp;2;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;h&nbsp;=&nbsp;(byte)&nbsp;((ind&nbsp;&gt;&gt;&nbsp;8)&nbsp;&amp;&nbsp;0x7F);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;l&nbsp;=&nbsp;(byte)&nbsp;(ind&nbsp;&amp;&nbsp;0xFF);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;buffer[0]&nbsp;=&nbsp;h;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;buffer[1]&nbsp;=&nbsp;l;</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;String(buffer,&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">,&nbsp;index,&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">UTF-8</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;fillBuffer(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;value)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(value&nbsp;</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0x0000007F</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer[index</span><span style="color: #000000;">++</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;value;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(value&nbsp;</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0x00000080</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;value&nbsp;</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0x000007FF</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">&nbsp;b1&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;(</span><span style="color: #000000;">0x60</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;(value&nbsp;</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">6</span><span style="color: #000000;">));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">&nbsp;b2&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;(</span><span style="color: #000000;">0x80</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;(value&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0x3F</span><span style="color: #000000;">));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer[index</span><span style="color: #000000;">++</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;b1;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer[index</span><span style="color: #000000;">++</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;b2;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(value&nbsp;</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0x00000800</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;value&nbsp;</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0x0000FFFF</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">&nbsp;b1&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;(</span><span style="color: #000000;">0xE0</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;(value&nbsp;</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">12</span><span style="color: #000000;">));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">&nbsp;b2&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;(</span><span style="color: #000000;">0x80</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;((value&nbsp;</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">6</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0x3F</span><span style="color: #000000;">));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">&nbsp;b3&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;(</span><span style="color: #000000;">0x80</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;(value&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0x3F</span><span style="color: #000000;">));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer[index</span><span style="color: #000000;">++</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;b1;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer[index</span><span style="color: #000000;">++</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;b2;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer[index</span><span style="color: #000000;">++</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;b3;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(value&nbsp;</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0x00010000</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;value&nbsp;</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0x001FFFFF</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">&nbsp;b1&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;(</span><span style="color: #000000;">0x1E</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;(value&nbsp;</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">18</span><span style="color: #000000;">));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">&nbsp;b2&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;(</span><span style="color: #000000;">0x80</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;((value&nbsp;</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">12</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0x3F</span><span style="color: #000000;">));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">&nbsp;b3&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;(</span><span style="color: #000000;">0x80</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;((value&nbsp;</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">6</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0x3F</span><span style="color: #000000;">));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">&nbsp;b4&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;(</span><span style="color: #000000;">0x80</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;(value&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0x3F</span><span style="color: #000000;">));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer[index</span><span style="color: #000000;">++</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;b1;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer[index</span><span style="color: #000000;">++</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;b2;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer[index</span><span style="color: #000000;">++</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;b3;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer[index</span><span style="color: #000000;">++</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;b4;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(value&nbsp;</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0x00200000</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;value&nbsp;</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0x03FFFFFF</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">&nbsp;b1&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;(</span><span style="color: #000000;">0x3E</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;(value&nbsp;</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">24</span><span style="color: #000000;">));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">&nbsp;b2&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;(</span><span style="color: #000000;">0x80</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;((value&nbsp;</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">18</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0x3F</span><span style="color: #000000;">));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">&nbsp;b3&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;(</span><span style="color: #000000;">0x80</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;((value&nbsp;</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">12</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0x3F</span><span style="color: #000000;">));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">&nbsp;b4&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;(</span><span style="color: #000000;">0x80</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;((value&nbsp;</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">6</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0x3F</span><span style="color: #000000;">));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">&nbsp;b5&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;(</span><span style="color: #000000;">0x80</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;(value&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0x3F</span><span style="color: #000000;">));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer[index</span><span style="color: #000000;">++</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;b1;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer[index</span><span style="color: #000000;">++</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;b2;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer[index</span><span style="color: #000000;">++</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;b3;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer[index</span><span style="color: #000000;">++</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;b4;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer[index</span><span style="color: #000000;">++</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;b5;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(value&nbsp;</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0x04000000</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;value&nbsp;</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0x7FFFFFFF</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">&nbsp;b1&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;(</span><span style="color: #000000;">0x7E</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;(value&nbsp;</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">30</span><span style="color: #000000;">));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">&nbsp;b2&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;(</span><span style="color: #000000;">0x80</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;((value&nbsp;</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">24</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0x3F</span><span style="color: #000000;">));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">&nbsp;b3&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;(</span><span style="color: #000000;">0x80</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;((value&nbsp;</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">18</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0x3F</span><span style="color: #000000;">));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">&nbsp;b4&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;(</span><span style="color: #000000;">0x80</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;((value&nbsp;</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">12</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0x3F</span><span style="color: #000000;">));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">&nbsp;b5&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;(</span><span style="color: #000000;">0x80</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;((value&nbsp;</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">6</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0x3F</span><span style="color: #000000;">));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">&nbsp;b6&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;(</span><span style="color: #000000;">0x80</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;(value&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0x3F</span><span style="color: #000000;">));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer[index</span><span style="color: #000000;">++</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;b1;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer[index</span><span style="color: #000000;">++</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;b2;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer[index</span><span style="color: #000000;">++</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;b3;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer[index</span><span style="color: #000000;">++</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;b4;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer[index</span><span style="color: #000000;">++</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;b5;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer[index</span><span style="color: #000000;">++</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;b6;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;byte2Int(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">&nbsp;b)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(b&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">256</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;b;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;b;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}<br />
<br />
</span></div>
<img src ="http://www.blogjava.net/lukewange-hit1983/aggbug/222241.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lukewange-hit1983/" target="_blank">LukeW</a> 2008-08-15 14:47 <a href="http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222241.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>修改png图的调色板</title><link>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222235.html</link><dc:creator>LukeW</dc:creator><author>LukeW</author><pubDate>Fri, 15 Aug 2008 06:34:00 GMT</pubDate><guid>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222235.html</guid><wfw:comment>http://www.blogjava.net/lukewange-hit1983/comments/222235.html</wfw:comment><comments>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222235.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lukewange-hit1983/comments/commentRss/222235.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lukewange-hit1983/services/trackbacks/222235.html</trackback:ping><description><![CDATA[今天在硬盘上挖出这个存放了几年的代码。又回忆起3年前的那个j2me手机游戏程序员&#8230;&#8230;<br />
<br />
这个算法是参考一位高人的文章，直接读取并修改png格式图片的调色板，然后生成新的调色板替代原来的。<br />
这样可以实现游戏中常见的变色效果，可以解决游戏容量有限，不能存放太多精灵图片的问题。<br />
<br />
具体过程其实并不复杂，大家可以先搜索资料，先看看png图片的格式定义。这个算法正是找到调色板区，根据原有格式修改之后，生成新的crc校验码，然后替换原来的调色板。这样就可以用一个png图片，创建多个变色副本。<br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;PalettedImage&nbsp;{<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;Image&nbsp;getPalettedImage(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[]&nbsp;data,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">[]&nbsp;originalColors,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">[]&nbsp;palettedColors)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[]&nbsp;tempData&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[data.length];<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.arraycopy(data,&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">,&nbsp;tempData,&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">,&nbsp;data.length);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image&nbsp;img&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">null</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">[]&nbsp;parameter&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;{&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">,&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">,&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">&nbsp;};<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;analyze(tempData,&nbsp;parameter);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;i&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;&nbsp;i&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;originalColors.length;&nbsp;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;replaceColor(tempData,&nbsp;parameter,&nbsp;originalColors[i],<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;palettedColors[i]);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fillData(tempData,&nbsp;parameter);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">try</span><span style="color: #000000;">&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;img&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;Image.createImage(tempData,&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">,&nbsp;data.length);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">catch</span><span style="color: #000000;">&nbsp;(Exception&nbsp;e)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(</span><span style="color: #000000;">"</span><span style="color: #000000;">getPalettedImage&nbsp;&nbsp;&amp;&amp;&nbsp;&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;e.toString());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;img;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;analyze(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[]&nbsp;data,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">[]&nbsp;para)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;offset&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">8</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;chunkLen&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">&nbsp;(data[offset&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">4</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0x50</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">||</span><span style="color: #000000;">&nbsp;data[offset&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">5</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0x4c</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">||</span><span style="color: #000000;">&nbsp;data[offset&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">6</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0x54</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">||</span><span style="color: #000000;">&nbsp;data[offset&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">7</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0x45</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chunkLen&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;readInt(data,&nbsp;offset);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;offset&nbsp;</span><span style="color: #000000;">+=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #000000;">4</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">4</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;chunkLen&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">4</span><span style="color: #000000;">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chunkLen&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;readInt(data,&nbsp;offset);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;para[</span><span style="color: #000000;">2</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;chunkLen&nbsp;</span><span style="color: #000000;">/</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">3</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;para[</span><span style="color: #000000;">0</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;offset&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">8</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;para[</span><span style="color: #000000;">1</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;offset&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">8</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;chunkLen;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;readInt(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[]&nbsp;data,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;offset)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;((data[offset]&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0xFF</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">24</span><span style="color: #000000;">)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;((data[offset&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0xFF</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">16</span><span style="color: #000000;">)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;((data[offset&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">2</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0xFF</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">8</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;(data[offset&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">3</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0xFF</span><span style="color: #000000;">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;replaceColor(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[]&nbsp;data,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">[]&nbsp;para,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;oldColor,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;newColor)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">&nbsp;rr&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;((oldColor&nbsp;</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">16</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0xff</span><span style="color: #000000;">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">&nbsp;gg&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;((oldColor&nbsp;</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">8</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0xff</span><span style="color: #000000;">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">&nbsp;bb&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;(oldColor&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0xff</span><span style="color: #000000;">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;i&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">,&nbsp;offset&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;para[</span><span style="color: #000000;">0</span><span style="color: #000000;">],&nbsp;temp&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;&nbsp;i&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;para[</span><span style="color: #000000;">2</span><span style="color: #000000;">];&nbsp;i</span><span style="color: #000000;">++</span><span style="color: #000000;">,&nbsp;offset&nbsp;</span><span style="color: #000000;">+=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">3</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(rr&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;data[offset]&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;gg&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;data[offset&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;bb&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;data[offset&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">2</span><span style="color: #000000;">])&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data[offset]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;((newColor&nbsp;</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">16</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0xff</span><span style="color: #000000;">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data[offset&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;((newColor&nbsp;</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">8</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0xff</span><span style="color: #000000;">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data[offset&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">2</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;(newColor&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0xff</span><span style="color: #000000;">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;fillData(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[]&nbsp;data,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">[]&nbsp;para)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;checksum&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;update_crc(data,&nbsp;para[</span><span style="color: #000000;">0</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">4</span><span style="color: #000000;">,&nbsp;para[</span><span style="color: #000000;">2</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">3</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">4</span><span style="color: #000000;">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data[para[</span><span style="color: #000000;">1</span><span style="color: #000000;">]]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;((checksum&nbsp;</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">24</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0xff</span><span style="color: #000000;">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data[para[</span><span style="color: #000000;">1</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;((checksum&nbsp;</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">16</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0xff</span><span style="color: #000000;">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data[para[</span><span style="color: #000000;">1</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">2</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;((checksum&nbsp;</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">8</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0xff</span><span style="color: #000000;">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data[para[</span><span style="color: #000000;">1</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">3</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">)&nbsp;((checksum)&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0xff</span><span style="color: #000000;">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;update_crc(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[]&nbsp;buf,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;off,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;len)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;c&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0xffffffff</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;n,&nbsp;k;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;xx;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">[]&nbsp;crc_table&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">[</span><span style="color: #000000;">256</span><span style="color: #000000;">];<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;(n&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;&nbsp;n&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">256</span><span style="color: #000000;">;&nbsp;n</span><span style="color: #000000;">++</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xx&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;n;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;(k&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;&nbsp;k&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">8</span><span style="color: #000000;">;&nbsp;k</span><span style="color: #000000;">++</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;((xx&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xx&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0xedb88320</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">^</span><span style="color: #000000;">&nbsp;(xx&nbsp;</span><span style="color: #000000;">&gt;&gt;&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xx&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;xx&nbsp;</span><span style="color: #000000;">&gt;&gt;&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;crc_table[n]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;xx;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;(n&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;off;&nbsp;n&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;len&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;off;&nbsp;n</span><span style="color: #000000;">++</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;crc_table[(c&nbsp;</span><span style="color: #000000;">^</span><span style="color: #000000;">&nbsp;buf[n])&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0xff</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">^</span><span style="color: #000000;">&nbsp;(c&nbsp;</span><span style="color: #000000;">&gt;&gt;&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">8</span><span style="color: #000000;">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;(c&nbsp;</span><span style="color: #000000;">^</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0xffffffff</span><span style="color: #000000;">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;<br />
}<br />
<br />
</span></div>
<br />
接口就是getPalettedImage()函数，只需要输入原始图片的byte数组，以及需要替换颜色的颜色值还有目标颜色值就行了。因为可以同时替换多个颜色，所以输入参数是代表颜色的整形的数组。总之，要保证原始颜色与目标颜色一一对应就好了。方法简单实用。<br />
<br />
欢迎大家使用并留下宝贵的意见。当然，也可以修改一下这个函数，做一些特殊的效果。这里就不多说了。<br />
不过这个代码用处已经不大，因为现在的手机基本上都支持midp2.0所以可以使用更方便的方法替换颜色。<br />
<br />
总之，再次感谢这位已经被我忘掉名字的大侠，关键代码是他写的，我只是修改整理而已。
<img src ="http://www.blogjava.net/lukewange-hit1983/aggbug/222235.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lukewange-hit1983/" target="_blank">LukeW</a> 2008-08-15 14:34 <a href="http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222235.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Autostarting MIDlets in JP-7 phones using PushRegistry </title><link>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222182.html</link><dc:creator>LukeW</dc:creator><author>LukeW</author><pubDate>Fri, 15 Aug 2008 02:57:00 GMT</pubDate><guid>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222182.html</guid><wfw:comment>http://www.blogjava.net/lukewange-hit1983/comments/222182.html</wfw:comment><comments>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222182.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lukewange-hit1983/comments/commentRss/222182.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lukewange-hit1983/services/trackbacks/222182.html</trackback:ping><description><![CDATA[In Sony Ericsson Java Platform 7 (JP-7) phones, such as the K610 or
W850, a new push functionality is introduced. PushRegistry Alarm and
PushRegistry SMS are supported throughout the Java Platforms, with JP-4
adding PushRegistry CBS and now JP-7 includes PushRegistry auto start.<br />
<br />
Note: the auto start functionality is not supported by the first
released K610 and K800 JP-7 phones. Please use the phone's upgrade
service to ensure that you are using the latest firmware version.<br />
<br />
The auto start functionality can be set static by including it in the
.jad file or it can be set dynamically in the code. Two example MIDlets
are included as an example of this.<br />
<br />
<a href="/Files/lukewange-hit1983/autostart.zip" title="Download the source code and examples here&gt;&gt;">Download the source code and examples here&gt;&gt;</a><br />
<br />
To enable the auto start functionality in the .jad file include the line:<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">MIDlet-Push-&lt;n&gt;:&nbsp;&lt;ConnectionURL&gt;,&nbsp;&lt;MIDletClassName&gt;,&nbsp;&lt;AllowedSender&gt;</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">MIDlet</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">Push</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">:&nbsp;autostart:</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">:,&nbsp;AutoStartStatic,&nbsp;*</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);"><br />
</span></div>
<br />
To make the MIDlet auto start from the source code the following methods can be used to register and un-register the MIDlet.<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">Registers&nbsp;the&nbsp;pushRegistry</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;Register(){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;List&nbsp;of&nbsp;registered&nbsp;push&nbsp;connections.</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;connections[];<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;Check&nbsp;to&nbsp;see&nbsp;if&nbsp;the&nbsp;connection&nbsp;has&nbsp;been&nbsp;registered.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;This&nbsp;is&nbsp;a&nbsp;dynamic&nbsp;connection&nbsp;allocated&nbsp;on&nbsp;first<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;time&nbsp;execution&nbsp;of&nbsp;this&nbsp;MIDlet.</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;connections&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;PushRegistry.listConnections(</span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;(connections.length&nbsp;</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);">&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">Register&nbsp;so&nbsp;the&nbsp;MIDlet&nbsp;will&nbsp;wake&nbsp;up&nbsp;when&nbsp;phone&nbsp;is&nbsp;started.</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PushRegistry.registerConnection(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">autostart://:</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">AutoStartDyn</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sDisplayString&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">MIDlet&nbsp;is&nbsp;registered</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);">&nbsp;(Exception&nbsp;ex)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Exception:&nbsp;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;ex);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sDisplayString&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Fail:&nbsp;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;ex;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sDisplayString&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Already&nbsp;registered</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;displayForm.deleteAll();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;displayForm.append(sDisplayString);<br />
}<br />
&nbsp;<br />
</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">Unregisters&nbsp;the&nbsp;pushRegistry</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;Unregister(){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;(PushRegistry.unregisterConnection(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">autostart://:</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">The&nbsp;pushRegistry&nbsp;is&nbsp;unregistered</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sDisplayString&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">MIDlet&nbsp;is&nbsp;unregistered.</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">There&nbsp;is&nbsp;no&nbsp;pushRegistry&nbsp;to&nbsp;unregister</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sDisplayString&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">No&nbsp;MIDlet&nbsp;to&nbsp;unregister&nbsp;or&nbsp;failed&nbsp;to&nbsp;unregister</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;displayForm.deleteAll();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;displayForm.append(sDisplayString);<br />
}<br />
<br />
</span></div>
<br />
<br />
To find out if the MIDlet is started via pushRegistry or manually you
can inspect the connections registered by pushRegistry. Below is a
small example of how to handle pushRegistry autostart or manual startup.<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;handlePushActivation()&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String[]&nbsp;connections&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;PushRegistry.listConnections(</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;(connections&nbsp;</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">&nbsp;connections.length&nbsp;</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">&nbsp;connections.length;&nbsp;i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;(connections[i].startsWith(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">autostart</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">))&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Application&nbsp;autostarted</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">User&nbsp;started&nbsp;the&nbsp;application</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
</span></div>
<pre><code jive-plain=""></code></pre>
<img src ="http://www.blogjava.net/lukewange-hit1983/aggbug/222182.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lukewange-hit1983/" target="_blank">LukeW</a> 2008-08-15 10:57 <a href="http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222182.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Using simultaneous sounds</title><link>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222173.html</link><dc:creator>LukeW</dc:creator><author>LukeW</author><pubDate>Fri, 15 Aug 2008 02:34:00 GMT</pubDate><guid>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222173.html</guid><wfw:comment>http://www.blogjava.net/lukewange-hit1983/comments/222173.html</wfw:comment><comments>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222173.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lukewange-hit1983/comments/commentRss/222173.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lukewange-hit1983/services/trackbacks/222173.html</trackback:ping><description><![CDATA[The code sample below describes how to play two sounds at the same
time. This feature is supported by the Sony Ericsson JP-5 platform and
onwards.<br />
<br />
Only one wav file can be played simultaniously but several midi files
might be played at the same time. The number of existing players is
only limited by available memory, and you have the possibility to play
more than one player at the same time. The example below shows how to
do this using one midi file and one wav file.<br />
<br />
The code is straight-forward - just load the resource, create the player and start playing the file.<br />
<br />
Here's the sample:<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 0, 0);">InputStream&nbsp;is&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;getClass().getResourceAsStream(file);<br />
InputStream&nbsp;is1&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;getClass().getResourceAsStream(file1);<br />
<br />
player&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;Manager.createPlayer(is,&nbsp;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&nbsp;audio/midi</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />
player.setLoopCount(</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br />
player.prefetch();<br />
player.realize();<br />
<br />
player1&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;Manager.createPlayer(is1,&nbsp;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">audio/x-wav</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />
player1.setLoopCount(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br />
player1.prefetch();<br />
player1.realize();<br />
<br />
player.start();<br />
player1.start();<br />
<br />
</span></div>
<pre><code jive-plain=""><a href="/Files/lukewange-hit1983/Sound_Mixer.zip" title="example code">example code</a></code></pre>
<img src ="http://www.blogjava.net/lukewange-hit1983/aggbug/222173.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lukewange-hit1983/" target="_blank">LukeW</a> 2008-08-15 10:34 <a href="http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222173.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Serializing an Image </title><link>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222171.html</link><dc:creator>LukeW</dc:creator><author>LukeW</author><pubDate>Fri, 15 Aug 2008 02:29:00 GMT</pubDate><guid>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222171.html</guid><wfw:comment>http://www.blogjava.net/lukewange-hit1983/comments/222171.html</wfw:comment><comments>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222171.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lukewange-hit1983/comments/commentRss/222171.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lukewange-hit1983/services/trackbacks/222171.html</trackback:ping><description><![CDATA[Creating an image from an array of data is an easy task, but to create
a byte-array of data from an image is a little more complicated. But
it's required if you want to send a modified image to a server.<br />
<br />
To create a byte-array of data from an image, we can use the getRGB(..)
method in the image class in MIDP 2.0. From the getRGB method we get an
int-array of data containing all the ARGB values of each pixel in the
image. Integers in java are four bytes, so we need to split each int
value into four byte values.<br />
<br />
To mask out each of the bytes in the int we can use the 'AND &amp;'
operator and the 'shift-right &gt;&gt;' operator. Here's an example:<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;ARGB&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0xFFFFFFFF</span><span style="color: rgb(0, 0, 0);">;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;AARRGGBB&nbsp;</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;a&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;(ARGB&nbsp;</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0xFF000000</span><span style="color: rgb(0, 0, 0);">);&nbsp;<br />
</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;r&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;(ARGB&nbsp;</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0x00FF0000</span><span style="color: rgb(0, 0, 0);">);&nbsp;<br />
</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;g&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;(ARGB&nbsp;</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0x0000FF00</span><span style="color: rgb(0, 0, 0);">);&nbsp;<br />
</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;b&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;(ARGB&nbsp;</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0x000000FF</span><span style="color: rgb(0, 0, 0);">);<br />
&nbsp;<br />
</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;Here&nbsp;we&nbsp;move&nbsp;each&nbsp;bit&nbsp;to&nbsp;the&nbsp;right.</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">a&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;(a&nbsp;</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">24</span><span style="color: rgb(0, 0, 0);">);&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;a&nbsp;=&nbsp;0x000000FF&nbsp;</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">r&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;(r&nbsp;</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">16</span><span style="color: rgb(0, 0, 0);">);&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;r&nbsp;=&nbsp;0x000000FF&nbsp;</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">g&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;(g&nbsp;</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">8</span><span style="color: rgb(0, 0, 0);">);&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;g&nbsp;=&nbsp;0x000000FF</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">b&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;b;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;b&nbsp;=&nbsp;0x000000FF</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);"><br />
</span></div>
<br />
When we convert the integer to a byte, there are some problems since
there are only signed bytes in Java. A byte may contain values between
-128 and 127 and from our integer we'll have a value between 0 and 255.<br />
<br />
Here are some conversion examples:<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 0, 0);"><br />
Integer&nbsp;val&nbsp;</span><span style="color: rgb(0, 0, 0);">127</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">byte</span><span style="color: rgb(0, 0, 0);">&nbsp;val&nbsp;</span><span style="color: rgb(0, 0, 0);">127</span><span style="color: rgb(0, 0, 0);">.<br />
Integer&nbsp;val&nbsp;</span><span style="color: rgb(0, 0, 0);">128</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">byte</span><span style="color: rgb(0, 0, 0);">&nbsp;val&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">128</span><span style="color: rgb(0, 0, 0);">.<br />
Integer&nbsp;val&nbsp;</span><span style="color: rgb(0, 0, 0);">255</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">byte</span><span style="color: rgb(0, 0, 0);">&nbsp;val&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">.<br />
<br />
</span><span style="color: rgb(0, 0, 255);">byte</span><span style="color: rgb(0, 0, 0);">&nbsp;ba&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;(</span><span style="color: rgb(0, 0, 255);">byte</span><span style="color: rgb(0, 0, 0);">)(a);&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;if&nbsp;a=0x000000FF&nbsp;(255),&nbsp;then&nbsp;ba&nbsp;=&nbsp;-1</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 255);">byte</span><span style="color: rgb(0, 0, 0);">&nbsp;br&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;(</span><span style="color: rgb(0, 0, 255);">byte</span><span style="color: rgb(0, 0, 0);">)(r);&nbsp;<br />
</span><span style="color: rgb(0, 0, 255);">byte</span><span style="color: rgb(0, 0, 0);">&nbsp;bg&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;(</span><span style="color: rgb(0, 0, 255);">byte</span><span style="color: rgb(0, 0, 0);">)(g);&nbsp;<br />
</span><span style="color: rgb(0, 0, 255);">byte</span><span style="color: rgb(0, 0, 0);">&nbsp;bb&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;(</span><span style="color: rgb(0, 0, 255);">byte</span><span style="color: rgb(0, 0, 0);">)(b);<br />
<br />
</span></div>
<br />
We have to loop though each pixel in the image and get each pixel value
to our byte-array. When that's done, we can send the image to a server
where we can convert the byte-array back to a integer-array and then
show our picture.<br />
<br />
So, when we convert the byte back to an integer we have to check if the byte value is less than zero.<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;a,&nbsp;r,&nbsp;g,&nbsp;b;<br />
</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(ba</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">256</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">&nbsp;a;<br />
<br />
</span></div>
<br />
Now our new integer value will be between 0 and 255 and we just have to
use the 'shift-left &lt;&lt;' operator and add the values together to
get our new int-array.<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 0, 0);">a&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;(a&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">24</span><span style="color: rgb(0, 0, 0);">);<br />
r&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;(r&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">16</span><span style="color: rgb(0, 0, 0);">);<br />
g&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;(g&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">8</span><span style="color: rgb(0, 0, 0);">);<br />
b&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;(b);<br />
&nbsp;<br />
&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 0);">0xFF000000</span><span style="color: rgb(0, 0, 0);">&nbsp;(a)<br />
</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0x00FF0000</span><span style="color: rgb(0, 0, 0);">&nbsp;(r)<br />
</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0x0000FF00</span><span style="color: rgb(0, 0, 0);">&nbsp;(g)<br />
</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0x000000FF</span><span style="color: rgb(0, 0, 0);">&nbsp;(b)<br />
</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0xFFFFFFFF</span><span style="color: rgb(0, 0, 0);">&nbsp;(argb)<br />
&nbsp;<br />
</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;ARGB&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;a</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">r</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">g</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">b;<br />
</span></div>
<br />
After the integer-array is recreated we can use the createRGBImage(..) method in the Image class to create our image.<br />
<br />
Be aware that the byte-array is quite large as it contains all of the
ARGB values for each pixel. If an image is 100*60 px, where each pixel
is four bytes, the byte array will be 24kb.<br />
<a href="/Files/lukewange-hit1983/ImageByteArray.ZIP" title="example code">example code</a><br />
<img src ="http://www.blogjava.net/lukewange-hit1983/aggbug/222171.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lukewange-hit1983/" target="_blank">LukeW</a> 2008-08-15 10:29 <a href="http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222171.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Fade in and out images in MIDP 2.0</title><link>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222167.html</link><dc:creator>LukeW</dc:creator><author>LukeW</author><pubDate>Fri, 15 Aug 2008 02:18:00 GMT</pubDate><guid>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222167.html</guid><wfw:comment>http://www.blogjava.net/lukewange-hit1983/comments/222167.html</wfw:comment><comments>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222167.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lukewange-hit1983/comments/commentRss/222167.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lukewange-hit1983/services/trackbacks/222167.html</trackback:ping><description><![CDATA[This tip describes how to change the alpha value of an image to make it
appear blended. There's also an example MIDlet with source code.<br />
<br />
In MIDP 2.0 there's a new method in the Image class, <strong>getRGB(...)</strong>
that will get all Alpha, Red, Green, Blue (ARGB) values from the image
to an int array. We can use this method, and the resulting array, to
change the alpha value of the image.<br />
<br />
Integers in J2ME are four bytes, each pixel in the image is described
by the ARGB values where each color is one byte 0 to 255. If the alpha
value is 0, the corresponding pixel will be transparent, if the alpha
is 255, the pixel will be opaque.<br />
<br />
To get a specific color from the int array, it's possible to use the <strong>AND '&amp;'</strong> operator and to accomplish the blending effect we need to get the colors, without the alpha value, from the int array.<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 0, 0);">FF&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">11111111</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">255</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">0xFFFFFFFF</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">&nbsp;Alpha&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">255</span><span style="color: rgb(0, 0, 0);">,&nbsp;Red&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">255</span><span style="color: rgb(0, 0, 0);">&nbsp;Green&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">255</span><span style="color: rgb(0, 0, 0);">,&nbsp;Blue&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">255</span><span style="color: rgb(0, 0, 0);"><br />
(</span><span style="color: rgb(0, 0, 0);">0xFFFFFFFF</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0x00FFFFFF</span><span style="color: rgb(0, 0, 0);">)&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0x00FFFFFF</span><span style="color: rgb(0, 0, 0);"><br />
<br />
</span></div>
<br />
By doing this we'll only get the RGB colors from the int array, alpha equals zero.<br />
<br />
Now when we just have the RGB colors and alpha equals zero, we can just add our new alpha value.<br />
<br />
If we have the alpha value 255 and want to add this to our color, we need to use the shift left operator.<br />
<br />
To change:<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">00000000</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">00000000</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">00000000</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">11111111</span><span style="color: rgb(0, 0, 0);">)&nbsp;to&nbsp;<br />
(</span><span style="color: rgb(0, 0, 0);">11111111</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">00000000</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">00000000</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">00000000</span><span style="color: rgb(0, 0, 0);">)<br />
use&nbsp;the&nbsp;shift&nbsp;left&nbsp;</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">&nbsp;operator.<br />
(</span><span style="color: rgb(0, 0, 0);">0xFF</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">24</span><span style="color: rgb(0, 0, 0);">)&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0xFF000000</span><span style="color: rgb(0, 0, 0);">.<br />
<br />
</span></div>
<br />
With this knowledge we now can change the alpha value or do masking for specific colors.<br />
<br />
<ul>
    <li>Use the getRGB(...) method in the image class to get all the ARGB values from the image to a int array.</li>
    <li>Use the blend function below to change the alpha value for each value in the array.</li>
    <li>Use the createRGBImage(...) method in the image class to create a new image from the edited int array.</li>
</ul>
<br />
There are examples on how to use the getRGB and createRGBImage methods in the MIDlet below.<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;blend(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">[]&nbsp;raw,&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;alphaValue){<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;len&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;raw.length;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;Start&nbsp;loop&nbsp;through&nbsp;all&nbsp;the&nbsp;pixels&nbsp;in&nbsp;the&nbsp;image.</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;&nbsp;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">len;&nbsp;i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;a&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;color&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;(raw[i]&nbsp;</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0x00FFFFFF</span><span style="color: rgb(0, 0, 0);">);&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;get&nbsp;the&nbsp;color&nbsp;of&nbsp;the&nbsp;pixel.</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;alphaValue;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;set&nbsp;the&nbsp;alpha&nbsp;value&nbsp;we&nbsp;want&nbsp;to&nbsp;use&nbsp;0-255.</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;(a</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">24</span><span style="color: rgb(0, 0, 0);">);&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;left&nbsp;shift&nbsp;the&nbsp;alpha&nbsp;value&nbsp;24&nbsp;bits.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;if&nbsp;color&nbsp;=&nbsp;00000000&nbsp;11111111&nbsp;11111111&nbsp;00000000&nbsp;(0xFFFF00&nbsp;=&nbsp;Yellow)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;and&nbsp;alpha=&nbsp;01111111&nbsp;00000000&nbsp;00000000&nbsp;00000000<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;then&nbsp;c+a&nbsp;=&nbsp;01111111&nbsp;11111111&nbsp;11111111&nbsp;00000000<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;and&nbsp;the&nbsp;pixel&nbsp;will&nbsp;be&nbsp;blended.</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;color&nbsp;</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);">&nbsp;a;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raw[i]&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;color;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}<br />
<br />
</span></div>
<a href="/Files/lukewange-hit1983/fading.ZIP" title="example code">example code</a><br />
<img src ="http://www.blogjava.net/lukewange-hit1983/aggbug/222167.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lukewange-hit1983/" target="_blank">LukeW</a> 2008-08-15 10:18 <a href="http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222167.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Fast stream reading in Java</title><link>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222163.html</link><dc:creator>LukeW</dc:creator><author>LukeW</author><pubDate>Fri, 15 Aug 2008 02:11:00 GMT</pubDate><guid>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222163.html</guid><wfw:comment>http://www.blogjava.net/lukewange-hit1983/comments/222163.html</wfw:comment><comments>http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222163.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lukewange-hit1983/comments/commentRss/222163.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lukewange-hit1983/services/trackbacks/222163.html</trackback:ping><description><![CDATA[To increase the performance of your Java&#8482; application when reading from
an InputStream, there are a few key areas to look into. If possible,
don't make any reallocations of memory. Allocate the input buffer once.
Let the Java&#8482; VM do the bulk of the reading, i.e. read data in big
chunks. Some coding examples show a loop that reads data a small amount
at a time. Using a too small buffer is inefficient. A much better
technique is to use a relatively large buffer.<br />
<br />
If you know the maximum content length, the most optimal read would be a single line, like this:<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 0, 0);">len&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;instream.read(&nbsp;buf,&nbsp;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,&nbsp;MAX_BUFFER_SIZE&nbsp;);</span></div>
<br />
In this case we assume that the buffer has already been allocated with a size set to MAX_BUFFER_SIZE.<br />
<br />
If the content is of varying size, we have to make a tradeoff between
performance and memory usage. If you keep your buffer size just above
the average content length, then the number of reallocations of the
data buffer and the number of reads will be kept at a minimum. There is
no defined size of what a large buffer is but a good rule of thumb
might be to keep the buffer size at most about 0,5 to 1 MB.<br />
<img src ="http://www.blogjava.net/lukewange-hit1983/aggbug/222163.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lukewange-hit1983/" target="_blank">LukeW</a> 2008-08-15 10:11 <a href="http://www.blogjava.net/lukewange-hit1983/archive/2008/08/15/222163.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>