Using Libraries: Java.net

In addition to the Socket class, the Java.net library also contains a particular class which I learned of about a day later than I needed to. The name of the cob_spec test, “Parameter Decode,” should have given me a hint towards choosing the words I should use to search for in looking for the correct tool for the job, but I noticed that it looked like this test was asking me to convert the hexadecimal code in the query string into normal ASCII symbols, and thought I knew how to tackle this one. The hex codes are separated by percent signs (the test in FitNesse looks like this), so I figured I could separate the hex codes from the text by splitting the string literal into a string array on the percent signs and then converting each element from hex to string using the parseInt method from the Integer class like so:

    int decimal = Integer.parseInt(String hexCodeForSpace, 16);

I could then cast this int as a char and append it onto a string builder thusly:

    stringBuilder.append((char) decimal);

and then return the string from the string builder like this:

    return stringBuilder.toString();

When I first tried to convert each element from hex to symbol like this, it didn’t quite work because “variable_1=Operators” isn’t a hex code. So I tried to convert each element, but only if the length of that element was exactly two. This almost worked, but then I had to add spaces around the equals sign so that it looked like “variable_1 = Operators”. And then THIS almost worked, but it added spaces around the equals signs that had been decoded from hexadecimal. I noticed a few other complications around this time as well.

If you looked more closely than I did at the query string in the FitNesse test above, you might have noticed that it contains words represented by string literals separated by the hex code for a space (20). This caused me some trouble because I would have to separate the codes from words, and I didn’t want to have to read the string character by character in order to decide if the character was part of a word or hex code. It was at this point that I asked another apprentice if my approach made sense. And it was at this point that I was reminded of the Java.net library.

Within this library is the URLDecoder class that contains the method decode(String s, String encoding). This guy. In browsing the docs for the class I discovered that the method does the opposite of the encode(String s, String encoding) method (i.e. the method used to create the query string in the first place ; the parameter I needed to decode). This is my code before using the URLDecoder and this is the code after using it.