Monday, September 14, 2009

A lenient URL decoder for Java

The URLDecoder class in the JDK insists on doing a strict parsing of escape characters in an encoded URL string. Sometimes, the application might want to decode correctly escaped string sequences and leave incorrect sequences intact. In fact the Sun documentation states that this aspect of decode handling is implementation dependent. However, Sun's implementation is strict - it throws an exception when it encounters improper escape sequences rather than treating them like regular text.

I couldn't find a lenient implementation so hand-crafted this from the original source for URLDecode class found here.

Following is the lenient decode.

    public static String decodeLenient(String s, String enc)
throws UnsupportedEncodingException {

boolean needToChange = false;
StringBuffer sb = new StringBuffer();
int numChars = s.length();
int i = 0;

if (enc.length() == 0) {
throw new UnsupportedEncodingException("URLDecoder: empty string enc parameter");

while (i < numChars) {
char c = s.charAt(i);
switch (c) {
case '+':
sb.append(' ');
needToChange = true;
case '%':
# * Starting with this instance of %, process all
# * consecutive substrings of the form %xy. Each
# * substring %xy will yield a byte. Convert all
# * consecutive bytes obtained this way to whatever
# * character(s) they represent in the provided
# * encoding.
# */

// (numChars-i)/3 is an upper bound for the number
// of remaining bytes
byte[] bytes = new byte[(numChars - i) / 3];
int pos = 0;

while (((i + 2) < numChars) &&
(c == '%')) {
String hex = s.substring(i + 1, i + 3);
try {
bytes[pos] =
(byte) Integer.parseInt(hex, 16);
} catch (NumberFormatException e) {
sb.append(new String(bytes, 0, pos, enc));
pos = 0;

i += 3;
if (i < numChars)
c = s.charAt(i);

sb.append(new String(bytes, 0, pos, enc));

// A trailing, incomplete byte encoding such as
// "%x" will be treated as unencoded text
if ((i < numChars) && (c == '%')) {
for (; i<numChars; i++) {


needToChange = true;

return (needToChange ? sb.toString() : s);

Remove intrusive bing popups from content sites

Today as I was pursuing the site, in my quest to acquire the financial knowledge of the Wall Street robber barons, I stumbled on the latest feature from Bing : annoying hover over popups that show a search listing.

They are rather annoying for two reasons:
1) They appear over the text you are reading
2) It is very difficult to navigate around them as all you need to do is to have the mouse cursor on the link area for the helpful popup to appear

So I modified my previous GreaseMonkey script to handle this as well. Fortunately it is easy as this popup is fired off an anchor tag with an attribute of "itxtdid". All we need to do is remove the attribute, and the popup gets disabled.

Below is the new script. Enjoy.

// ==UserScript==
// @name test
// @namespace
// @include *
// ==/UserScript==
var allLinks, thisLink;
allLinks = document.evaluate(
for (var i = 0; i < allLinks.snapshotLength; i++) {
thisLink = allLinks.snapshotItem(i);
// do something with thisLink
if (thisLink.href.substring(0,19)=="") {
if (thisLink.hasAttribute("itxtdid")) {

Tuesday, September 01, 2009

Ubuntu 8.04 - sound on flash player with firefox

If sound works generally, but not inside Flash Player (in FireFox), try this:

thushara@agni:~$ sudo apt-get install libflashsupport
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
0 upgraded, 1 newly installed, 0 to remove and 131 not upgraded.
Need to get 8326B of archives.
After this operation, 65.5kB of additional disk space will be used.
Get:1 hardy/universe libflashsupport 1.9-0ubuntu1 [8326B]
Fetched 8326B in 0s (14.9kB/s)
Selecting previously deselected package libflashsupport.
(Reading database ... 99009 files and directories currently installed.)
Unpacking libflashsupport (from .../libflashsupport_1.9-0ubuntu1_i386.deb) ...
Setting up libflashsupport (1.9-0ubuntu1) ...

Processing triggers for libc6 ...
ldconfig deferred processing now taking place

restart FireFox and sound should be available...