我需要检查编码文件。此代码有效,但有点长。如何能够对这个逻辑进行任何重构。也许可以为此目标使用其他变体?
代码:
class CharsetDetector implements Checker {
Charset detectCharset(File currentFile, String[] charsets) {
Charset charset = null;
for (String charsetName : charsets) {
charset = detectCharset(currentFile, Charset.forName(charsetName));
if (charset != null) {
break;
}
}
return charset;
}
private Charset detectCharset(File currentFile, Charset charset) {
try {
BufferedInputStream input = new BufferedInputStream(
new FileInputStream(currentFile));
CharsetDecoder decoder = charset.newDecoder();
decoder.reset();
byte[] buffer = new byte[512];
boolean identified = false;
while ((input.read(buffer) != -1) && (!identified)) {
identified = identify(buffer, decoder);
}
input.close();
if (identified) {
return charset;
} else {
return null;
}
} catch (Exception e) {
return null;
}
}
private boolean identify(byte[] bytes, CharsetDecoder decoder) {
try {
decoder.decode(ByteBuffer.wrap(bytes));
} catch (CharacterCodingException e) {
return false;
}
return true;
}
@Override
public boolean check(File fileChack) {
if (charsetDetector(fileChack)) {
return true;
}
return false;
}
private boolean charsetDetector(File currentFile) {
String[] charsetsToBeTested = { "UTF-8", "windows-1253", "ISO-8859-7" };
CharsetDetector charsetDetector = new CharsetDetector();
Charset charset = charsetDetector.detectCharset(currentFile,
charsetsToBeTested);
if (charset != null) {
try {
InputStreamReader reader = new InputStreamReader(
new FileInputStream(currentFile), charset);
@SuppressWarnings("unused")
int valueReaders = 0;
while ((valueReaders = reader.read()) != -1) {
return true;
}
reader.close();
} catch (FileNotFoundException exc) {
System.out.println("File not found!");
exc.printStackTrace();
} catch (IOException exc) {
exc.printStackTrace();
}
} else {
System.out.println("Unrecognized charset.");
return false;
}
return true;
}
}
问题:
- 这个程序逻辑是如何重构的?
- 还有哪些检测编码的方法(如UTF-16序列等)?