//
|
// 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("ÀîÑÅ΢"));
|
}
|
}
|