I am trying to use sun.misc.BASE64Encoder/Decoder, but this code:
(new sun.misc BASE64Encoder()).encode(new
sun.misc.BASE64Decoder().decodeBuffer("test string XML:"))
returns "test/string/XML/" I am embarrassed
Sun / Oracle said it was a bad idea. There were always alternatives. The JDK 11 replacement for sun.misc.BASE64Encoder and sun.miscBASE64Decoder is java.util.Base64.Encoder and java.util.Base64.Decoder. String encoded = new BASE64Encoder ().encode (bBytes); byte [] decoded = new BASE64Decoder ().decodeBuffer (encoded);
Decodes a Base64 encoded String into a newly-allocated byte array using the Base64 encoding scheme. Decodes all bytes from the input byte buffer using the Base64 encoding scheme, writing the results into a newly-allocated ByteBuffer. Returns an input stream for decoding Base64 encoded byte stream.
The JDK 11 replacement for sun.misc.BASE64Encoder and sun.miscBASE64Decoder is java.util.Base64.Encoder and java.util.Base64.Decoder. String encoded = new BASE64Encoder ().encode (bBytes); byte [] decoded = new BASE64Decoder ().decodeBuffer (encoded); The JDK 11 replacements which would generate identical values are: import java.util.Base64; // ...
In the actual test of encoding and decoding speed, Base64 provided by Java 8 is at least 11 times faster than that provided by sun.mis c suite and at least 3 times faster than that provided by Apache Commons Codec.
Don't use sun.misc
or com.sun
classes. They are not guaranteed to be consistent between different versions of the jre.
Use commons-codec Base64.encodeBase64(..)
and Base64.decodeBase64(..)
Use Class:
javax.xml.bind.DatatypeConverter
It has 2 methods of interest:
public static byte[] parseBase64Binary( String lexicalXSDBase64Binary )
public static String printBase64Binary( byte[] val )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With