Ilguiz (eel ghEEz) Latypov (ghee ) wrote,
Ilguiz (eel ghEEz) Latypov

Java codecs

What a shame! I spent few hours trying to find a decoder for Java string literals.

It turned out the Apache Commons Lang project, not Sun, provides the decoder and the encoder. I remembered its value from the time I was looking for an XML encoder to use in my JSP page.

I started my search when I realized that a brute force approach such as s.replace("\\\\", "\\").replace("\\n", "\n") would fail to correctly decode a legitimate sequence of 3 characters '\\', '\\' and 'n' into a string of 2 characters '\\' and 'n'. This is because multiple .replace() invocations apply per-character decoding more than once, which is wrong.
  • Post a new comment


    default userpic

    Your reply will be screened

    Your IP address will be recorded