<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Barklund.org &#187; Java</title>
	<atom:link href="http://www.barklund.org/blog/category/programming/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.barklund.org/blog</link>
	<description>work smarter when building current web trends</description>
	<lastBuildDate>Wed, 26 May 2010 09:49:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Browser Snapshot Sharing &#8211; January 2010 Ideas</title>
		<link>http://www.barklund.org/blog/2010/01/27/browser-snapshort-sharing-january-2010-idea/</link>
		<comments>http://www.barklund.org/blog/2010/01/27/browser-snapshort-sharing-january-2010-idea/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 08:00:54 +0000</pubDate>
		<dc:creator>Barklund</dc:creator>
				<category><![CDATA[January 2010 Ideas]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.barklund.org/blog/?p=672</guid>
		<description><![CDATA[The twenty-seventh idea for my 365 social ideas is an idea which has spun off my &#8220;investigation&#8221; of the SnapABug service: create a bookmarklet, that when clicked let&#8217;s you mark a section of the current webpage you are viewing (in it&#8217;s current state etc.) and then snapshots this and uploads the image to your image [...]


Related posts:<ol><li><a href='http://www.barklund.org/blog/2010/01/24/private-comment-sharing-january-2010-idea/' rel='bookmark' title='Permanent Link: Private Comment Sharing &#8211; January 2010 Ideas'>Private Comment Sharing &#8211; January 2010 Ideas</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/02/offline-news-aggregator-january-2010-idea/' rel='bookmark' title='Permanent Link: Offline News Aggregator &#8211; January 2010 Ideas'>Offline News Aggregator &#8211; January 2010 Ideas</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/22/social-traffic-analyzer-january-2010-idea/' rel='bookmark' title='Permanent Link: Social Traffic Analyzer &#8211; January 2010 Ideas'>Social Traffic Analyzer &#8211; January 2010 Ideas</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>The twenty-seventh idea for my <a href="/blog/365ideas/" title="365 Social Ideas from Barklund.org">365 social ideas</a> is an idea which has spun off <a href="/blog/2009/10/14/how-snapabug-works/">my &#8220;investigation&#8221;</a> of the <a href="http://snapabug.com">SnapABug service</a>: create a bookmarklet, that when clicked let&#8217;s you mark a section of the current webpage you are viewing (in it&#8217;s current state etc.) and then snapshots this and uploads the image to your image sharing service of choice &#8211; popular choices being <a href="http://flickr.com">flickr</a> or more shoot-from-the-hip style services like <a href="http://tinypic.com">tinypic</a>.</p>
<p><span id="more-672"></span></p>
<h4 id="toc-why">Why?</h4>
<p>Sharing snapshots of what you see currently is difficult. It is a lot easier on a Mac than on Windows, though Windows 7 has made it easier, but sharing it on the web still requires that you go to some website, browse for the image locally and upload in order to obtain a shareable url. If you had a simple bookmarklet as explained above, this process would be much easier.</p>
<h4 id="toc-whats-next">What&#8217;s next?</h4>
<p>Do with this idea whatever you like &#8211; expand, implement, trash or forget. Just remember, that if you use it in anyway make sure to attribute me according to the Creative Commons Attribution 3.0 License, that all these <a href="/blog/365ideas" title="365 Social Ideas from Barklund.org">365 Social Ideas</a> are published under.</p>


<p>Related posts:<ol><li><a href='http://www.barklund.org/blog/2010/01/24/private-comment-sharing-january-2010-idea/' rel='bookmark' title='Permanent Link: Private Comment Sharing &#8211; January 2010 Ideas'>Private Comment Sharing &#8211; January 2010 Ideas</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/02/offline-news-aggregator-january-2010-idea/' rel='bookmark' title='Permanent Link: Offline News Aggregator &#8211; January 2010 Ideas'>Offline News Aggregator &#8211; January 2010 Ideas</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/22/social-traffic-analyzer-january-2010-idea/' rel='bookmark' title='Permanent Link: Social Traffic Analyzer &#8211; January 2010 Ideas'>Social Traffic Analyzer &#8211; January 2010 Ideas</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.barklund.org/blog/2010/01/27/browser-snapshort-sharing-january-2010-idea/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why signed applet trust is a stupid question (and why SnapABug is not to blame)</title>
		<link>http://www.barklund.org/blog/2009/10/19/signed-applet-trust-stupid/</link>
		<comments>http://www.barklund.org/blog/2009/10/19/signed-applet-trust-stupid/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 11:45:25 +0000</pubDate>
		<dc:creator>Barklund</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Trends]]></category>

		<guid isPermaLink="false">http://www.barklund.org/blog/?p=406</guid>
		<description><![CDATA[After having written about the inner workings of SnapABug, I have now looked a bit further into signed applets and their permission levels. And the conclusion is, that the trust question is stupid. SnapABug did the right thing, the only thing they could do. Untrusted (unsigned) code lives in a special sandbox and cannot use [...]


Related posts:<ol><li><a href='http://www.barklund.org/blog/2009/10/14/how-snapabug-works/' rel='bookmark' title='Permanent Link: How SnapABug works &#8211; and what they should do'>How SnapABug works &#8211; and what they should do</a></li>
<li><a href='http://www.barklund.org/blog/2009/06/06/wordpress-automatic-update-fails/' rel='bookmark' title='Permanent Link: If WordPress automatic update fails &#8220;silently&#8221;&#8230;'>If WordPress automatic update fails &#8220;silently&#8221;&#8230;</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/06/password-friendly-website-certificate-january-2010-idea/' rel='bookmark' title='Permanent Link: Password-friendly Website Certificate &#8211; January 2010 Ideas'>Password-friendly Website Certificate &#8211; January 2010 Ideas</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>After having written about the <a href="http://www.barklund.org/blog/2009/10/14/how-snapabug-works/">inner workings of SnapABug</a>, I have now looked a bit further into signed applets and their permission levels. And the conclusion is, that the trust question is stupid.</p>
<p>SnapABug did the right thing, the only thing they could do. Untrusted (unsigned) code <a href="http://java.sun.com/javase/6/docs/technotes/guides/deployment/deployment-guide/security.html#policy" title="Security in JavaSE 6 from sun.com">lives in a special sandbox</a> and cannot use functions outside of this sandbox. The end user can change how much this sandbox has access to, but it is not trivial and the normal user would never touch that with a pitch fork. <a href="http://java.sun.com/javase/6/docs/technotes/guides/plugin/developer_guide/rsa_deploying.html" title="Deploying RSA-Signed Applets in Java Plug-in from sun.com">Trusted code lives in another sandbox</a> with almost infinite possibilities. The end user can limit this sandbox too, but again almost no end user does that.</p>
<p><span id="more-406"></span></p>
<p>The problem? How is code deemed to be trusted code? Code is trusted, if the publisher is trusted, and the validity of the publisher is guaranteed through the digital signature. The signature is automatically verified by the Java client, and the end user is then asked to do the final step &#8211; the Java client asks the end user to trust the code:</p>
<blockquote><p>We know this code has been written by this person (even though we don&#8217;t know what the code does). We know this person/company is, who (s)he says (s)he is. You can therefore safely trust the authenticity of this person. Do you trust that this person has written safe code?</p></blockquote>
<p>First of all, this is a strange question to ask any end user. Secondly, that if the actual question was written as above, it would have been much better as it would assume that the person has written safe code, but would let the end user decide &#8211; instead the question is written in a form that almost suggests, that the end user tries to illegally gain access to your computer by asking:</p>
<blockquote><p>An applet from &#8220;domain&#8221; is requesting unrestricted access to your computer</p></blockquote>
<div id="attachment_407" class="wp-caption aligncenter" style="width: 494px"><img src="http://www.barklund.org/blog/wp-content/uploads/2009/10/Screen-shot-2009-10-15-at-12.29.52.png" alt="The SnapABug applet trust question" title="The SnapABug applet trust question" width="484" height="199" class="size-full wp-image-407" /><p class="wp-caption-text">The SnapABug applet trust question</p></div>
<p>And below that in smaller letters is says that the digital signature has been verified (whatever that means to Average Joe). I am aware, that this might be the more specific question asked by my Java client/browser combination, as Sun <a href="http://java.sun.com/javase/6/docs/technotes/guides/deployment/deployment-guide/security.html#dialogs">seems to indicate</a>, that the dialog looks different on other systems.</p>
<p>In any case: that question is stupid. Would the normal end user install programs, if the standard windows installation flow included the question:</p>
<blockquote><p>When installing this application, the application could potentially install spamware, adware, take over your computer or copy your banking information. Do you wish to proceed?</p></blockquote>
<p>Let&#8217;s just say that adoptions of small programs from small developers would be a lot lower (which might be a good thing to prevent all those installing spamware because they think they have a virus or silly things like that, but in the long run it would probably be a bad thing). Asking such a question is stupid. This question really should be re-phrased in a way to clearly indicate, that the user should assume that this is a safe, trusted application, but the user has the opportunity to deny access anyway.</p>
<p>Secondly, (trusted) applications should have a way of asking for access to only what they need. The digital signature is simply for verification of origin, but the actual access level needed should be separate from that. As I asked in my previous post: why should the SnapABug applet have access to files on my computer? And they don&#8217;t need it, in fact I&#8217;m pretty sure, that if they could avoid having that access, they would rather do that. But they can&#8217;t.</p>
<p>Thus SnapABug is not to blame &#8211; if anyone, Sun is.</p>


<p>Related posts:<ol><li><a href='http://www.barklund.org/blog/2009/10/14/how-snapabug-works/' rel='bookmark' title='Permanent Link: How SnapABug works &#8211; and what they should do'>How SnapABug works &#8211; and what they should do</a></li>
<li><a href='http://www.barklund.org/blog/2009/06/06/wordpress-automatic-update-fails/' rel='bookmark' title='Permanent Link: If WordPress automatic update fails &#8220;silently&#8221;&#8230;'>If WordPress automatic update fails &#8220;silently&#8221;&#8230;</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/06/password-friendly-website-certificate-january-2010-idea/' rel='bookmark' title='Permanent Link: Password-friendly Website Certificate &#8211; January 2010 Ideas'>Password-friendly Website Certificate &#8211; January 2010 Ideas</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.barklund.org/blog/2009/10/19/signed-applet-trust-stupid/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How SnapABug works &#8211; and what they should do</title>
		<link>http://www.barklund.org/blog/2009/10/14/how-snapabug-works/</link>
		<comments>http://www.barklund.org/blog/2009/10/14/how-snapabug-works/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 11:07:05 +0000</pubDate>
		<dc:creator>Barklund</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.barklund.org/blog/?p=390</guid>
		<description><![CDATA[I just saw the SnapABug website, and was quite impressed, until I actually tried to submit a bug. Unrestricted access to my computer, why would I grant that to an unknown applet? And why do you even require unrestricted access to all files etc. on my computer to create a screenshot? Well, I delved into [...]


Related posts:<ol><li><a href='http://www.barklund.org/blog/2009/10/19/signed-applet-trust-stupid/' rel='bookmark' title='Permanent Link: Why signed applet trust is a stupid question (and why SnapABug is not to blame)'>Why signed applet trust is a stupid question (and why SnapABug is not to blame)</a></li>
<li><a href='http://www.barklund.org/blog/2009/11/23/iphone-developer-boycott-in-the-works/' rel='bookmark' title='Permanent Link: The iPhone developer boycott in the works'>The iPhone developer boycott in the works</a></li>
<li><a href='http://www.barklund.org/blog/2009/07/17/uk-copyright-does-extend-to-digitization/' rel='bookmark' title='Permanent Link: Why UK copyright law does extend copyright to the digitization of public domain works'>Why UK copyright law does extend copyright to the digitization of public domain works</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I just saw the <a href="http://www.snapabug.com/" title="Official page">SnapABug website</a>, and was quite impressed, until I actually tried to submit a bug. Unrestricted access to my computer, why would I grant that to an unknown applet? And why do you even require unrestricted access to all files etc. on my computer to create a screenshot?</p>
<p>Well, I delved into the application and found the answers &#8211; they could of course have done with a more restrictive permission.</p>
<p><span id="more-390"></span></p>
<p>First I found <a href="http://www.snapabug.com/snapabug.js">the JavaScript file</a>. The interesting part here was to find, that they actually inserted a java-applet in the corner of the application (in the <code>startWebCapture</code>-function):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">startWebCapture<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    	document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'SnapABug_Applet'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'block'</span><span style="color: #339933;">;</span>
    	document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'SnapABug_Applet'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> 
	    <span style="color: #3366CC;">'&lt;applet style=&quot;position:absolute;background-position-x:0px;background-position-y:0px;display:inline;font-size:2px;width:300px;height:2px;&quot; name=&quot;SnapABug_Applet&quot; code=&quot;webCaptureApplet.WebCaptureApplet.class&quot; codebase=&quot;'</span> <span style="color: #339933;">+</span> baseURL <span style="color: #339933;">+</span> <span style="color: #3366CC;">'/&quot; archive=&quot;sWebCaptureApplet.jar&quot; width=&quot;200&quot; height=&quot;2&quot; mayscript&gt;'</span>
        <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;param name=&quot;h&quot; value=&quot;'</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getWindowHeight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;'</span>
        <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;param name=&quot;w&quot; value=&quot;'</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getWindowWidth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;'</span>
        <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;param name=&quot;c&quot; value=&quot;'</span> <span style="color: #339933;">+</span> caseId <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;'</span>
        <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;param name=&quot;d&quot; value=&quot;300&quot;&gt;'</span>
        <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/applet&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>This applet (<a href="http://www.snapabug.com/sWebCaptureApplet.jar">JAR file</a>) is passed the viewport size and a case ID (used for sending the screenshot directly to the server). The source for this applet is easily accessible using e.g. <a href="http://java.decompiler.free.fr/?q=jdgui#downloads" title="The JD Java Decompiler download page">JD</a>.</p>
<p>The interesting part here is how they actually find the offset of the viewport. Getting a screenshot of all the available screen devices is quite easily managed through the <code>GraphicsEnvironment</code> class iterating through all screen <code>GraphicsDevice</code>&#8216;s to make it work on multi-screen setups. But finding the browser-window on the screenshot bitmap is a bit tricky &#8211; the browser chrome can look in all different sorts of ways, the only thing that we can be sure of is, that the viewport is rectangular and has the size given to the applet by JavaScript. If we could find just one of the corners of the viewport&#8230; Well, they solve this in a very low-tech way by via the above JavaScript embedding the Java Applet in the top-left corner at 2*200px and then in the <code>paint(Graphics g)</code>-function of the applet draw a certain pattern in that area:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  ...
  <span style="color: #003399;">BigInteger</span> bi <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BigInteger</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;11010100110010110011&quot;</span>, <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  ...
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> paint<span style="color: #009900;">&#40;</span><span style="color: #003399;">Graphics</span> g<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;paint: drawing corner&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    drawCornerCode<span style="color: #009900;">&#40;</span>g<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  ...
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> drawCornerCode<span style="color: #009900;">&#40;</span><span style="color: #003399;">Graphics</span> g<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    g.<span style="color: #006633;">clearRect</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">100</span>, <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">bi</span>.<span style="color: #006633;">testBit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">19</span> <span style="color: #339933;">-</span> i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        g.<span style="color: #006633;">setColor</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Color</span>.<span style="color: #006633;">black</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        g.<span style="color: #006633;">setColor</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Color</span>.<span style="color: #006633;">white</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      g.<span style="color: #006633;">drawLine</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">*</span> <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">0</span>, i <span style="color: #339933;">*</span> <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      g.<span style="color: #006633;">drawLine</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">*</span> <span style="color: #cc66cc;">3</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">0</span>, i <span style="color: #339933;">*</span> <span style="color: #cc66cc;">3</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      g.<span style="color: #006633;">drawLine</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">*</span> <span style="color: #cc66cc;">3</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">0</span>, i <span style="color: #339933;">*</span> <span style="color: #cc66cc;">3</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>This draws a black and white striped pattern in the top left corner of the browser which functions somewhat like a bar code. Then, when JavaScript asks the applet to create a screenshot, the applet runs through every pixel of every screen and sees, if from that pixel the bar code follows:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">      <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span>j <span style="color: #339933;">&lt;</span> devices.<span style="color: #006633;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>detected<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>j<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">GraphicsDevice</span> device <span style="color: #339933;">=</span> devices<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">GraphicsConfiguration</span> config <span style="color: #339933;">=</span> device.<span style="color: #006633;">getDefaultConfiguration</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">Rectangle</span> bounds <span style="color: #339933;">=</span> config.<span style="color: #006633;">getBounds</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        ...
          <span style="color: #006633;">screenshot</span> <span style="color: #339933;">=</span> rbt.<span style="color: #006633;">createScreenCapture</span><span style="color: #009900;">&#40;</span>bounds<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        ...
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> y <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span>y <span style="color: #339933;">&lt;</span> screenshot.<span style="color: #006633;">getHeight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>detected<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>y<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> x <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span>x <span style="color: #339933;">&lt;</span> screenshot.<span style="color: #006633;">getWidth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">21</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>detected<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> x <span style="color: #339933;">+=</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span>
          <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>screenshot.<span style="color: #006633;">getRGB</span><span style="color: #009900;">&#40;</span>x, y<span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #003399;">Color</span>.<span style="color: #006633;">black</span>.<span style="color: #006633;">getRGB</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
              <span style="color: #000000; font-weight: bold;">continue</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
              <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">bi</span>.<span style="color: #006633;">testBit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">19</span> <span style="color: #339933;">-</span> i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>screenshot.<span style="color: #006633;">getRGB</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">+</span> i <span style="color: #339933;">*</span> <span style="color: #cc66cc;">3</span>, y<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #003399;">Color</span>.<span style="color: #006633;">black</span>.<span style="color: #006633;">getRGB</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span>
                <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">bi</span>.<span style="color: #006633;">testBit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">19</span> <span style="color: #339933;">-</span> i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>screenshot.<span style="color: #006633;">getRGB</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">+</span> i <span style="color: #339933;">*</span> <span style="color: #cc66cc;">3</span>, y<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #003399;">Color</span>.<span style="color: #006633;">white</span>.<span style="color: #006633;">getRGB</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
              <span style="color: #009900;">&#123;</span>
                detected <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
              <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                detected <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
              <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>detected<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
              <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;detected x: &quot;</span> <span style="color: #339933;">+</span> x <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; y: &quot;</span> <span style="color: #339933;">+</span> y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
              detectedX <span style="color: #339933;">=</span> x<span style="color: #339933;">;</span>
              detectedY <span style="color: #339933;">=</span> y<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
          <span style="color: #009900;">&#125;</span></pre></div></div>

<p>This <strong>is</strong> actually quite brilliant, but of course not a revolution &#8211; just a smart simple way of using what&#8217;s available.</p>
<p>What is the problem then? Unrestricted access is the problem! I never allow unrestricted access to any applet unless I can&#8217;t do without it and trust the publisher. For this tool, neither criteria is fulfilled. After having checked the source code, I have allowed the applet, but not before &#8211; unrestricted access is simply way more than they should ever have. They need access for two things:</p>
<ol>
<li><code>AWTPermission</code> &#8211; access to the screens (which also gives access to create frameless windows and access to the visible state of all other programs running).</li>
<li><code>SocketPermission</code> &#8211; access to cross-domain posting of the final image.</li>
</ol>
<p>Of these permissions, only the first one is really necessary. The latter could be solved by requiring implementers to have a script locally on their own domain, that could process submitted images or simply forward them.</p>
<p>If the applet only asked permission to these two &#8211; or even better only <code>AWTPermission</code> &#8211; it would be way more trustworthy. As <a href="http://java.sun.com/developer/onlineTraining/Programming/JDCBook/appA.html" title="Appendix A: Security and Permissions">Sun says</a> about the <code>AllPermission</code>:</p>
<blockquote><p>
<code>java.security.AllPermission</code> specifies all permissions in the system for all possible targets and actions. This permission should be used only during testing because it grants permission to run with all security restrictions disabled as if there were no security manager.
</p></blockquote>
<p>Only during testing! Never in deployed applets from an unknown company expecting major adoption across third-party websites. The permission, that I particularly don&#8217;t like any applet not needing it to have is <code>FilePermission</code>. They should not be able to read any files on my computer or write any &#8211; they don&#8217;t need it!</p>
<p><s>If it does get implemented on many sites, this jar file will then be very very interesting to phishers and scammers, as they could use this (if they replaced the file with their own) to infiltrate many computers with key-loggers, malware and anything else.</s> This is not the case, as the applet is digitally signed, so modifying would require re-signing it, which would require re-authorizing it by end users.</p>
<p>But nevertheless &#8211; a very interesting feedback mechanism, that definitely has potential.</p>
</p>
<p>Source-code published under fair use. All rights belong to <a href="http://www.timzon.com/">Timzon</a> and only published here as-is without any warranty, obligations or any claims of copyright on my behalf.</p>


<p>Related posts:<ol><li><a href='http://www.barklund.org/blog/2009/10/19/signed-applet-trust-stupid/' rel='bookmark' title='Permanent Link: Why signed applet trust is a stupid question (and why SnapABug is not to blame)'>Why signed applet trust is a stupid question (and why SnapABug is not to blame)</a></li>
<li><a href='http://www.barklund.org/blog/2009/11/23/iphone-developer-boycott-in-the-works/' rel='bookmark' title='Permanent Link: The iPhone developer boycott in the works'>The iPhone developer boycott in the works</a></li>
<li><a href='http://www.barklund.org/blog/2009/07/17/uk-copyright-does-extend-to-digitization/' rel='bookmark' title='Permanent Link: Why UK copyright law does extend copyright to the digitization of public domain works'>Why UK copyright law does extend copyright to the digitization of public domain works</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.barklund.org/blog/2009/10/14/how-snapabug-works/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>A UTF-8 decoder with ISO 8859-1 failover</title>
		<link>http://www.barklund.org/blog/2007/04/28/a-utf-8-decoder-with-iso-8859-1-failover/</link>
		<comments>http://www.barklund.org/blog/2007/04/28/a-utf-8-decoder-with-iso-8859-1-failover/#comments</comments>
		<pubDate>Sat, 28 Apr 2007 14:48:01 +0000</pubDate>
		<dc:creator>Barklund</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Wikipedia]]></category>

		<guid isPermaLink="false">http://www.barklund.org/blog/2007/04/28/a-utf-8-decoder-with-iso-8859-1-failover/</guid>
		<description><![CDATA[It took me quite a while, but I finally managed. On IRC, the Danish Wikipedia channel on freenode, we have a bot running (built on Linky again built on PircBot). This bot&#8217;s primary purpose is to extend socalled wikilinks. That is, when someone writes Someone wrote silly stuff in the [[USA]] article again the bot [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>It took me quite a while, but I finally managed.</p>
<p>On IRC, the <a href="http://da.wikipedia.org" title="Not as big as sv or no, but very interesting still">Danish Wikipedia</a> <a href="irc://irc.freenode.org/wikipedia-da" title="Always some admins if you need them">channel on freenode</a>, we have a bot running (built on <a href="http://djini.de/software/linky/" title="A simple BeanShell framework">Linky</a> again built on <a href="http://www.jibble.org/pircbot.php" title="More complex framework in pure java">PircBot</a>). This bot&#8217;s primary purpose is to extend socalled <em>wikilinks</em>. That is, when someone writes <code>Someone wrote silly stuff in the [[USA]] article again</code> the bot replies <code>http://da.wikipedia.org/wiki/USA</code> &#8211; it expands the bracket-style links to proper URL&#8217;s.</p>
<p>The problem was the character encodings. Some use UTF-8 in this channel and others use ISO 8859-1. And how can you make the bot expand links for the Danish term <code>[[KÃ¸dpÃ¥lÃ¦g]]</code> when written in either of the two encodings? The correct URL for this word is <a href="http://da.wikipedia.org/wiki/K%C3%B8dp%C3%A5l%C3%A6g"><code>K%C3%B8dp%C3%A5l%C3%A6g</code></a> The built-in UTF-8-decoder in Java will replace the &#8220;bad characters&#8221; with the Unicode replacement character U+FFFD, and thus the previous term written sent by a ISO 8859-1 client with Linky in UTF-8 mode would become <a href="http://da.wikipedia.org/wiki/K%EF%BF%BDdp%EF%BF%BDl%EF%BF%BDg"><code>K%EF%BF%BDdp%EF%BF%BDl%EF%BF%BDg</code></a>, and in the reverse situation with Linky in ISO 8859-1 mode, the UTF-8 clients messages would be interpreted to <a href="http://da.wikipedia.org/wiki/K%C3%83%C2%B8dp%C3%83%C2%A5l%C3%83%C2%A6g"><code>K%C3%83%C2%B8dp%C3%83%C2%A5l%C3%83%C2%A6g</code></a>. Both are very wrong.</p>
<p><span id="more-26"></span></p>
<p>The solution is to have Linky run in UTF-8 mode, but when incorrect byte sequences occur, don&#8217;t replace them with the (proper) replacement character, but instead translate them using ISO 8859-1. And this is exactly, what I have created. A jar-file with a <code>CharsetProvider</code> providing a <code>X-UTF-8-Failover</code>-charset that simply does this. Then I added this jar to the classpath when running Linky and set the charset of Linky to be <code>X-UTF-8-Failover</code> &#8211; and it works!</p>
<p>The hard parts of this were:</p>
<ol>
<li>How do I decode UTF-8 properly?</li>
<li>How do I determine bad sequences?</li>
<li>How do I decode these differently instead?</li>
<li>How do I create a charset that Java can use?</li>
<li>How do I link this so Linky will use it?</li>
</ol>
<p>Well, questions 1 and 2 I had some prior knowledge about, but reading specs was still required. Question 3 was very simple once I realised, that the characters in UTF-8 below U+0100 is exactly the same as in ISO 8859-1. Question 4 was answered partly by reading the documentation for <a href="https://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/spi/CharsetProvider.html" title="CharsetProvider (Java 2 Platform SE 5.0)">CharsetProvider</a> and partly by <a href="http://mindprod.com/jgloss/encoding.html" title="encoding : Java Glossary">this nice overview of Java and encodings</a>. The fifth question was a simple matter of classpath.</p>
<p>To get to the main part of the whole deal, the <a href="https://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/CharsetDecoder.html#decodeLoop(java.nio.ByteBuffer,%20java.nio.CharBuffer)" title="protected abstract CoderResult CharsetDecoder.decodeLoop(ByteBuffer in, CharBuffer out)">decodeLoop</a>-implementation for X-UTF-8-Failover looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">protected</span> CoderResult decodeLoop<span style="color: #009900;">&#40;</span>ByteBuffer in, CharBuffer out<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">int</span> inPos <span style="color: #339933;">=</span> in.<span style="color: #006633;">position</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>in.<span style="color: #006633;">hasRemaining</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">char</span> c<span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">byte</span> b1 <span style="color: #339933;">=</span> in.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">int</span> highNibble <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>b1 <span style="color: #339933;">&gt;&gt;</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> 0xF<span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>highNibble<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">:</span>
			<span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span>
			<span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span>
			<span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">:</span>
			<span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">:</span>
			<span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">:</span>
			<span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">:</span>
			<span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">:</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>out.<span style="color: #006633;">remaining</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
					<span style="color: #000000; font-weight: bold;">return</span> CoderResult.<span style="color: #006633;">OVERFLOW</span><span style="color: #339933;">;</span>
				out.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#41;</span> b1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				inPos <span style="color: #339933;">=</span> in.<span style="color: #006633;">position</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">case</span> 0xC<span style="color: #339933;">:</span>
			<span style="color: #000000; font-weight: bold;">case</span> 0xD<span style="color: #339933;">:</span>
				<span style="color: #000066; font-weight: bold;">byte</span> b2<span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>in.<span style="color: #006633;">remaining</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
					<span style="color: #000000; font-weight: bold;">return</span> CoderResult.<span style="color: #006633;">UNDERFLOW</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>out.<span style="color: #006633;">remaining</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
					<span style="color: #000000; font-weight: bold;">return</span> CoderResult.<span style="color: #006633;">OVERFLOW</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>isContinuation<span style="color: #009900;">&#40;</span>b2 <span style="color: #339933;">=</span> in.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #666666; font-style: italic;">// put second char back by rewinding, putting and re-rewinding</span>
					in.<span style="color: #006633;">position</span><span style="color: #009900;">&#40;</span>in.<span style="color: #006633;">position</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					in.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>b2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					in.<span style="color: #006633;">position</span><span style="color: #009900;">&#40;</span>in.<span style="color: #006633;">position</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #666666; font-style: italic;">// update last read legal position</span>
					inPos <span style="color: #339933;">=</span> in.<span style="color: #006633;">position</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #666666; font-style: italic;">// append byte directly used</span>
					out.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span>b1 <span style="color: #339933;">&amp;</span> 0xFF<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #666666; font-style: italic;">// break switch</span>
					<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				c <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>b1 <span style="color: #339933;">&amp;</span> 0x1F<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> <span style="color: #009900;">&#40;</span>b2 <span style="color: #339933;">&amp;</span> 0x3F<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #666666; font-style: italic;">// check that we had the shortest encoding</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>c <span style="color: #339933;">&lt;=</span> 0x7F<span style="color: #009900;">&#41;</span>
					<span style="color: #000000; font-weight: bold;">return</span> CoderResult.<span style="color: #006633;">malformedForLength</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				out.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				inPos <span style="color: #339933;">=</span> in.<span style="color: #006633;">position</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">case</span> 0xE<span style="color: #339933;">:</span>
				<span style="color: #000066; font-weight: bold;">byte</span> b3<span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>in.<span style="color: #006633;">remaining</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span>
					<span style="color: #000000; font-weight: bold;">return</span> CoderResult.<span style="color: #006633;">UNDERFLOW</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>out.<span style="color: #006633;">remaining</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
					<span style="color: #000000; font-weight: bold;">return</span> CoderResult.<span style="color: #006633;">OVERFLOW</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>isContinuation<span style="color: #009900;">&#40;</span>b2 <span style="color: #339933;">=</span> in.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #666666; font-style: italic;">// put second char back by rewinding, putting and re-rewinding</span>
					in.<span style="color: #006633;">position</span><span style="color: #009900;">&#40;</span>in.<span style="color: #006633;">position</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					in.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>b2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					in.<span style="color: #006633;">position</span><span style="color: #009900;">&#40;</span>in.<span style="color: #006633;">position</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #666666; font-style: italic;">// update last read legal position</span>
					inPos <span style="color: #339933;">=</span> in.<span style="color: #006633;">position</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #666666; font-style: italic;">// append byte directly used</span>
					out.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span>b1 <span style="color: #339933;">&amp;</span> 0xFF<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #666666; font-style: italic;">// break switch</span>
					<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>isContinuation<span style="color: #009900;">&#40;</span>b3 <span style="color: #339933;">=</span> in.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #666666; font-style: italic;">// put third char back by rewinding, putting and re-rewinding</span>
					in.<span style="color: #006633;">position</span><span style="color: #009900;">&#40;</span>in.<span style="color: #006633;">position</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					in.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>b3<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					in.<span style="color: #006633;">position</span><span style="color: #009900;">&#40;</span>in.<span style="color: #006633;">position</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #666666; font-style: italic;">// update last read legal position</span>
					inPos <span style="color: #339933;">=</span> in.<span style="color: #006633;">position</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #666666; font-style: italic;">// append first and second byte directly used</span>
					out.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span>b1 <span style="color: #339933;">&amp;</span> 0xFF<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					out.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span>b2 <span style="color: #339933;">&amp;</span> 0xFF<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #666666; font-style: italic;">// break switch</span>
					<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				c <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>b1 <span style="color: #339933;">&amp;</span> 0x0F<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #cc66cc;">12</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>b2 <span style="color: #339933;">&amp;</span> 0x3F<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> <span style="color: #009900;">&#40;</span>b3 <span style="color: #339933;">&amp;</span> 0x3F<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #666666; font-style: italic;">// check that we had the shortest encoding</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>c <span style="color: #339933;">&lt;=</span> 0x7FF<span style="color: #009900;">&#41;</span>
					<span style="color: #000000; font-weight: bold;">return</span> CoderResult.<span style="color: #006633;">malformedForLength</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				out.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				inPos <span style="color: #339933;">=</span> in.<span style="color: #006633;">position</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">default</span><span style="color: #339933;">:</span>
				<span style="color: #666666; font-style: italic;">// parse as latin 1</span>
				out.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span>b1 <span style="color: #339933;">&amp;</span> 0xFF<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				inPos <span style="color: #339933;">=</span> in.<span style="color: #006633;">position</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> CoderResult.<span style="color: #006633;">UNDERFLOW</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// In case we did a get(), then encountered an error, reset the</span>
		<span style="color: #666666; font-style: italic;">// position to before the error.  If there was no error, this</span>
		<span style="color: #666666; font-style: italic;">// will benignly reset the position to the value it already has.</span>
		in.<span style="color: #006633;">position</span><span style="color: #009900;">&#40;</span>inPos<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Complete source for this can be download <a href="/examples/x_utf8_failover_complete.jar" title="JAR with embedded source">here</a> under the regular <a href="/blog/licens" title="CC-BY-SA-NC">license</a>.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.barklund.org/blog/2007/04/28/a-utf-8-decoder-with-iso-8859-1-failover/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
