[测评系统]--测评系统核心代码库
linzhijie
2021-08-02 565244093e398d5ee6276978b0b60d8f1c13ccb5
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
package com.ots.common.utils.text;
import com.ots.common.utils.StringUtils;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
 
public class CharsetKit {
    
    public static final String ISO_8859_1 = "ISO-8859-1";
    
    public static final String UTF_8 = "UTF-8";
    
    public static final String GBK = "GBK";
    
    public static final Charset CHARSET_ISO_8859_1 = Charset.forName(ISO_8859_1);
    
    public static final Charset CHARSET_UTF_8 = Charset.forName(UTF_8);
    
    public static final Charset CHARSET_GBK = Charset.forName(GBK);
    
    public static Charset charset(String charset) {
        return StringUtils.isEmpty(charset) ? Charset.defaultCharset() : Charset.forName(charset);
    }
    
    public static String convert(String source, String srcCharset, String destCharset) {
        return convert(source, Charset.forName(srcCharset), Charset.forName(destCharset));
    }
    
    public static String convert(String source, Charset srcCharset, Charset destCharset) {
        if (null == srcCharset) {
            srcCharset = StandardCharsets.ISO_8859_1;
        }
        if (null == destCharset) {
            srcCharset = StandardCharsets.UTF_8;
        }
        if (StringUtils.isEmpty(source) || srcCharset.equals(destCharset)) {
            return source;
        }
        return new String(source.getBytes(srcCharset), destCharset);
    }
    
    public static String systemCharset() {
        return Charset.defaultCharset().name();
    }
}