Archive for the ‘Tech’ Category

Encoding URLs in Java

Thursday, December 4th, 2014

To encode a URL in Java, create a new instance of java.net.URI and then call toString() or toASCIIString() on it.

DO NOT use java.net.URLEncoder. It is for HTML form encoding, not encoding URLs, despite its name.

Each part of a URL must be escaped differently, so use the multi-argument constructors for URI where you have multiple parts you want to stick together. You can pass in null for arguments you want to omit, and it works the way you’d hope.

This program:

class EncodeUrls
{
    public static void main( String[] args ) throws java.net.URISyntaxException
    {
        url( "http", "example.com", "/x/y/x", "x=y", "pos" );

        url(
            "http",
            "exa\uD83D\uDCA9mple.com",
            "/x/\uD83D\uDCA9/x",
            "x=\uD83D\uDCA9",
            "po\uD83D\uDCA9"
        );
    }

    private static void url(
        String scheme,
        String host,
        String path,
        String query,
        String fragment
    )
    throws java.net.URISyntaxException
    {
        java.net.URI uri = new java.net.URI(
            scheme, host, path, query, fragment );

        System.out.println( "." + uri.toString() );
        System.out.println( "." + uri.toASCIIString() );
        System.out.println();
    }
}

prints:

.http://example.com/x/y/x?x=y#pos
.http://example.com/x/y/x?x=y#pos

.http://exa💩mple.com/x/💩/x?x=💩#po💩
.http://exa%F0%9F%92%A9mple.com/x/%F0%9F%92%A9/x?x=%F0%9F%92%A9#po%F0%9F%92%A9

LibreOffice spell check not working in Lubuntu

Wednesday, November 12th, 2014

I installed LibreOffice into Lubuntu, but the spell checking didn’t work.

It turns out I need to install a dictionary for my locale:

sudo apt-get install myspell-en-gb

I needed to restart LibreOffice, and then I got spell checking as I typed.

Is it ok to represent dates as Unix time (seconds since the epoch) in JSON?

Thursday, November 6th, 2014

Yes.

If your JSON contains integer numbers that represent Unix time (seconds since the “epoch”), and you parse your JSON to JavaScript, the range of integers that can accurately be represented is -9007199254740992 to 9007199254740992 (ref: EcmaScript standard, section 8.5).

The year 4000AD starts at unix time 64060588815, which is 9007135194152192 less than the maximum.

The year 1AD started at unix time -2177452800, which is 9007197077288192 more than the minimum.

So there are enough numbers.

Using hexdump to represent a binary file in Java source code

Wednesday, November 5th, 2014

To embed a binary file (e.g. a png image) into Java source code:

$ hexdump -v -e '/1 "%3i, "' myfile.png
-119,  80,  78,  71,  13,  10,  26,  10,   0,   0,   0,  13,  73,  72,  68,  82, ...

Copy and paste the output into Java code like this:

private static final byte[] myfile = { (byte)-119,  80,  78,  71, ... };

Disabling screensaver on LXDE

Saturday, October 18th, 2014

Caffeine wasn’t working for me. I got an error like this:

dbus.exceptions.DBusException: org.freedesktop.DBus.Error.ServiceUnknown: The name org.freedesktop.ScreenSaver was not provided by any .service files

Bug 1280449 contained the solution for me. Thank you Paulius Vitkus:

sudo apt-add-repository ppa:behda/ppa
sudo apt-get remove caffeine
sudo apt-get update && sudo apt-get install caffeine