Once upon a time there was no standard Base64 encoder class in java. However, it was added from JDK 1.8. → What's new in JDK 8
With JDK 1.8, don't hesitate to use java.util.Base64.
Main.java
public static void main(String[] args) {
byte[] buf = { 1, 2, 3 };
java.util.Base64.Encoder e = java.util.Base64.getEncoder();
System.out.println(e.encodeToString(buf));
}
Not standard for JDK 1.7 and earlier. The following are somehow usable (used). Of these, the Commons Codec is safe.
It's been in the JDK for some time, even though it's not in the JDK. But it's not good to use.
Main.java
public static void main(String[] args) {
byte[] buf = { 1, 2, 3 };
sun.misc.BASE64Encoder e = new sun.misc.BASE64Encoder();
System.out.println(e.encode(buf));
}
Compiling with JDK 1.4 and 1.5 gives no warning. With JDK 1.6
Main.java:6:warning:sun.misc.BASE64Encoder is a Sun-owned API that may be removed in future releases.
sun.misc.BASE64Encoder e = new sun.misc.BASE64Encoder();
^
Main.java:6:warning:sun.misc.BASE64Encoder is a Sun-owned API that may be removed in future releases.
sun.misc.BASE64Encoder e = new sun.misc.BASE64Encoder();
^
2 warnings
With JDK 1.7 and 1.8
Main.java:6:warning:BASE64Encoder is an internally owned API and may be removed in a future release
sun.misc.BASE64Encoder e = new sun.misc.BASE64Encoder();
^
Main.java:6:warning:BASE64Encoder is an internally owned API and may be removed in a future release
sun.misc.BASE64Encoder e = new sun.misc.BASE64Encoder();
^
2 warnings
I thought it would disappear in JDK 1.8, but it was still there. At the moment it can be compiled and run.
Recommended Posts