Search code examples
javaencodingjava-8base64sun

Java Base64 Sun to JDK8


I have a bunch of strings in the DB which were encoded with the sun.misc.BASE64Encoder a while ago.

Now I wanna decode (and encode further strings) with java.util.Base64.

The difference between these two is that the Sun one added a new line string at each n characters

Example:

Sun Base64:   54y49568uyj304j534w5y
              34y0639j6yh93j5h0653j
              s45hr68o

JDK8 Base64:  54y49568uyj304j534w5y34y0639j6yh93j5h0653js45hr68o

In order for the JDK decoder to parse these zipped strings, I would need to remove the new line characters.

Questions:

  1. Do I remove \r\n (Unix) or \n (Windows) or \r (old Macs)? Because the strings depend on which machine they were encoded

  2. If I say zippedString.replaceAll("\r", "").replaceAll("\n", "") how can I make sure that I won't have a \r chacter in the actual string, resulting in corrupted data?

  3. Is there any other way to create a bridge between these two mechanisms?


Solution

  • There is no white space in Base64 so I would remove the all.

    String toDecode = str.replaceAll("\\s+", "");
    

    This removes any ambiguity on how to handle specific newlines or spaces.