Java8 Base64 和 Apache Base64 编码解码以及速度比较

在Java中经常用到Base64编码,下面介绍Apache Base64 编码 和 Java8 之后Util 包内Base64 两种编码方式,并加以比较。此处也添加了用于HTTP URL的安全编码方式。

一、Base64 简介(百度百科)

1. Base64是网络上最常见的用于传输8Bit字节码的编码方式之一。
2. Base64编码是从二进制到字符的过程,可用于在HTTP环境下传递较长的标识信息。

二、Apache Base64

此处Maven Pom 文件引入依赖,版本1.12最新版本:



    commons-codec
    commons-codec
    1.12

代码如下:

import org.apache.commons.codec.binary.Base64;

import java.io.UnsupportedEncodingException;

/**
 * @author fan
 * @create 2019-06-15 22:11
 * @description
 * @see
 */
public class ApacheBase64Util {
    private static Base64 base64;
    private static Base64 base64Safe;
    private static String UTF_8 ="UTF-8";

    static {
        base64 = new Base64();
        base64Safe = new Base64(true);
    }

    //base encode
    public static byte[] encode(byte[] bytes){
        return base64.encode(bytes);
    }

    public static String encode2String(byte[] bytes){
        return base64.encodeToString(bytes);
    }
    public static byte[] encode2Byte(String string){
        try {
            return base64.encode(string.getBytes(UTF_8));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static String encode(String strings){
        try {
            return base64.encodeToString(strings.getBytes(UTF_8));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
    //safe Base64 (URL Base64)
    public static byte[] safeEncode(byte[] bytes){
        return base64Safe.encode(bytes);
    }

    public static String safeEncode2String(byte[] bytes){
        return base64Safe.encodeToString(bytes);
    }
    public static byte[] safeEncode(String string){
        try {
            return base64Safe.encode(string.getBytes(UTF_8));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static String safeEncode2Byte(String strings){
        try {
            return base64Safe.encodeToString(strings.getBytes(UTF_8));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
    //decode
    public static byte[] decode(byte[] bytes){
        return base64.decode(bytes);
    }

    public static String decode2String(byte[] bytes){
        try {
            return new String(base64.decode(bytes),UTF_8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static byte[] decode(String string){
        try {
            return base64.decode(string.getBytes(UTF_8));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static String decode2Byte(String strings){
        try {
            return new String(decode(strings.getBytes(UTF_8)));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    //safeDecode
    public static byte[] safeDecode(byte[] bytes){
        return base64Safe.decode(bytes);
    }

    public static String safeDecode2String(byte[] bytes){
        try {
            return new String(base64Safe.decode(bytes),UTF_8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static byte[] safeDecode(String string){
        try {
            return base64Safe.decode(string.getBytes(UTF_8));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static String safeDecode2Byte(String strings){
        try {
            return new String(safeDecode(strings.getBytes(UTF_8)));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
}

三、Java8 Base64

此处使用JDK8,代码如下:

import java.io.UnsupportedEncodingException;
import java.util.Base64;

/**
 * @Author fankunfeng
 * @Date 2019-01-22 14:39:23
 * @Describe Java8之后Util包Base64编码,比apache方式效率更高
 */
public class JavaBase64Util {
    public static final String UTF_8 = "UTF-8";
    public static Base64.Encoder encoder;
    //即为安全的编码方式,替换“+” “/” “-”为“_”
    public static Base64.Encoder urlEncoder;
    public static Base64.Decoder decoder;
    public static Base64.Decoder urlDecoder;

    static {
        encoder = Base64.getEncoder();
        urlEncoder = Base64.getUrlEncoder();
        decoder = Base64.getDecoder();
        urlDecoder = Base64.getUrlDecoder();
    }
    //encode
    public static byte[] encode(byte[] bytes) {
        return encoder.encode(bytes);
    }

    public static String encode(String string) {
        byte[] encode = encode(string.getBytes());
        try {
            return new String(encode, UTF_8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static String encode2String(byte[] bytes) {
        return encoder.encodeToString(bytes);
    }
    public static byte[] encode2Byte(String string) {
        return encode(string.getBytes());
    }

    //urlEncoder
    public static byte[] urlEncode(byte[] bytes) {
        return urlEncoder.encode(bytes);
    }
    public static String urlEncode(String string) {
        byte[] encode = urlEncode(string.getBytes());
        try {
            return new String(encode, UTF_8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static String urlEncode2String(byte[] bytes) {
        return urlEncoder.encodeToString(bytes);
    }
    public static byte[] urlEncode2Byte(String string) {
        return urlEncode(string.getBytes());
    }
    //decode
    public static byte[] decode(byte[] bytes) {
        return decoder.decode(bytes);
    }
    public static byte[] decode2Byte(String string) {
        return decoder.decode(string.getBytes());
    }
    public static String decode2String(byte[] bytes) {
        try {
            return new String(decoder.decode(bytes),UTF_8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static String decode(String string) {
        byte[] decode = decode(string.getBytes());
        try {
            return new String(decode, UTF_8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
    //urlDecode
    public static byte[] urlDecode(byte[] bytes) {
        return urlDecoder.decode(bytes);
    }
    public static byte[] urlDecode2Byte(String string) {
        return urlDecode(string.getBytes());
    }
    public static String urlDecode2String(byte[] bytes) {
        try {
            return new String(urlDecode(bytes),UTF_8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static String urlDecode(String string) {
        byte[] decode = urlDecode(string.getBytes());
        try {
            return new String(decode, UTF_8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
}

四、测试代码

此处编写了测试代码比较两个工具类的效率,测试的基础字符为“abcdeABCDE”,分别叠加次数【1000 * 1000,1000 * 1000 * 10,1000 * 1000 * 20,1000 * 1000 * 40】,代码如下:

import com.fan.utils.ApacheBase64Util;
import com.fan.utils.JavaBase64Util;

import java.util.Calendar;

/**
 1. @author fan
 2. @create 2019-06-16 20:17
 3. @description
 4. @see
 */
public class Base64Test {
    public static StringBuffer sb = new StringBuffer("abcdeABCDE");
    public static final Base64Test test = new Base64Test();

    public static void main(String[] args) {
        test.num(1000*1000);
        test.num(1000*1000*10);
        test.num(1000*1000*20);
        test.num(1000*1000*40);
    }
    public void num(int num){
        for (int i = 0; i < num ; i++) {
            sb = sb.append(i);
        }
        test.javaBase64(num,sb.toString());
        test.apacheBase64(num,sb.toString());
    }

    public void javaBase64(int num,String string){
        long startTime = Calendar.getInstance().getTimeInMillis();
        String encode = JavaBase64Util.encode(string);
        long endTime = Calendar.getInstance().getTimeInMillis();
        System.out.println("Java Base64 encode 次数:"+num+"花费时间:"+ (endTime-startTime)+" ms");
        long startTime2 = Calendar.getInstance().getTimeInMillis();
        JavaBase64Util.decode(encode);
        long endTime2 = Calendar.getInstance().getTimeInMillis();
        System.out.println("Java Base64 decode 次数:"+num+"花费时间:"+ (endTime2-startTime2)+" ms");
    }

    public void apacheBase64(int num,String string){
        long startTime = Calendar.getInstance().getTimeInMillis();
        String encode = ApacheBase64Util.encode(string);
        long endTime = Calendar.getInstance().getTimeInMillis();
        System.out.println("apache Base64 encode 次数:"+num+" 花费时间:"+(endTime-startTime)+" ms");
        long startTime2 = Calendar.getInstance().getTimeInMillis();
        ApacheBase64Util.encode(encode);
        long endTime2 = Calendar.getInstance().getTimeInMillis();
        System.out.println("apache Base64 encode 次数:"+num+"花费时间:"+(endTime2-startTime2)+" ms");
    }
}

五、执行结果及说明

  1. 执行结果:
Java Base64 encode 次数:1000000花费时间:80 ms
Java Base64 decode 次数:1000000花费时间:48 ms
apache Base64 encode 次数:1000000 花费时间:106 ms
apache Base64 encode 次数:1000000花费时间:99 ms
Java Base64 encode 次数:10000000花费时间:362 ms
Java Base64 decode 次数:10000000花费时间:484 ms
apache Base64 encode 次数:10000000 花费时间:662 ms
apache Base64 encode 次数:10000000花费时间:797 ms
Java Base64 encode 次数:20000000花费时间:910 ms
Java Base64 decode 次数:20000000花费时间:1268 ms
apache Base64 encode 次数:20000000 花费时间:1718 ms
apache Base64 encode 次数:20000000花费时间:2449 ms
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at java.lang.StringCoding$StringEncoder.encode(StringCoding.java:300)
	at java.lang.StringCoding.encode(StringCoding.java:344)
	at java.lang.StringCoding.encode(StringCoding.java:387)
	at java.lang.String.getBytes(String.java:958)
	at com.fan.utils.JavaBase64Util.encode(JavaBase64Util.java:31)
	at com.fan.think.Base64Test.javaBase64Test(Base64Test.java:36)
	at com.fan.think.Base64Test.num(Base64Test.java:30)
	at com.fan.think.Base64Test.main(Base64Test.java:23)
  1. 结果说明:
    后续次数比较多内存溢出了,但是仍然可以很明显的看出Java8的执行效率比较高,文件越大越明显,推荐使用。

你可能感兴趣的:(Java8 Base64 和 Apache Base64 编码解码以及速度比较)