com.ckjava.utils.CharsetUtils Maven / Gradle / Ivy
package com.ckjava.utils;
import java.nio.charset.UnsupportedCharsetException;
public class CharsetUtils implements Constants {
public static String getEncoding(String str) throws Exception {
if (str == null) {
throw new IllegalArgumentException("judge string encoding, the parameter is null");
}
try {
String encode = CHARSET.UTF8;
if (str.equals(new String(str.getBytes(encode), encode))) {
return encode;
}
encode = CHARSET.ISO88591;
if (str.equals(new String(str.getBytes(encode), encode))) {
return encode;
}
encode = CHARSET.UTF8;
if (str.equals(new String(str.getBytes(encode), encode))) {
return encode;
}
encode = CHARSET.GBK;
if (str.equals(new String(str.getBytes(encode), encode))) {
return encode;
}
} catch (Exception e) {
if (e instanceof UnsupportedCharsetException) {
throw new UnsupportedCharsetException("cann't judge the string's encoding");
} else {
throw new Exception("judge the string's encoding occurrence error", e);
}
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy