<?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; Wikipedia</title>
	<atom:link href="http://www.barklund.org/blog/category/general/wikipedia/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>Twikicatalyzer  &#8211; January 2010 Ideas</title>
		<link>http://www.barklund.org/blog/2010/01/05/twikicatalyzer-january-2010-idea/</link>
		<comments>http://www.barklund.org/blog/2010/01/05/twikicatalyzer-january-2010-idea/#comments</comments>
		<pubDate>Tue, 05 Jan 2010 18:00:54 +0000</pubDate>
		<dc:creator>Barklund</dc:creator>
				<category><![CDATA[API]]></category>
		<category><![CDATA[January 2010 Ideas]]></category>
		<category><![CDATA[Wikipedia]]></category>

		<guid isPermaLink="false">http://www.barklund.org/blog/?p=550</guid>
		<description><![CDATA[The fifth idea for my 365 Social Ideas is a Twitter- and Wikipedia-mashup idea (and some maps): Find out who&#8217;s talking about what on Twitter through Wikipedia categories. There are many sites for tracking Twitter trends , but they all require that you know what you&#8217;re looking for (or where you&#8217;re looking for it geographically). [...]


Related posts:<ol><li><a href='http://www.barklund.org/blog/2010/01/01/twitter-follow-organizer-january-2010-idea/' rel='bookmark' title='Permanent Link: Twitter Follow Organizer &#8211; January 2010 Ideas'>Twitter Follow Organizer &#8211; January 2010 Ideas</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/21/game-of-life-january-2010-idea/' rel='bookmark' title='Permanent Link: Game of Life &#8211; January 2010 Ideas'>Game of Life &#8211; January 2010 Ideas</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/19/twadio-january-2010-idea/' rel='bookmark' title='Permanent Link: Twadio &#8211; January 2010 Ideas'>Twadio &#8211; January 2010 Ideas</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>The fifth idea for my <a href="/blog/365ideas/" title="365 Social Ideas from Barklund.org">365 Social Ideas</a> is a <a href="http://twitter.com" title="Twitter">Twitter-</a> and <a href="http://en.wikipedia.org/wiki/" title="The English-languaged Wikipedia Homepage">Wikipedia-mashup</a> idea (and some maps): Find out who&#8217;s talking about what on Twitter through Wikipedia categories. There are many sites for tracking Twitter trends , but they all require that you know what you&#8217;re looking for (or where you&#8217;re looking for it geographically). But what if you simply wanted to know what movies people are tweeting about? Or what about bands, musicians, flowers, four-legged mammals, financial institutions, politicians from Guadelupe or any other arbitrarily broad or narrow category of items?</p>
<p><span id="more-550"></span></p>
<p>For that purpose, <a href="http://en.wikipedia.org/wiki/Wikipedia:Categories" title="Categories described on Wikipedia">Wikipedia categories</a> seem very useful and given the openness of the Wikipedia philosophy, it would actually be quite simple. The site could be as simple as a single input field with input suggestions based on the Wikipedia category system. For example you could input <a href="http://en.wikipedia.org/wiki/Category:1950s_in_film" title="1950s in film category on Wikipedia">&#8220;1950s in film&#8221;</a> and maybe even intersect it with another category like <a href="http://en.wikipedia.org/wiki/Category:Best_Picture_Academy_Award_winners" title="Best Picture Academy Award winners category on Wikipedia">&#8220;Best Picture Academy Award winners&#8221;</a> and the resulting list (including all sub-category entries as well of course) would then be looked up in recent tweets on Twitter and the results would be displayed in different ways &#8211; including a list of most popular items, time-based breakdowns, geographic breakdowns, related links, most dominant profiles etc.</p>
<p>The technology behind it is quite simple and Wikipedia category search and even intersection has been seen in many different applications before. But using it for creating lists of items in a certain category to lookup in a different medium all-together is a new in my book.</p>
<p>Oh, and yes, the name used in the headline is quite stupid, agreed, but with Twitter and Wikipedia involved, Twiki-something seems appropriate.</p>
<h4 id="toc-why">Why?</h4>
<p>Trending topics on Twitter are highly popular and the target of many services, lots of discussion and even some succesful spamming attempts. But the general trends are way to general to be interesting for analyzing what&#8217;s happening in a certain industry or area of interest. This application/service/mashup is a low-hanging fruit, and might carry some hype once deployed, but might not have a long-standing potential. I do however personally find it very interesting and believe that a lot of &#8220;social media experts&#8221;, trend researchers and marketeers will as well.</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/01/twitter-follow-organizer-january-2010-idea/' rel='bookmark' title='Permanent Link: Twitter Follow Organizer &#8211; January 2010 Ideas'>Twitter Follow Organizer &#8211; January 2010 Ideas</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/21/game-of-life-january-2010-idea/' rel='bookmark' title='Permanent Link: Game of Life &#8211; January 2010 Ideas'>Game of Life &#8211; January 2010 Ideas</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/19/twadio-january-2010-idea/' rel='bookmark' title='Permanent Link: Twadio &#8211; January 2010 Ideas'>Twadio &#8211; January 2010 Ideas</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.barklund.org/blog/2010/01/05/twikicatalyzer-january-2010-idea/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Why UK copyright law does extend copyright to the digitization of public domain works</title>
		<link>http://www.barklund.org/blog/2009/07/17/uk-copyright-does-extend-to-digitization/</link>
		<comments>http://www.barklund.org/blog/2009/07/17/uk-copyright-does-extend-to-digitization/#comments</comments>
		<pubDate>Fri, 17 Jul 2009 13:57:49 +0000</pubDate>
		<dc:creator>Barklund</dc:creator>
				<category><![CDATA[Online Rights]]></category>
		<category><![CDATA[Wikipedia]]></category>

		<guid isPermaLink="false">http://www.barklund.org/blog/?p=219</guid>
		<description><![CDATA[I&#8217;ve been reading up on the matter of the National Portrait Gallery in the United Kingdom and their threatening letters to the Wikimedia Foundation about the legal status of the digitised works created by said public institution. After reading up on UK copyright law, my judgment is unclear &#8211; but mostly in favor of the [...]


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/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>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been reading up on <a href="http://news.slashdot.org/story/09/07/17/085244/New-Developments-In-NPGWikipedia-Lawsuit-Threat?from=rss" title="New Developments in NPG/Wikimedia Lawsuit Threat from slashdot.org">the matter</a> of the <a href="http://www.npg.org.uk/" title="Official website of the National Portrait Gallery">National Portrait Gallery in the United Kingdom</a> and <a href="http://yro.slashdot.org/story/09/07/11/1239244/UKs-National-Portrait-Gallery-Threatens-To-Sue-Wikipedia-User" title="UK's National Portrait Gallery Threatens To Sue Wikipedia User from slashdot.org">their threatening letters</a> to the <a href="http://wikimediafoundation.org/wiki/Home" title="Home of the Wikimedia Foundation">Wikimedia Foundation</a> about the legal status of the digitised works created by said public institution.</p>
<p>After reading up on UK copyright law, my judgment is unclear &#8211; but mostly in favor of the NPG and thus against Wikimedia.</p>
<p><span id="more-219"></span></p>
<p>The CDPA of 1988 clearly states in <a href="http://www.opsi.gov.uk/acts/acts1988/ukpga_19880048_en_2#pt1-ch1-pb1-l1g1" title="Copyright, Designs and Patents Act 1988, Chapter 1, Paragraph 1">the very first paragraph</a>:</p>
<blockquote><ol>
<li>Copyright and copyright works</p>
<ol>
<li>Copyright is a property right which subsists in accordance with this Part in the following descriptions of work—</p>
<ol>
<li style="list-style-type:lower-latin"><strong>original</strong> literary, dramatic, musical or artistic works, </li>
<li style="list-style-type:lower-latin">[...]</li>
</ol>
</li>
</ol>
</li>
</ol>
</blockquote>
<p>To me, one crucial part is the word &#8220;original&#8221;. This removes the possibility of claiming, that merely taking a photograph of another work is copyrightable in itself. The law goes on to specify what each of the four here-described works are, and it is clear, that taking a photo is an artistic work (defined in §4 sec. 1.a), but it is not an <strong>original</strong> work (in my opinion), when it is a photograph of a painting.</p>
<p>But the manipulation <a href="http://en.wikipedia.org/wiki/Conservation-restoration" title="Conservation-restoration on the English Wikipedia">done by the conservationists when digitizing the photos</a> includes a lot of work, which is definitely craftsmanship, but if deemed as artistic craftsmanship, it <strong>is</strong> in itself covered by the CDPA in <a href="http://www.opsi.gov.uk/acts/acts1988/ukpga_19880048_en_2#pt1-ch1-pb2-l1g4" title="Copyright, Designs and Patents Act 1988, Chapter 1, Paragraph 4">§4 section 1.c</a>:</p>
<blockquote><ol>
<li style="list-style:none"/>
<li style="list-style:none"/>
<li style="list-style:none"/>
<li>Artistic works</p>
<ol>
<li>In this Part “artistic work” means—</p>
<ol>
<li style="list-style:none" />
<li style="list-style-type:lower-latin">[...]</li>
<li style="list-style-type:lower-latin">a work of artistic craftsmanship.</li>
</ol>
</li>
</ol>
</li>
</ol>
</blockquote>
<p>My conclusion is thus, that it is irrelevant whether it is a derivative work or not (non-original works are never copyrightable) &#8211; the matter of the case is, whether the work of conservationists is artistic craftsmanship or not. And I&#8217;m leaning towards a &#8220;yes&#8221;, but I&#8217;m pretty sure the case cannot be settled without a court ruling, as it is clearly a case of interpretation. You can argue that a conservationist is merely a scientist, not an artist &#8211; but even scientific papers and articles are subject to copyright, and thus I believe, that the work of a conservationist is subject to copyright given that it is artistic craftsmanship and/or original scientific work.</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/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>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.barklund.org/blog/2009/07/17/uk-copyright-does-extend-to-digitization/feed/</wfw:commentRss>
		<slash:comments>0</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>
