开发者

Base64 decoding using JDK6 only

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 开发者_StackOverflowI 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.


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.


Just like Joachim Sauer said in a previous comment JDK1.6 already comes bundled with it's own Base64 implementation (sun.misc.*), here's an example:

String toEncode = "Encoding and Decoding in Base64 Test";
//Encoding in b64
String encoded = new BASE64Encoder().encode(toEncode.getBytes());
System.out.println(encoded);
//Decoding in b64
byte[] decodeResult = new BASE64Decoder().decodeBuffer(encoded);
System.out.println(new String(decodeResult));


i used byte[] decodedValue = DatatypeConverter.parseBase64Binary(value);

this website explains well: https://www.rgagnon.com/javadetails/java-0598.html

excerpt from it:

   public static String encode(String value) throws Exception {
      return  DatatypeConverter.printBase64Binary
          (value.getBytes(StandardCharsets.UTF_8)); // use "utf-8" if java 6
   }

   public static String decode(String value) throws Exception {
      byte[] decodedValue = DatatypeConverter.parseBase64Binary(value);
      return new String(decodedValue, StandardCharsets.UTF_8); // use "utf-8" if java 6
   }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