public class EncodingTest {
private static String UTF_8 = "656E636F64696E673D225554462D3822";
private static String UTF_16 =
"65006E0063006F00640069006E0067003D0022005500540046002D003100360022";
private final static char[] hexArray = "0123456789ABCDEF".toCharArray();
public static InputStream readInputStream(String location) {
InputStream in = null;
try {
in = new FileInputStream(new File(location));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return in;
}
public static String testByteEncodingFinder(InputStream inputStream) {
String ecncodingFormat = null;
try {
byte[] bytes = IOUtils.toByteArray(inputStream);
char[] hexChars = new char[200];
if (bytes.length > 100)
for (int i = 0; i < 100; i++) {
int v = bytes[i] & 0xFF;
hexChars[i * 2] = hexArray[v >>> 4];
hexChars[i * 2 + 1] = hexArray[v & 0x0F];
}
String text = new String(hexChars);
if (text.contains(UTF_16)) {
ecncodingFormat = StandardCharsets.UTF_16.name();
} else if (text.contains(UTF_8)) {
ecncodingFormat = StandardCharsets.UTF_8.name();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ecncodingFormat;
}
public static void main(String[] args) {
testByteEncodingFinder(readInputStream("test_279108466.xml"));
testByteEncodingFinder(readInputStream("Japanese UTF-16.xml"));
}
}
Instagram: https://www.instagram.com/diAvokato/
Un saludo virtual.