Base64 decoding using JDK6 only
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);
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.