zhuguifei
2025-04-28 442928123f63ee497d766f9a7a14f0a6ee067e25
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
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
 
package org.jeecg.modules.doc.util;
 
import org.jeecg.modules.doc.exception.UFOPException;
 
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Arrays;
 
public class CharsetUtils {
    public CharsetUtils() {
    }
 
    public static byte[] convertTxtCharsetToGBK(byte[] bytes, String extendName) {
        if (Arrays.asList(UFOPUtils.TXT_FILE).contains(extendName)) {
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
 
            try {
                String str = new String(bytes, getFileCharsetName(byteArrayInputStream));
                return str.getBytes("GBK");
            } catch (IOException var4) {
                throw new UFOPException(var4);
            }
        } else {
            return bytes;
        }
    }
 
    public static byte[] convertTxtCharsetToUTF8(byte[] bytes, String extendName) {
        if (Arrays.asList(UFOPUtils.TXT_FILE).contains(extendName)) {
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
 
            try {
                String str = new String(bytes, getFileCharsetName(byteArrayInputStream));
                return str.getBytes("UTF-8");
            } catch (IOException var4) {
                throw new UFOPException(var4);
            }
        } else {
            return bytes;
        }
    }
 
    public static String getFileCharsetName(InputStream inputStream) throws IOException {
        String charset = "GBK";
        byte[] first3Bytes = new byte[3];
 
        try {
            boolean checked = false;
            BufferedInputStream bis = new BufferedInputStream(inputStream);
            bis.mark(0);
            int read = bis.read(first3Bytes, 0, 3);
            if (read == -1) {
                bis.close();
                return charset;
            }
 
            if (first3Bytes[0] == -1 && first3Bytes[1] == -2) {
                charset = "UTF-16LE";
                checked = true;
            } else if (first3Bytes[0] == -2 && first3Bytes[1] == -1) {
                charset = "UTF-16BE";
                checked = true;
            } else if (first3Bytes[0] == -17 && first3Bytes[1] == -69 && first3Bytes[2] == -65) {
                charset = "UTF-8";
                checked = true;
            }
 
            bis.reset();
            if (!checked) {
                label72:
                do {
                    do {
                        if ((read = bis.read()) == -1 || read >= 240 || 128 <= read && read <= 191) {
                            break label72;
                        }
 
                        if (192 <= read && read <= 223) {
                            read = bis.read();
                            continue label72;
                        }
                    } while(224 > read || read > 239);
 
                    read = bis.read();
                    if (128 <= read && read <= 191) {
                        read = bis.read();
                        if (128 <= read && read <= 191) {
                            charset = "UTF-8";
                        }
                    }
                    break;
                } while(128 <= read && read <= 191);
            }
 
            bis.close();
        } catch (Exception var6) {
            var6.printStackTrace();
        }
 
        return charset;
    }
 
    public static void main(String[] args) {
        System.out.println(Charset.forName("GB2312").newEncoder().canEncode("ÀîÑÅ΢"));
        System.out.println(Charset.forName("ISO-8859-1").newEncoder().canEncode("ÀîÑÅ΢"));
    }
}