Simon Buckle’s Weblog

Random thoughts from random people

Simon Buckle’s Weblog header image 1

Flushing The Document Early

June 26th, 2009 · Comments

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’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 – in the example below I suspend the current thread for 10 seconds.

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 Transfer-Encoding header. By setting it to chunked we can serve the header part of the page – the first chunk – to the browser while the server works on generating the body of the page; in other words we don’t need to generate the page all in one go and then serve it up. The Transfer-Encoding header informs the browser that the content for the current page is going to come down the pipe in pieces (”chunks”) 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 “header”) it can start to download the banner ad in parallel* while it waits for the remaining part of the page from the server. Overall the page should feel more responsive.

So, how to do it in a Java servlet. You would think you would just call the setHeader method on the servlet response object but you don’t – what were you thinking? Turns out it’s even easier than that! An example is given below:


void doGet(HttpServletRequest request,
           HttpServletResponse response)
   throws ServletException, IOException {

	response.setContentType("text/plain");
	PrintWriter out = response.getWriter();
	out.println("The start of the page.");
	out.flush();
	// Wait 10 seconds for no reason whatsoever
	try {
	     Thread.currentThread().sleep(10000);
	} catch (InterruptedException e) {
	     // Do nothing
	}
	out.println("The rest of the page ...");
}

Basically, if you write something to the output buffer and then call flush() that will automatically set the Transfer-Encoding 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.

End of note.

(*) 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.

CommentsTags: Uncategorized

URL Shorteners

June 24th, 2009 · Comments

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’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.

So how do they work? I still don’t know but here’s one approach that I took:

Let’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 – a simple one to one mapping – then that’s a lot of URLs! The key point is that, unlike a hash function, I don’t think the URL is used as input to determine what comes out of the other end; a “random” character code is generated and then is just stored with the URL so it can be retrieved with a simple table look-up.

I came up with a probabilistic approach to generating these “hash” 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 birthday paradox we should expect to see a collision after generating 2n/2 items, or approx. every 215 items for a 5 character code using the example code, assuming, of course, that all generated codes are equally likely to occur. It’s an example, it will do!

You can look at the source code here. In the example I use a bit vector to record what codes have already been generated; the bit vector is limited to representing 231-1 different values therefore the example code is restricted to generating a maximum of 5 character codes; each character requires 6 bits. I’ll let you do the math as I have already done it :)

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!

It’s a first attempt so there is likely some room for improvement but it’s a start. Would be great to hear any alternative thoughts on how these things work.

CommentsTags: Uncategorized

Alternative App Store?

June 19th, 2009 · Comments

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’s app store policy about what qualifies (and disqualifies) an application from being sold – or given away – on the app store. Now I don’t know much about Apple’s policy but I have followed the various discussions about it in the “media” 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’t help when I keep reading articles like this. From a business perspective I don’t want to spend months developing something only to have Apple turn around and reject it!

This neatly segways into my next point: Why isn’t there an independent store selling applications for the iPhone? A place where all the misfit applications rejected by Apple can be sold on – think of it as a council estate for mobile phone apps. Maybe there is one; I have no idea. I can see why developers would want to sell their apps through the App store as it’s baked right into iTunes and it’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’ll find out soon enough but if you have any useful advice in the meantime, please leave a comment.

* when I say “about to embark” that’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’t have any excuse not to get one.

CommentsTags: Uncategorized

The Trouble With URL Shorteners

June 17th, 2009 · Comments

I have just finished reading this post about URL shortening services and it got me thinking.

I use URL shorteners on the odd occasion but I have a problem with them. Answer the following simple question: What is the destination of the following links (and no peeking by clicking on them first):

  1. http://bit.ly/guNtb
  2. http://www.simonbuckle.com

Hopefully this highlights the problem: You don’t know where you are going to when you click on the links provided by these URL shortening services! This seems to me to be an area ripe for Internet scams (especially if you use Internet Explorer); I am thinking links to porn sites, links that download the latest malware on to your PC etc; there are endless possibilities!

What I would like to see is some kludge so that when you hover over one of these shortened URLs you can see the destination of the link. Sure, not all URLs indicate what exactly awaits you at the other end of it, and, in the case of Twitter, if it is someone that you are following then you can be fairly confident that they aren’t going to send you somewhere you really don’t want to go (or maybe you do). Still, there is definitely room for improvement.

CommentsTags: Uncategorized

New Host

June 10th, 2009 · Comments

By now you should be reading this on my new host, assuming the DNS changes have propagated. I finally got around to moving this blog from Joyent – the crap and (relatively) expensive hosting company, formerly known as TextDrive – to GoDaddy. It’s even running the latest version of bloatware for blogging!

I had been meaning to migrate for a while as I had been having problems with uptime, among other things. Ultimately, I had no choice but to migrate as a few weeks ago I tried to login using the password I always use but for some reason it was complaining about an incorrect password blah blah (and no, I wasn’t using the wrong password). Anyway, I reset my password and used the one Wordpress had generated for me but that too, for some still-unknown reason, wouldn’t work either. I was locked out of my own blog! What to do. Well, after some careful exporting of some database tables but not others, I now have a working blog again hosted elsewhere for half the cost! Migration was fairly painless, apart from the database munging. Was impressed with Disqus; migrating the comments just worked!

