[JAVA] Base64 encoder this and that

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

For JDK 1.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));
    }

For JDK 1.7 or earlier

Not standard for JDK 1.7 and earlier. The following are somehow usable (used). Of these, the Commons Codec is safe.

Forbidden sun.misc.BASE64Encoder

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

Base64 encoder this and that
This and that about Base64 (Java)
Docker settings and this and that
[Rails] strftime this and that
Digital certificate this and that
This and that of the JDK
This and that of Core Graphics
This and that of Swift corner Radius
This and that of conditional branching of rails development
This and that for editing ini in Java. : inieditor-java
[Java] Upload images and base64
String operations this and that-Java edition-
This and that of the implementation of date judgment within the period in Java