liulin
2024-07-29 b65029f9d6e351a714682c110343a8c23eb4e793
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.lunhan.xxx.common.security;
 
import com.lunhan.xxx.common.util.StringUtil;
 
import java.nio.charset.Charset;
import java.util.Base64;
 
/**
 * Base64工具类
 * @author linliu
 * @date   2019-06-14
 */
public final class Base64Util {
    private Base64Util() {
        throw new IllegalStateException("Utility class");
    }
    private static final String DEFAULT = "utf-8";
 
    /**
     * base64解码(utf-8编码)
     * @author linliu
     * @date   2019-06-14
     * @param str 密文字符串
     * @return 解码后的明文
     */
    public static String encode(String str) {
        return encode(str, DEFAULT);
    }
    /**
     * base64解码
     * @author linliu
     * @date   2019-06-14
     * @param str 密文字符串
     * @param encoding 编码格式[默认utf-8]
     * @return 解码后的明文
     */
    public static String encode(String str, String encoding) {
        if(StringUtil.isNullOrEmpty(str)) {
            return "";
        }
        if(StringUtil.isNullOrEmpty(encoding)) {
            encoding = DEFAULT;
        }
        byte[] bytes = str.getBytes(Charset.forName(encoding));
        return Base64.getEncoder().encodeToString(bytes);
    }
    /**
     * base64解码(utf-8编码)
     * @author linliu
     * @date   2019-06-14
     * @param str 密文字符串
     * @return 解码后的明文
     */
    public static String decode(String str) {
        return decode(str, DEFAULT);
    }
    /**
     * base64解码
     * @author linliu
     * @date   2019-06-14
     * @param str 密文字符串
     * @param encoding 编码格式[默认utf-8]
     * @return 解码后的明文
     */
    public static String decode(String str, String encoding) {
        if(StringUtil.isNullOrEmpty(str)) {
            return "";
        }
        if(StringUtil.isNullOrEmpty(encoding)) {
            encoding = DEFAULT;
        }
        byte[] bytes = Base64.getDecoder().decode(str);
        return new String(bytes, Charset.forName(encoding));
    }
 
    /**
     * 二进制流转成base64字符串
     * @param buffer 二进制数组
     * @return 编码后的base64字符串
     */
    public static String encodeStream(final byte[] buffer) {
        return Base64.getEncoder().encodeToString(buffer);
    }
 
    /**
     * 二进制流转成base64流
     * @param buffer 二进制数组
     * @return 编码后的base64流
     */
    public static byte[] encodeToStream(final byte[] buffer) {
        return Base64.getEncoder().encode(buffer);
    }
 
    /**
     * base64字符串转成二进制流
     * @param str 密文字符串
     * @return 解码后的文件流
     */
    public static byte[] decodeToStream(String str) {
        try {
            return Base64.getDecoder().decode(str);
        } catch (Exception e) {
            return new byte[0];
        }
    }
 
    /**
     * base64字符串转成二进制流
     * @param buffer 密文流
     * @return 解码后的文件流
     */
    public static byte[] decodeToStream(final byte[] buffer) {
        try {
            return Base64.getDecoder().decode(buffer);
        } catch (Exception e) {
            return new byte[0];
        }
    }
}