<?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>Simon Buckle&#039;s Weblog &#187; Uncategorized</title>
	<atom:link href="http://www.simonbuckle.com/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.simonbuckle.com</link>
	<description>Random thoughts for random people</description>
	<lastBuildDate>Tue, 06 Jul 2010 12:46:01 +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>Order, Order</title>
		<link>http://www.simonbuckle.com/2010/01/15/order-order/</link>
		<comments>http://www.simonbuckle.com/2010/01/15/order-order/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 15:48:04 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.simonbuckle.com/?p=436</guid>
		<description><![CDATA[This post is about how to impose order on instances of Java classes. The context of the post will be centred around reading in some text and then counting the number of times each word appears in the text. The output will then be sorted either alphabetically or in order of the number of times [...]]]></description>
			<content:encoded><![CDATA[<p>This post is about how to impose order on instances of Java classes. The context of the post will be centred around reading in some text and then counting the number of times each word appears in the text. The output will then be sorted either alphabetically or in order of the number of times each word appears etc. Links to the source code are provided if you want to take a look.</p>
<p>Some instances of classes have an implicit order &#8211; a natural ordering. For example, in Java, <code>String</code> objects are ordered lexicographically; <code>Integer</code> objects are ordered numerically. If you find you are writing a value class whose instances have an obvious natural order, you should consider implementing the <code>Comparable</code> interface:</p>
<pre>public interface Comparable {
  int compareTo(T t);
}</pre>
<p><br/>The <code>String</code> class is an example of a class that implements <code>Comparable</code>; instances of it are ordered lexicographically. If you choose to implement <code>Comparable</code>, your class will be able to take advantage of many of the generic algorithms and collections available in the Java APIs, assuming you need to of course! The following example reads in some text and counts the instances of each word:</p>
<pre>public class <a href="/files/WordCountTest.java">WordCountTest</a> {
   public static void main(String[] args) throws Exception {
	<a href="/files/WordReader.java">WordReader</a> reader = new WordReader(new InputStreamReader(System.in));
	Map wordMap = new TreeMap();
	String word;
	while ((word=reader.readWord()) != null) {
		Integer count = wordMap.containsKey(word) ? wordMap.get(word) : 0;
		wordMap.put(word, ++count);
        }
	// Print out using the natural order of the key
	for (Map.Entry entry : wordMap.entrySet()) {
		System.out.println(entry.getKey()+" : "+entry.getValue());
	}
   }
}</pre>
<p><br/>A <code>TreeMap</code> sorts entries based on the natural order of the key, in this case, a string, but what if you want to order your instances in an <em>unnatural</em> order? e.g. ordering integers in decreasing order from largest to smallest. Well, in Java, you would use a <code>Comparator</code>:</p>
<pre><code>public interface Comparator {
    int compare(T o1, T o2);
	boolean equals(Object obj);
}</code></pre>
<p><br/>Comparators also allow you to provide an ordering for objects that don&#8217;t have a natural ordering, e.g. classes that don&#8217;t implement <code>Comparable</code>. Let&#8217;s take a look at an example that uses a comparator &#8211; as defined in the Order class &#8211; that sorts the words based on the number of times they appear:</p>
<pre><code>public class <a href="/files/WordCountTest2.java">WordCountTest2</a> {
   public static void main(String[] args) throws Exception {
	WordReader reader = new WordReader(new InputStreamReader(System.in));
	Map wordMap = new HashMap();
	String word;
	while ((word=reader.readWord()) != null) {
		Integer count = wordMap.containsKey(word) ? wordMap.get(word) : 0;
		wordMap.put(word, ++count);
	}

	// Sort by greatest occurrence first
	Set entries = new TreeSet(<a href="/files/Order.java">Order</a>.INCREASING_COUNT_COMPARATOR);
	for (Map.Entry entry : wordMap.entrySet()) {
		entries.add(new WordCount(entry.getKey(), entry.getValue()));
	}

	// Print
	Iterator result = entries.iterator();
	while (result.hasNext()) {
		<a href="/files/WordCount.java">WordCount</a> e = result.next();
		System.out.println(e.getWord()+" "+e.getCount());
	}
  }
}</code></pre>
<p><br/>It&#8217;s also quite common to instantiate a <code>Comparator</code> as an anonymous class and pass it in to the constructor of a sorted collection.</p>
<p>It&#8217;s worth noting that in the compare method in the <code>Comparator</code> used in the example, we first compare the word counts and if they are equal, we then return the result of comparing the value of the words. This is essential because sorted collections in Java use the <code>compareTo</code> method &#8211; or in this case, the <code>compare</code> method of the <code>Comparator</code> &#8211; in place of <code>equals</code>. What this means is that if we were to compare just the counts and return zero because they are equal &#8211; even though the words may be different &#8211; the WordCount instance would NOT get added to the (sorted) collection if an instance already exists in the collection with the same count. There&#8217;s a whole section in Joshua Bloch&#8217;s book &#8211; Effective Java (2nd edition) &#8211; that discusses this in greater detail (Item 12) for those that are interested.</p>
<p>That&#8217;s it really. There&#8217;s not much to it but it is worth taking the time to understand the difference(s) between implementing <code>Comparable</code> and creating a custom <code>Comparator</code>. Happy sorting!</p>
<p>By the way, if anybody has any better/alternative ideas for how to implement this, let me know. For example, my initial implementation used a <code>TreeMap</code> but that only allows you to sort on the key and not on the value(s) so wanting to sort in order of greatest number of occurrences won&#8217;t work with this particular data structure. In the second example, after constructing the hash table, I then add each entry to a (sorted) set then print out each value. Is there a way of doing this and avoiding the second step? I can&#8217;t think of one but maybe I&#8217;m missing something <img src='http://www.simonbuckle.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonbuckle.com/2010/01/15/order-order/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reading List</title>
		<link>http://www.simonbuckle.com/2010/01/09/reading-list/</link>
		<comments>http://www.simonbuckle.com/2010/01/09/reading-list/#comments</comments>
		<pubDate>Sat, 09 Jan 2010 10:59:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.simonbuckle.com/?p=431</guid>
		<description><![CDATA[At the beginning of last year (2009) I planned to maintain a list of books that I had read during the course of the year. Of course, true to form, I didn&#8217;t! Anyway, this year I plan to do the same thing; however, unlike last year I have actually created a page for the list [...]]]></description>
			<content:encoded><![CDATA[<p>At the beginning of last year (2009) I planned to maintain a list of books that I had read during the course of the year. Of course, true to form, I didn&#8217;t! Anyway, this year I plan to do the same thing; however, unlike last year I have actually created a page for the <a href="http://www.simonbuckle.com/reading-list-2010/">list of books</a> that I have read so far in 2010. I&#8217;ll update it as I go along. If you have any suggestions for books that I should read, leave a comment. Happy reading!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonbuckle.com/2010/01/09/reading-list/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Invalid Certificate Error with the Fonera 2.0 Downloader</title>
		<link>http://www.simonbuckle.com/2009/12/17/invalid-certificate-error-with-the-fonera-2-0-downloader/</link>
		<comments>http://www.simonbuckle.com/2009/12/17/invalid-certificate-error-with-the-fonera-2-0-downloader/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 15:03:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.simonbuckle.com/?p=423</guid>
		<description><![CDATA[This is a brief note about how to fix an invalid certificate error when using the Fonera Downloader add-on for Firefox. I installed the latest version of the Fonera Downloader plugin (0.1.5) for Firefox (3.5.6) on my Mac but I kept getting an &#8220;invalid certifcate&#8221; error whenever I tried to use the downloader, which Firefox [...]]]></description>
			<content:encoded><![CDATA[<p>This is a brief note about how to fix an invalid certificate error when using the Fonera Downloader add-on for Firefox.</p>
<p>I installed the latest version of the Fonera Downloader plugin (0.1.5) for Firefox (3.5.6) on my Mac but I kept getting an &#8220;invalid certifcate&#8221; error whenever I tried to use the downloader, which Firefox helpfully kept reminding me about every 2 minutes even when I did press cancel! This <a href="http://blog.fonosfera.org/invalid-certificate-while-using-the-fonera-downloader-addon/">blog post</a> explains how to fix the problem but unfortunately it didn&#8217;t work for me &#8211; I used the (correct) WAN address for my Fonera as instructed in the blog post but to no avail. What did work was the following:</p>
<ol>
<li>Open up Firefox and go to https://fonera/</li>
<li>Click on &#8220;Add Exception&#8221; and follow the rest of the <a href="http://blog.fonosfera.org/invalid-certificate-while-using-the-fonera-downloader-addon/">instructions</a></li>
</ol>
<p>That&#8217;s it! No more annoying pop-up window. And now you can save files directly to your Fonera providing, of course, you remember to insert a USB thumb drive into the Fonera, which I forgot to do but that&#8217;s another story &#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonbuckle.com/2009/12/17/invalid-certificate-error-with-the-fonera-2-0-downloader/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New News</title>
		<link>http://www.simonbuckle.com/2009/10/06/new-news/</link>
		<comments>http://www.simonbuckle.com/2009/10/06/new-news/#comments</comments>
		<pubDate>Tue, 06 Oct 2009 11:28:09 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.simonbuckle.com/?p=413</guid>
		<description><![CDATA[While at the Over The Air conference just over a week ago, I got talking to a couple of people who were publicising the NHS web services (yes, they have web services). Anyway, I signed up for an account and decided to play around with the news API by building a very simple Mobile Safari [...]]]></description>
			<content:encoded><![CDATA[<p>While at the Over The Air conference just over a week ago, I got talking to a couple of people who were publicising the NHS web services (yes, they have web services). Anyway, I signed up for an account and decided to play around with the news API by building a very simple Mobile Safari application using Apple&#8217;s Dashcode tool.</p>
<p>If you have an iPhone or iPod Touch you can view it in Safari here: <a href="http://nhsdemo.webteq.eu/">http://nhsdemo.webteq.eu/</a></p>
<p>There were a couple of things I wanted to do &#8211; such as adding a topic filter &#8211; but due to the fact that Dashcode is rather annoying, I haven&#8217;t been able to figure out how to do want I wanted to do &#8230;. so I didn&#8217;t. It&#8217;s just a proof of concept anyway, although I&#8217;m not entirely sure what concept I was trying to prove. </p>
<p>It wasn&#8217;t as straight forward as it looks though. Due to the fact that XML HTTP requests are restricted to the domain that the page was initially loaded from, I had to write a proxy (in Java) to proxy the request through to the NHS web service &#8211; I had to do this anyway because you have to pass your username and password to the NHS web service(s) as URL parameters (very secure!) and I didn&#8217;t want to put them directly into Javascript for obvious reasons!</p>
<p>Anyway, as soon as I recover from my first encounter with Dashcode I intend to play around with some of the new HTML 5 features that Safari supports such as client side storage etc, and further explore the NHS web services. Until then you have no excuse for not finding out when the next wave of pig flu is going to bring the nation to its knees!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonbuckle.com/2009/10/06/new-news/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sieving Numbers</title>
		<link>http://www.simonbuckle.com/2009/09/09/sieving-numbers/</link>
		<comments>http://www.simonbuckle.com/2009/09/09/sieving-numbers/#comments</comments>
		<pubDate>Wed, 09 Sep 2009 11:10:09 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.simonbuckle.com/?p=401</guid>
		<description><![CDATA[Here&#8217;s a problem that you some times come across in problem sets and job interviews:   Q. Write a program to generate all the prime numbers up to N. The simplest algorithm I can think of is the Sieve of Eratosthenes. Here&#8217;s my attempt: public class PrimeSieve { public static void main(String[] args) { int [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a problem that you some times come across in problem sets and job interviews:  </p>
<p><em>Q. Write a program to generate all the prime numbers up to N.</em></p>
<p>The simplest algorithm I can think of is the <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">Sieve of Eratosthenes</a>.<br />
Here&#8217;s my attempt:</p>
<pre><code>
public class PrimeSieve {

     public static void main(String[] args) {
	int N = Integer.parseInt(args[0]);

	boolean[] isPrime = new boolean[N+1];
	Arrays.fill(isPrime, true);

	int max = (int)Math.sqrt(N);

	for (int i=2; i<=max; i++) {
	   if (isPrime[i]) {
	      // Remove all of the multiples of i
	      for (int j=2; i*j<=N; j++) {
		 isPrime[i*j] = false;
	      }
	    }
	}

	// List the primes
	for (int k=2; k<=N; k++) {
	    if (isPrime[k]) System.out.print(k+" ");
	}
     }
}
</code>
</pre>
<p>That's all. You can leave now.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonbuckle.com/2009/09/09/sieving-numbers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mobile Mentality</title>
		<link>http://www.simonbuckle.com/2009/08/10/mobile-mentality/</link>
		<comments>http://www.simonbuckle.com/2009/08/10/mobile-mentality/#comments</comments>
		<pubDate>Mon, 10 Aug 2009 11:53:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.simonbuckle.com/?p=395</guid>
		<description><![CDATA[During the course of last week I have been getting up to speed on the iPhone SDK; yes, I did eventually get around to buying a new MacBook Pro! The more I learn, the more doubts I have about developing for the iPhone. Nothing to do with the technology &#8211; although Objective-C is an interesting [...]]]></description>
			<content:encoded><![CDATA[<p>During the course of last week I have been getting up to speed on the iPhone SDK; yes, I did eventually get around to buying a new MacBook Pro! The more I learn, the more doubts I have about developing for the iPhone. Nothing to do with the technology &#8211; although Objective-C is an <em>interesting</em> extension to C &#8211; but everything to do with the (closed) platform, app store etc. For example, I discovered that in order to test my application on a <em>real</em> device I need to stump up $99 to get onto the developer program; the SDK only has an iPhone simulator. I have highlighted other concerns that I have in previous posts that become more <em>real</em> when you read posts such as <a href="http://factoryjoe.com/blog/2009/08/01/steve-jobs-hates-the-appstore/">this one</a>.</p>
<p>What particularly irks me is the fact that the price of apps are perpetually being driven down to zero! Sure, there have been <em>some</em> hits but these are exceptions to the rule; the rule being: you aren&#8217;t going to make any money off your app! If you are a developer (or a company) trying to make a living out of this and you sell your app for 59p, you work out how many copies of it you are going to have to sell in order to make a living off of it. The numbers don&#8217;t add up!</p>
<p>I came across a good example of this over the weekend. There is a website called <a href="http://www.bnonews.com/about">Breaking News Online</a> and they have an iPhone app that they charge $2 for and there is a recurring $1 a month subscription to receive breaking news via push notifications. You can read about it on ReadWriteWeb <a href="http://www.readwriteweb.com/archives/breaking_news_online_the_iphone_app_is_live_worth.php">here</a>. Anyway, if you look at the comments, some people are bitching about having to fork out $1 a month! What? &#8220;Is anything worth that?&#8221;, they cry! $1 a month buys you half a latte from Starbucks (depending on the size of course) yet even this seems to be too much for some people! The bottom line is this: if you are <em>in</em> the business of just selling applications for the iPhone then you are soon going to be going <em>out</em> of business! You can blame cheap users for that.</p>
<p>I see two primary ways of making money from mobile applications: you can offer a mobile version that supplements whatever it is you happen to be selling, e.g. Salesforce does CRM and they have an iPhone app but it&#8217;s <strong>not</strong> their main line of business; or you can write mobile applications <strong>for</strong> other companies. There are a number of apps that I have come across recently that have inline ads but your app really needs to become popular before you can start making any serious money from it.</p>
<p>Myself, I am focusing on both approaches. I have done Cocoa development in the past (on the desktop) so focusing on mobile right now is just another string to add to my bow. I hope to have something in the app store during the course of the next couple of <span style="text-decoration: line-through;">weeks</span> <span style="text-decoration: line-through;">months</span> years &#8230; assuming of course that it doesn&#8217;t get blocked. We&#8217;ll see!</p>
<p>Finally, if you do want an iPhone app building then let me know. Just don&#8217;t ask me to try and sell it for you!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonbuckle.com/2009/08/10/mobile-mentality/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Opportunity Knocks</title>
		<link>http://www.simonbuckle.com/2009/07/23/opportunity-knocks/</link>
		<comments>http://www.simonbuckle.com/2009/07/23/opportunity-knocks/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 13:24:36 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.simonbuckle.com/?p=393</guid>
		<description><![CDATA[What follows is a brief list (without much of an explanation I might add) of what I perceive to be gaps in the market. Opportunities if you prefer. Areas where the current state of the art sucks! Not good enough. Please fix. You&#8217;ll be making my life much easier, as well as countless others. You&#8217;ll [...]]]></description>
			<content:encoded><![CDATA[<p>What follows is a brief list (without much of an explanation I might add) of what I perceive to be gaps in the market. Opportunities if you prefer. Areas where the current state of the art sucks! Not good enough. Please fix. You&#8217;ll be making my life much easier, as well as countless others. You&#8217;ll probably make a few billion on the side too!</p>
<ol>
<li><strong>Create a search engine that works</strong> &#8211; Yahoo, Google, Bing etc. Err, no. I&#8217;m not suggesting it&#8217;s an easy problem to solve (it isn&#8217;t) but with some of the biggest companies in the search game you would think they&#8217;d be able to come up with something that didn&#8217;t involve me <em>still</em> having to type in hundreds of different combinations of words in the hope of finding what I&#8217;m looking for (and still not finding it!). I find myself having to do this all the time.</li>
<li><strong>Develop a decent desktop mail client</strong> &#8211; I currently use the Mail application on Mac OS X. It&#8217;s woeful! I can&#8217;t tag individual messages so they are easy to refer back to and I still can&#8217;t figure out how to mark all items (in the RSS feed reader) as being read. Windows users&#8217; have Outlook. Enough said. And the response, &#8220;But I use Gmail all the time &#8230;&#8221;, is not a valid one! I want something that works on my desktop.</li>
<li><strong>System for delivering targeted adverts</strong> &#8211; Firstly, I very rarely click on adverts. Period. Some people do, judging by the amount of revenue Google makes! Anyway, I have lost track of the number of times I have come across adverts on sites that have nothing to do at all with what the site is about. I once saw an advert for Crucial memory on some fashion site! It has been shown that if you have adverts that are relevant to the user, click-through rates are much higher. Now Google attempts to do this AdSense by looking at keywords in the page content but it doesn&#8217;t work well in my opinion. I used to have Google Ads on this blog but it kept showing adverts for belt buckles! I don&#8217;t tend to write about belt buckles much on this blog so not really relevant.</li>
</ol>
<p>Anyway, these are just a few ideas to get you started. Now go forth and conquer and let me know when you&#8217;re done.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonbuckle.com/2009/07/23/opportunity-knocks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flushing The Document Early</title>
		<link>http://www.simonbuckle.com/2009/06/26/flushing-the-document-early/</link>
		<comments>http://www.simonbuckle.com/2009/06/26/flushing-the-document-early/#comments</comments>
		<pubDate>Fri, 26 Jun 2009 15:22:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.simonbuckle.com/?p=373</guid>
		<description><![CDATA[This post is a note to myself and regards the Transfer-Encoding header field, defined in the HTTP spec. I was reminded of its use while reading Even Faster Web Sites. First, some assumptions for the example that follows. Let&#8217;s say that the header of your page contains some annoying Flash banner advert that is downloaded [...]]]></description>
			<content:encoded><![CDATA[<p>This post is a note to myself and regards the <em>Transfer-Encoding</em> <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.41">header</a> field, defined in the HTTP spec. I was reminded of its use while reading <a href="http://oreilly.com/catalog/9780596522308/">Even Faster Web Sites</a>.</p>
<p>First, some assumptions for the example that follows. Let&#8217;s say that the header of your page contains some annoying Flash banner advert that is downloaded from a different host and that the body of the page takes a few seconds to generate &#8211; in the example below I suspend the current thread for 10 seconds.</p>
<p>The page will be generated on the server and then served back to the browser. The browser will then parse the HTML and proceed to fetch the banner ad etc. In the meantime the user will be sitting there wondering what, if anything, is going on! So how can we present a more user-friendly page? Something that feels more responsive to the user. Enter the <em>Transfer-Encoding</em> header. By setting it to <em>chunked</em> we can serve the header part of the page &#8211; the first chunk &#8211; to the browser while the server works on generating the body of the page; in other words we don&#8217;t need to generate the page <strong>all in one go</strong> and then serve it up. The <em>Transfer-Encoding</em> header informs the browser that the content for the current page is going to come down the pipe in pieces (&#8220;chunks&#8221;) and not all at once. It also has the added benefit that as soon as the browser retrieves the first part of the page (the &#8220;header&#8221;) it can start to download the banner ad in parallel<sup>*</sup> while it waits for the remaining part of the page from the server. Overall the page should <strong>feel</strong> more responsive.</p>
<p>So, how to do it in a Java servlet. You would think you would just call the <em>setHeader</em> method on the servlet response object but you don&#8217;t &#8211; what were you thinking? Turns out it&#8217;s even easier than that! An example is given below:</p>
<pre><code>
void doGet(HttpServletRequest request,
           HttpServletResponse response)
   throws ServletException, IOException {

	response.setContentType("text/plain");
	PrintWriter out = response.getWriter();
	out.println("The start of the page.");
	<strong>out.flush();</strong>
	// Wait 10 seconds for no reason whatsoever
	try {
	     Thread.currentThread().sleep(10000);
	} catch (InterruptedException e) {
	     // Do nothing
	}
	out.println("The rest of the page ...");
}
</code></pre>
<p>Basically, if you write something to the output buffer and then call flush() that will automatically set the <em>Transfer-Encoding</em> header for you. If you remove the call to flush() then all the output will be buffered before it is sent back to the browser. If you fire up the example code in Tomcat (or something) and then look at it in a browser, the first line will be returned immediately followed 10 seconds later by the rest of the page; if you remove the call to flush() then the page content will be returned all at the same time after about 10 seconds or so.</p>
<p>End of note.</p>
<p>(*) Most browsers open up a limited number of connections to a given host. For example, Firefox 3 opens up a maximum of 6 connections at any one time to a given host.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonbuckle.com/2009/06/26/flushing-the-document-early/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>URL Shorteners</title>
		<link>http://www.simonbuckle.com/2009/06/24/url-shorteners/</link>
		<comments>http://www.simonbuckle.com/2009/06/24/url-shorteners/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 14:37:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.simonbuckle.com/?p=368</guid>
		<description><![CDATA[Yes, another post about url shorteners. Recently, apart from complaining about them, I have been thinking about how URL shortening services work; services such as bit.ly and tinyurl. Many of these services reduce a URL to a small string; typically a length of 3 to 6 characters. As a result, they can&#8217;t be simply hashing [...]]]></description>
			<content:encoded><![CDATA[<p>Yes, another post about url shorteners. Recently, apart from complaining about them, I have been thinking about how URL shortening services work; services such as bit.ly and tinyurl.</p>
<p>Many of these services reduce a URL to a small string; typically a length of 3 to 6 characters. As a result, they can&#8217;t be simply hashing the URL. For example, if you use MD5 to hash the domain name of this site you get (in hexadecimal): 4302e8ae08795f0c67c932338f516e2f. The resulting hash value is longer than the URL itself! Not very useful for a URL shortening service.</p>
<p>So how do they work? I still don&#8217;t know but here&#8217;s one approach that I took:</p>
<p>Let&#8217;s say we want to produce a code using characters from the following alphabet [a-zA-Z0-9]; that gives a total of 62 different alphanumeric characters. For a 5 character code there are 62*62*62*62*62 (=916,132,832) possible combinations. If we associate each code with a given URL &#8211; a simple one to one mapping &#8211; then that&#8217;s a lot of URLs! The key point is that, unlike a hash function, I don&#8217;t think the URL is used as input to determine what comes out of the other end; a &#8220;random&#8221; character code is generated and then is just stored with the URL so it can be retrieved with a simple table look-up.</p>
<p>I came up with a probabilistic approach to generating these &#8220;hash&#8221; codes. I say probabilistic as it just generates a code at random. If there is a collision, it just tries again and generates another one. So how likely are collisions to occur? Well according to the <a href="http://en.wikipedia.org/wiki/Birthday_paradox">birthday paradox</a> we should expect to see a collision after generating 2<sup>n/2</sup> items, or approx. every 2<sup>15</sup> items for a 5 character code using the example code, assuming, of course, that all generated codes are equally likely to occur. It&#8217;s an example, it will do!</p>
<p>You can look at the source code <a href="http://www.simonbuckle.com/URLShortener.java.txt">here</a>. In the example I use a bit vector to record what codes have already been generated; the bit vector is limited to representing 2<sup>31</sup>-1 different values therefore the example code is restricted to generating a maximum of 5 character codes; each character requires 6 bits. I&#8217;ll let you do the math as I have already done it <img src='http://www.simonbuckle.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>If you do run it you may have to increase the maximum heap size, e.g. -Xmx256m. I ran out of the heap space the first time I ran it using Eclipse!</p>
<p>It&#8217;s a first attempt so there is likely some room for improvement but it&#8217;s a start. Would be great to hear any alternative thoughts on how these things work.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonbuckle.com/2009/06/24/url-shorteners/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Alternative App Store?</title>
		<link>http://www.simonbuckle.com/2009/06/19/alternative-app-store/</link>
		<comments>http://www.simonbuckle.com/2009/06/19/alternative-app-store/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 09:03:26 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.simonbuckle.com/?p=365</guid>
		<description><![CDATA[I am about to embark* on developing an application for the iPhone, of which I am going to sell thousands of copies and then retire to the Caribbean (just like all iPhone apps right?) but I have a few concerns, primarily with Apple&#8217;s app store policy about what qualifies (and disqualifies) an application from being [...]]]></description>
			<content:encoded><![CDATA[<p>I am about to embark* on developing an application for the iPhone, of which I am going to sell thousands of copies and then retire to the Caribbean (<em>just like all iPhone apps right?</em>) but I have a few concerns, primarily with Apple&#8217;s app store policy about what qualifies (and disqualifies) an application from being sold &#8211; or given away &#8211; on the app store. Now I don&#8217;t know much about Apple&#8217;s policy but I have followed the various discussions about it in the &#8220;media&#8221; so I know about things such as how if your application is deemed to compete in some way with Apple then your app will be rejected etc but it doesn&#8217;t help when I keep reading articles like <a href="http://daringfireball.net/linked/2009/06/15/arment-wwdc-fu">this</a>. From a business perspective I don&#8217;t want to spend months developing something only to have Apple turn around and reject it!</p>
<p>This neatly segways into my next point: Why isn&#8217;t there an independent store selling applications for the iPhone? A place where all the misfit applications rejected by Apple can be sold on &#8211; think of it as a council estate for mobile phone apps. Maybe there is one; I have no idea. I can see <em>why</em> developers would want to sell their apps through the App store as it&#8217;s baked right into iTunes and it&#8217;s easy to pay for and put on your iPhone etc but surely there must be some other way to manage applications? I guess I&#8217;ll find out soon enough but if you have any useful advice in the meantime, please leave a comment.</p>
<p>* <em>when I say &#8220;about to embark&#8221; that&#8217;s actually dependent on dragging myself away from my keyboard and down to the Apple store to buy myself a new Macbook Pro; mine is getting a bit long in the tooth. As they have just released the latest versions and lowered the price I guess I don&#8217;t have any excuse not to get one.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonbuckle.com/2009/06/19/alternative-app-store/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
