本文整理汇总了Java中java.nio.charset.CharsetDecoder.detectedCharset方法的典型用法代码示例。如果您正苦于以下问题:Java CharsetDecoder.detectedCharset方法的具体用法?Java CharsetDecoder.detectedCharset怎么用?Java CharsetDecoder.detectedCharset使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.charset.CharsetDecoder
的用法示例。
在下文中一共展示了CharsetDecoder.detectedCharset方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: detectingCharset
import java.nio.charset.CharsetDecoder; //导入方法依赖的package包/类
private static String detectingCharset(byte[] bytes) throws Exception {
//----------------------------------------------------------------
// Test special public methods of CharsetDecoder while we're here
//----------------------------------------------------------------
CharsetDecoder cd = Charset.forName("JISAutodetect").newDecoder();
check(cd.isAutoDetecting(), "isAutodecting()");
check(! cd.isCharsetDetected(), "isCharsetDetected");
cd.decode(ByteBuffer.wrap(new byte[] {(byte)'A'}));
check(! cd.isCharsetDetected(), "isCharsetDetected");
try {
cd.detectedCharset();
fail("no IllegalStateException");
} catch (IllegalStateException e) {}
cd.decode(ByteBuffer.wrap(bytes));
check(cd.isCharsetDetected(), "isCharsetDetected");
Charset cs = cd.detectedCharset();
check(cs != null, "cs != null");
check(! cs.newDecoder().isAutoDetecting(), "isAutodetecting()");
return cs.name();
}
示例2: handleDecodingError
import java.nio.charset.CharsetDecoder; //导入方法依赖的package包/类
/**
* Handle an error thrown while file decoding. Inform search listener and
* append detailed info into the IDE Log.
*/
protected final void handleDecodingError(SearchListener listener,
FileObject file, CharsetDecoder decoder,
CharacterCodingException e) {
String charsetName;
try {
if (decoder.isAutoDetecting() && decoder.isCharsetDetected()) {
Charset c = decoder.detectedCharset();
if (c != null) {
charsetName = c.displayName();
} else {
charsetName = decoder.charset().displayName();
}
} else {
charsetName = decoder.charset().displayName();
}
} catch (Exception ex) {
LOG.log(Level.INFO, "Failed to obtain actual charset", ex); //NOI18N
charsetName = decoder == null ? "null" : decoder.toString();//NOI18N
}
String msg = NbBundle.getMessage(ResultView.class,
"TEXT_INFO_ERROR_ENCODING", charsetName); //NOI18N
listener.fileContentMatchingError(file.getPath(),
new Exception(msg, e));
LOG.log(Level.INFO, "{0}; UnmappableCharacterException: {1}", //NOI18N
new Object[]{file.getPath(), e.getMessage()});
}