Java implements three ways of base64 encoding, the performance comparison of each method, and finally gives the best tool class practical operation record

A brief introduction to base64 encoding, and a performance comparison of the three implementation methods, you can directly use the tools provided at the end. The performance comparison does not need to be looked at carefully, and it is not nutritious.

 

1 base64 encoding

Encode the string into the format of [0-9a-zA-Z+/=], and then convert it to its original appearance through decoding. It's that simple.

I love coding! --> 5oiR54ix57yW56CB77yB---I love coding

2 Performance comparison of three implementation methods

package util.base64;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import org.apache.commons.codec.binary.Base64;

/**
 *
 * base64编码实现的三种实现方式性能测试,推荐使用Java8提供的方法
 *
 */
public class BASE64EncoderTest {

    /**
     * 实际测试编码与解码速度的话,Java 8提供的Base64比Apache Commons Codec提供的还要快
     * ,Apache Commons Codec提供的比sun.misc提供的还要快。
     * 因此在Java上若要使用Base64,这个Java 8的java.util提供的Base64类是首选!
     *
     *
     *
     * @param args
     */
    public static void main(String[] args){

        sunMiscDemo();
        apacheCommonsCodecDemo();
        java8UtilDemo();

    }

    /**
     * 早期在Java上做Base64的编码与解码,会使用到JDK里sun.misc套件下的BASE64Encoder和
     * BASE64Decoder这两个类别,用法如下:
     *Base64的加密解密都是使用sun.misc包下的BASE64Encoder及BASE64Decoder的sun.misc.
     * BASE64Encoder/BASE64Decoder类。
     * 这个类是sun公司的内部方法,并没有在java api中公开过,不属于JDK标准库范畴,但在JDK中包含了该类,可以直接使用。
     * 但是在Eclipse和MyEclipse中直接使用,却找不到该类。解决方法如下:

     */
    public static void sunMiscDemo() {
        BASE64Encoder encoder = new BASE64Encoder();
        BASE64Decoder decoder = new BASE64Decoder();
        String str = "字串文字";
        String strEncoder = null;
        String strDecoder = null;

        Long startDate = new Date().getTime();
        for(int i = 0; i < 1000000; i++){
            //编码
            try {
                strEncoder = encoder.encode(str.getBytes("UTF-8"));
                //System.out.println("strEncoder=" + strEncoder);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

            //解码
            try {
                strDecoder = new String(decoder.decodeBuffer(strEncoder), "UTF-8");
                //System.out.println("strDecoder=" + strDecoder);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }



        }
        Long endDate = new Date().getTime();
        System.out.println("SunMis test 100w次 加密解密耗时:" + (endDate - startDate));
    }

    /**
     * Apache Commons Codec有提供Base64的编码与解码功能,会使用到
     * org.apache.commons.codec.binary套件下的Base64类别
     */
    public static void apacheCommonsCodecDemo() {
        Base64 base64 = new Base64();
        String str = "字串文字";
        String strEncode = null;
        String strDecode = null;

            Long startDate = new Date().getTime();
            for(int i = 0; i < 1000000; i++){
                byte[] b = null;
                //编码
                try {
                    strEncode = new String(base64.encode(str.getBytes("UTF-8")), "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                //解码
                try {
                    strDecode = new String(base64.decode(strEncode.getBytes("UTF-8")), "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }


        }
        Long endDate = new Date().getTime();
        System.out.println("ApacheCommons  test 100w次耗时:" + (endDate - startDate));
    }

    /**
     * Java 8之后的作法
     * Java 8的java.util套件中,新增了Base64的类别,可以用来处理Base64的编码与解码,用法如下:
     *
     */
    public static void java8UtilDemo() {
        java.util.Base64.Encoder encoder = java.util.Base64.getEncoder();
        java.util.Base64.Decoder decoder = java.util.Base64.getDecoder();
        String str = "字串文字";
        String strEncoder = null;
        String strDecoder = null;

            Long startDate = new Date().getTime();
            for(int i = 0; i < 1000000; i++){
                //编码
                try {
                    strEncoder = encoder.encodeToString(str.getBytes("UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                //解码
                try {
                    strDecoder = new String(decoder.decode(strEncoder), "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            Long endDate = new Date().getTime();
            System.out.println("Java8 test 100w次耗时:" + (endDate - startDate));

          }

}

operation result:

3 Best way tools 

package util.base64;
import java.util.Base64;

/**
 * base64 加密解密
 */
public class Base64UtilXb {
    public static void main(String[] args) {
        System.out.println(encrypt("我爱编码!"));
    }
     /**
     * BASE64加密
     */
    public static String encrypt(String str){
        if(str==null)return null;
        byte[] bytes = str.getBytes();
        //Base64 加密
        String encoded = Base64.getEncoder().encodeToString(bytes);
        System.out.println("Base 64 加密后:" + encoded);
        return encoded;
    }

    /**
     * BASE64解密
     * @throws Exception
     */
    public static String  decrypt(String key)  {
        if(key==null)return null;
        byte[] decoded = Base64.getDecoder().decode(key);
        String decodeStr = new String(decoded);
        System.out.println("Base 64 解密后:" + decodeStr);
        return decodeStr;
    }

}

 

Guess you like

Origin blog.csdn.net/h4241778/article/details/108474534