MCPcopy Create free account
hub / github.com/diyhi/bbs / Coding

Class Coding

src/main/java/cms/utils/Coding.java:13–86  ·  view source on GitHub ↗

判定文件编码

Source from the content-addressed store, hash-verified

11 *
12 */
13public class Coding {
14 private static final Logger logger = LogManager.getLogger(Coding.class);
15
16 /**
17 * 判定文件是GBK还是UTF-8编码
18 * @param f
19 * @return
20 */
21 public static String detection(File f){
22 String charset = "GBK";
23 byte[] first3Bytes = new byte[3];
24 try {
25 boolean checked = false;
26 BufferedInputStream bis = new BufferedInputStream( new FileInputStream( f ) );
27 bis.mark( 0 );
28 int read = bis.read( first3Bytes, 0, 3 );
29 if ( read == -1 ) return charset;
30 if ( first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE ) {
31 charset = "UTF-16LE";
32 checked = true;
33 }
34 else if ( first3Bytes[0] == (byte) 0xFE && first3Bytes[1] == (byte) 0xFF ) {
35 charset = "UTF-16BE";
36 checked = true;
37 }
38 else if ( first3Bytes[0] == (byte) 0xEF && first3Bytes[1] == (byte) 0xBB && first3Bytes[2] == (byte) 0xBF ) {
39 charset = "UTF-8";
40 checked = true;
41 }
42 bis.reset();
43 if ( !checked ) {
44 // int len = 0;
45 int loc = 0;
46
47 while ( (read = bis.read()) != -1 ) {
48 loc++;
49 if ( read >= 0xF0 ) break;
50 if ( 0x80 <= read && read <= 0xBF ) // 单独出现BF以下的,也算是GBK
51 break;
52 if ( 0xC0 <= read && read <= 0xDF ) {
53 read = bis.read();
54 if ( 0x80 <= read && read <= 0xBF ) // 双字节 (0xC0 - 0xDF) (0x80
55 // - 0xBF),也可能在GB编码内
56 continue;
57 else break;
58 }
59 else if ( 0xE0 <= read && read <= 0xEF ) {// 也有可能出错,但是几率较小
60 read = bis.read();
61 if ( 0x80 <= read && read <= 0xBF ) {
62 read = bis.read();
63 if ( 0x80 <= read && read <= 0xBF ) {
64 charset = "UTF-8";
65 break;
66 }
67 else break;
68 }
69 else break;
70 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…