Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Base64 decoding using JDK6 only

Tags:

This question with regard to JDK 5 says, there is no implementation provided with JDK 5, but JDK 6 is supposed to have a sun.misc.Base64Decoder.

As far as I can tell though, this class is not provided with the JDK and I was not able to find any other similar classes in it

So, what is the situation like with JDK6?

I am aware of numerous implementations out there like the Commons and the JBoss ones, but we have a restrictive 3rd party lib policy, so I am trying to avoid reinventing the wheel.

like image 970
kostja Avatar asked May 06 '11 08:05

kostja


People also ask

How do I decode a Base64 encoded file?

To decode a file with contents that are base64 encoded, you simply provide the path of the file with the --decode flag. As with encoding files, the output will be a very long string of the original file. You may want to output stdout directly to a file.

Is it possible to decode Base64?

Base64 is an encoding, the strings you've posted are encoded. You can DECODE the base64 values into bytes (so just a sequence of bits). And from there, you need to know what these bytes represent and what original encoding they were represented in, if you wish to convert them again to a legible format.

Do Base64 always end in ==?

A more complete answer is that a base64 encoded string doesn't always end with a = , it will only end with one or two = if they are required to pad the string out to the proper length. That link is completely irrelevant to base64, though. – NH.

What does == mean in Base64?

When decoding Base64 text, four characters are typically converted back to three bytes. The only exceptions are when padding characters exist. A single = indicates that the four characters will decode to only two bytes, while == indicates that the four characters will decode to only a single byte. For example: Encoded.


2 Answers

There are official (non sun.misc) implementations in Java, but it's not where anyone suppose to be.

java.util.prefs.AbstractPreferences is the one that has the necessary methods to do so. You have to override put method.

And one more which is much easier to use:

javax.xml.bind.DatatypeConverter it has 2 methods of interest:

  • public static byte[] parseBase64Binary( String lexicalXSDBase64Binary )
  • public static String printBase64Binary( byte[] val )

clarification the base64 nature of AbstractPreferences: java.util.prefs.Preferences

     /**      * Associates a string representing the specified byte array with the      * specified key in this preference node.  The associated string is      * the Base64 encoding of the byte array, as defined in RFC 2045, Section 6.8,      * with one minor change: the string will consist solely of characters      * from the Base64 Alphabet; it will not contain any newline      * characters.  Note that the maximum length of the byte array is limited      * to three quarters of MAX_VALUE_LENGTH so that the length      * of the Base64 encoded String does not exceed MAX_VALUE_LENGTH.      * This method is intended for use in conjunction with      * {@link #getByteArray}.      */     public abstract void putByteArray(String key, byte[] value); 
like image 116
3 revs, 2 users 82% Avatar answered Oct 11 '22 12:10

3 revs, 2 users 82%


No, the situation didn't change between Java 5 and Java 6.

Unfortunately there is no official Base64 implementation in the Java SE platform. @bestsss has shown that there is in fact a (well-hidden) Base64 implementation in Java SE 6 (see his answer for more detail).

The Sun JDK ships with this class (sun.misc.Base64Decoder), but it's not specified and should not be used (especially as it's not required to exist in other implementations or even versions).

If you absolutely need to avoid third party libraries (Apache Commons Codec would be the traditional provider of a Base64 implementation), then you might want to copy a BSD (or similarly) licensed version into your project. There is a public domain implementation and that's about as painless as it gets, when it comes to licenses.

like image 27
Joachim Sauer Avatar answered Oct 11 '22 13:10

Joachim Sauer