本文整理汇总了Java中com.ibm.icu.text.CharsetDetector.setText方法的典型用法代码示例。如果您正苦于以下问题:Java CharsetDetector.setText方法的具体用法?Java CharsetDetector.setText怎么用?Java CharsetDetector.setText使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.ibm.icu.text.CharsetDetector
的用法示例。
在下文中一共展示了CharsetDetector.setText方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: checkCharset
import com.ibm.icu.text.CharsetDetector; //导入方法依赖的package包/类
public static CharsetMatch checkCharset(InputStream input) {
// BufferedInputStream bis = new BufferedInputStream(input);
CharsetDetector cd = new CharsetDetector();
try {
cd.setText(input);
} catch (IOException e) {
try {
input.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
CharsetMatch cm = cd.detect();
// if (cm != null) {
// //reader = cm.getReader();
// return cm.getName();
// } else {
// throw new UnsupportedCharsetException(null);
// }
return cm;
}
示例2: detectEncoding
import com.ibm.icu.text.CharsetDetector; //导入方法依赖的package包/类
/**
* 利用 icu4j 探测输入流编码,只能探测文本类型的输入流
* -
* 抛弃 juniversalchardet
*
* @param in
* @return
* @throws IOException
*/
public static Charset detectEncoding(InputStream in) throws IOException {
final CharsetDetector detector = new CharsetDetector();
detector.setText(in);
final CharsetMatch charsetMatch = detector.detect();
if (charsetMatch == null) {
log.info("Cannot detect source charset.");
return null;
}
//This is an integer from 0 to 100. The higher the value, the more confidence
//探测的相似度在 1~100 之间,相似度越高结果越准确。
int confidence = charsetMatch.getConfidence();
final String name = charsetMatch.getName();
log.info("CharsetMatch: {} ({}% 相似度,相似度小于 50% 时,可能编码无法判断。)", name, confidence);
//打印该文本编码,所有可能性
// CharsetMatch[] matches = detector.detectAll();
// System.out.println("All possibilities : " + Arrays.asList(matches));
return Charset.forName(name);
}
示例3: getText
import com.ibm.icu.text.CharsetDetector; //导入方法依赖的package包/类
/**
* Extract text to be indexed
*/
public static String getText(String mimeType, String encoding, InputStream isContent) throws IOException {
BufferedInputStream bis = new BufferedInputStream(isContent);
TextExtractor te = engine.get(mimeType);
String text = null;
if (te != null) {
if (mimeType.startsWith("text/") && encoding == null) {
CharsetDetector detector = new CharsetDetector();
detector.setText(bis);
CharsetMatch cm = detector.detect();
encoding = cm.getName();
}
text = te.extractText(bis, mimeType, encoding);
} else {
throw new IOException("Full text indexing of '" + mimeType + "' is not supported");
}
IOUtils.closeQuietly(bis);
return text;
}
示例4: getEncoding
import com.ibm.icu.text.CharsetDetector; //导入方法依赖的package包/类
protected String getEncoding( String requiredEncoding, File file, Log log )
throws IOException
{
FileInputStream fis = null;
try
{
fis = new FileInputStream( file );
CharsetDetector detector = new CharsetDetector();
detector.setDeclaredEncoding( requiredEncoding );
detector.setText( new BufferedInputStream( fis ) );
CharsetMatch[] charsets = detector.detectAll();
if ( charsets == null )
{
return null;
}
else
{
return charsets[0].getName();
}
}
finally
{
IOUtil.close( fis );
}
}
示例5: guessCharset
import com.ibm.icu.text.CharsetDetector; //导入方法依赖的package包/类
private Charset guessCharset(Path file, Charset charset) throws IOException {
CharsetDetector detector = new CharsetDetector();
byte[] data;
try (SeekableByteChannel byteChannel = Files.newByteChannel(file, StandardOpenOption.READ)) {
long size = byteChannel.size();
if (size >= Integer.MAX_VALUE) {
return guessCharsetChardet(file, charset);
}
int smallsize = (int) size;
ByteBuffer buffer = ByteBuffer.allocate(smallsize);
byteChannel.read(buffer);
data = buffer.array();
}
detector.setText(data);
CharsetMatch match = detector.detect();
return Charset.forName(match.getName());
}
示例6: sniff
import com.ibm.icu.text.CharsetDetector; //导入方法依赖的package包/类
public Encoding sniff() throws IOException {
try {
CharsetDetector detector = new CharsetDetector();
detector.setText(this);
CharsetMatch match = detector.detect();
Encoding enc = Encoding.forName(match.getName());
Encoding actual = enc.getActualHtmlEncoding();
if (actual != null) {
enc = actual;
}
if (enc != Encoding.WINDOWS1252 && enc.isAsciiSuperset()) {
return enc;
} else {
return null;
}
} catch (Exception e) {
return null;
}
}
示例7: parseContent
import com.ibm.icu.text.CharsetDetector; //导入方法依赖的package包/类
@Override
protected void parseContent(StreamLimiter streamLimiter, LanguageEnum lang)
throws IOException {
CharsetDetector detector = new CharsetDetector();
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(streamLimiter.getNewInputStream());
detector.setText(bis);
CharsetMatch match = detector.detect();
String content;
if (match != null)
content = match.getString();
else
content = IOUtils.toString(streamLimiter.getNewInputStream(), "UTF-8");
ParserResultItem result = getNewParserResultItem();
result.addField(ParserFieldEnum.content, content);
result.langDetection(10000, ParserFieldEnum.content);
} finally {
IOUtils.close(bis);
}
}
示例8: getCharsetFromText
import com.ibm.icu.text.CharsetDetector; //导入方法依赖的package包/类
/**
* Use a third party library as last resort to guess the charset from the
* bytes.
*/
private static String getCharsetFromText(byte[] content,
String declaredCharset, int maxLengthCharsetDetection) {
String charset = null;
// filter HTML tags
CharsetDetector charsetDetector = new CharsetDetector();
charsetDetector.enableInputFilter(true);
// give it a hint
if (declaredCharset != null)
charsetDetector.setDeclaredEncoding(declaredCharset);
// trim the content of the text for the detection
byte[] subContent = content;
if (maxLengthCharsetDetection != -1
&& content.length > maxLengthCharsetDetection) {
subContent = Arrays.copyOfRange(content, 0,
maxLengthCharsetDetection);
}
charsetDetector.setText(subContent);
try {
CharsetMatch charsetMatch = charsetDetector.detect();
charset = validateCharset(charsetMatch.getName());
} catch (Exception e) {
charset = null;
}
return charset;
}
示例9: toReader
import com.ibm.icu.text.CharsetDetector; //导入方法依赖的package包/类
public static Reader toReader(InputStream input) throws IOException {
if (!input.markSupported())
input = new BufferedInputStream(input);
CharsetDetector charsetDetector = new CharsetDetector();
charsetDetector.setText(input);
CharsetMatch m = charsetDetector.detect();
Reader reader;
if (m.getConfidence() > 50) {
reader = m.getReader();
} else {
reader = new InputStreamReader(input);
}
return reader;
}
示例10: getEncoding
import com.ibm.icu.text.CharsetDetector; //导入方法依赖的package包/类
public static String getEncoding(String text) {
InputStream bis = new ByteArrayInputStream(text.getBytes());
CharsetDetector detector = new CharsetDetector();
try {
detector.setText(bis);
} catch (IOException e) {
throw new RuntimeException(e);
}
String encoding = detector.detect().getName();
return encoding;
}
示例11: detect
import com.ibm.icu.text.CharsetDetector; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public String detect(InputStream stream, String defaultEncoding) throws IOException {
CharsetDetector detector = new CharsetDetector();
detector.setText(stream);
detector.setDeclaredEncoding(defaultEncoding);
detector.enableInputFilter(true);
CharsetMatch[] matches = detector.detectAll();
String encoding = null;
for (int i = 0; i < matches.length; i++) {
// Ensure that the detected encoding is supported in Java.
String candidateEncoding = matches[i].getName();
if (isSupportedEncoding(candidateEncoding)) {
encoding = candidateEncoding;
break;
}
}
return encoding;
}
示例12: detectEncoding
import com.ibm.icu.text.CharsetDetector; //导入方法依赖的package包/类
/**
* Returns the detected encoding of the given byte array.
*
* @param input The data to detect the encoding for.
* @param assume88591IfNotUtf8 True to assume that the encoding is ISO-8859-1 (the standard
* encoding for HTTP) if the bytes are not valid UTF-8. Only recommended if you can reasonably
* expect that other encodings are going to be specified. Full encoding detection is very
* expensive!
* @return The detected encoding.
*/
public static Charset detectEncoding(byte[] input, boolean assume88591IfNotUtf8) {
if (looksLikeValidUtf8(input)) {
return UTF_8;
}
if (assume88591IfNotUtf8) {
return ISO_8859_1;
}
// Fall back to the incredibly slow ICU. It might be better to just skip this entirely.
CharsetDetector detector = new CharsetDetector();
detector.setText(input);
CharsetMatch match = detector.detect();
return Charset.forName(match.getName().toUpperCase());
}
示例13: getCharset
import com.ibm.icu.text.CharsetDetector; //导入方法依赖的package包/类
/**
* Uses ICU4J to determine the charset of the given InputStream.
*
* @param input
* @return Detected charset name; null if not detected.
* @throws IOException
* @should detect charset correctly
*/
public static String getCharset(InputStream input) throws IOException {
CharsetDetector cd = new CharsetDetector();
try (BufferedInputStream bis = new BufferedInputStream(input)) {
cd.setText(bis);
CharsetMatch cm = cd.detect();
if (cm != null) {
return cm.getName();
}
}
return null;
}
示例14: getFileCharsetMatch
import com.ibm.icu.text.CharsetDetector; //导入方法依赖的package包/类
/**
* Detects charset/encoding for given file. Not 100% accurate for
* non-Unicode files.
*
* @param file the file for which to detect charset/encoding
* @return The match object form the detection process or <code>null</code> if no match was found
* @throws IOException
*/
@Nonnull
public static CharsetMatch getFileCharsetMatch(@Nonnull File file) throws IOException {
InputStream in = new BufferedInputStream(new FileInputStream(file));
CharsetDetector detector = new CharsetDetector();
detector.setText(in);
// Results are sorted on descending confidence, so we're only after the first one.
return detector.detectAll()[0];
}
示例15: ibmICU4j
import com.ibm.icu.text.CharsetDetector; //导入方法依赖的package包/类
private String ibmICU4j(byte[] bytes) {
CharsetDetector charsetDetector = new CharsetDetector();
charsetDetector.setText(bytes);
CharsetMatch charsetMatch = charsetDetector.detect();
String charset = charsetMatch.getName();
return charset;
}