When I first started this site I decided that it was imperative that I have access to all of my data. I could have chosen some free-to-use blogging service but many of these services don’t give you access to your data; well, they certainly didn’t at the time anyway. It’s just as well I did otherwise I could well have ended up permanently locked out of my own site!

CommentsTags: Uncategorized

Finally, decent customer service

May 6th, 2009 · Comments

It’s been a long time coming but I believe I have finally found a company that knows how to do customer service properly! Yes, hard to believe I know, but it’s true!

About 4 months ago I signed up with Slicehost. I was paying a small fortune every month for the dedicated server I had with another hosting company, which I wasn’t using, so I decided to downgrade and give Slicehost a try. About 2 minutes after signing up – and I’m not exaggerating – I had access to my (virtual) server. Anyway, over the course of the last few months I hadn’t been using the server much (again!) so I decided to cancel it. My billing cycle was at the end of each month so I had just paid for the whole of May at the end of April. As I was cancelling now I figured there goes my money – other companies I have used before “delete” your server at the end of the billing cycle so you are effectively paying for server even though you are not using it. Anyway, I deleted the server and then suddenly noticed that my account had been credited (prorated) with an amount equal to the remaining amount that I would have paid had I kept the server. Amazing!! I’ll have to wait and see if my credit card gets recharged at the end of the month.

Contrast this with the experience I had a few weeks ago with the online learning site Busuu. Aside from the interesting name, their customer service leaves a lot to do desired. Let me explain. I wanted to see what the site was like so I paid for the premium service when I signed up. On the subscription page, under the basic terms, it claims that you can cancel at any time – you have to be registered to view the page so you will have to take my word for it. There is a link to the terms and conditions at the bottom of the page. My thinking was that I would try it for a month then cancel as, apparently, I could cancel at any time.

Anyway, a few days before my subscription was due to run out (at the beginning of April) I cancelled it. I then got an email saying that my subscription had been cancelled and would expire on the 5th May. “Strange”, I thought. I then got an email a few days later telling me that my credit card had been charged (again) for the next month. Given that I distinctly remember cancelling the subscription days before I was a bit surprised so I sent an email to the accounts department kindly asking them to give me my money back as I had cancelled the subscription. I was then told that you have to give a minimum of 7 days notice to cancel premium subscriptions and if I looked at the terms and conditions it clearly states so. Oh! You mean the terms and conditions that nobody reads? Now I’m not an English professor but this definitely contradicts the basic terms on the subscription page that states that “you may cancel your subscription at any time”.

Fair enough, I admit I didn’t read the terms and conditions but then who does? Hands up all of those who read the terms and conditions of all the web sites they sign up! Needless to say I didn’t get my money back and I haven’t been back to the site since nor do I intend to use it ever again … and all for the sake of 12.99 euros!

As for Slicehost I may not be using their servers at the moment but I fully intend to use them in the future when the need arises. And all because of decent of customer service. Others take note!

CommentsTags: Uncategorized

Java Puzzler

March 16th, 2009 · Comments

Yesterday I read an article of an interview with Joshua Bloch about Java. In the interview he talked about where Java was heading and some of its quirks. Here’s an example of a quirk he gave in the interview:


public class Unbelievable {
    static Integer i;
    public static void main(String[] args) {
            if (i == 42)
                System.out.println("Unbelievable");
    }
}

Fairly innocuous bit of code … so what does it do? Well if you run it you get a NullPointerException, which is no surprise as the variable hasn’t been initialized. What is surprising is why the compiler didn’t pick this up at compile time! For example, if I modify the code to the example below, it doesn’t compile because the variable has not been initialized:


public class Unbelievable {
    public static void main(String[] args) {
            Integer i;
            if (i == 42)
                System.out.println("Unbelievable");
    }
}

Any Java experts out there care to explain this one to me? I compiled the above examples using Java 1.5 on the Mac.

CommentsTags: Uncategorized

CNBC Gives Financial Advice

March 5th, 2009 · Comments

CommentsTags: Uncategorized

Honda Rescued By?

March 5th, 2009 · Comments

According to the BBC, Honda have received financial backing that will allow them to compete next season but – as at the time of writing this – haven’t said from who. Step forward Richard Branson. Why? Well I reckon it would have something to do with Branson’s Virgin Fuels venture and wouldn’t be just a branding exercise. A Formula One car would be a perfect vehicle – in more than one sense of the word – to test high performance fuels etc – assuming they can be competitive of course – and the amount of data they would obtain from the car could increase the rate at which the fuels are developed; it would do no harm at the very least.

There have been rumors over the last couple of weeks about Branson getting involved in F1 so I am not exactly being 100% original in my prediction but then again, I could still be completely wrong!

CommentsTags: Uncategorized

What’s In A Name

February 9th, 2009 · Comments

Every now and again I keep coming across stupid acronyms / names for things. Here are a few that I have come across recently:

  • The British Association of Aesthetic Plastic Surgeons – came across this in some news article about boob jobs gone wrong. Nicely abbreviates to BAPS.
  • Git – distributed source code control system.
  • It seems the Sound class in ActionScript 3.0 supports an ID3 tag called TITS.
  • PerfAnal – a performance analysis tool for Java.
Clearly the people who name these things are either having a laugh or need to attend some marketing classes!! Still, I guess they are memorable.

CommentsTags: Uncategorized