Base64 decoding using JDK6 only

后端 未结 4 826
迷失自我
迷失自我 2020-12-29 04:37

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

相关标签:
4条回答
  • 2020-12-29 05:13

    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 讨论(0)
  • 2020-12-29 05:18

    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.

    0 讨论(0)
  • 2020-12-29 05:22

    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));
    
    0 讨论(0)
  • 2020-12-29 05:24

    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);
    
    0 讨论(0)
提交回复
热议问题