本文整理汇总了Java中com.intellij.openapi.vfs.CharsetToolkit.guessFromBOM方法的典型用法代码示例。如果您正苦于以下问题:Java CharsetToolkit.guessFromBOM方法的具体用法?Java CharsetToolkit.guessFromBOM怎么用?Java CharsetToolkit.guessFromBOM使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.intellij.openapi.vfs.CharsetToolkit
的用法示例。
在下文中一共展示了CharsetToolkit.guessFromBOM方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getCharsetAndBOM
import com.intellij.openapi.vfs.CharsetToolkit; //导入方法依赖的package包/类
@NotNull
private static Pair.NonNull<Charset,byte[]> getCharsetAndBOM(@NotNull byte[] content, @NotNull Charset charset) {
if (charset.name().contains(CharsetToolkit.UTF8) && CharsetToolkit.hasUTF8Bom(content)) {
return Pair.createNonNull(charset, CharsetToolkit.UTF8_BOM);
}
try {
Charset fromBOM = CharsetToolkit.guessFromBOM(content);
if (fromBOM != null) {
return Pair.createNonNull(fromBOM, ObjectUtils.notNull(CharsetToolkit.getMandatoryBom(fromBOM), ArrayUtil.EMPTY_BYTE_ARRAY));
}
}
catch (UnsupportedCharsetException ignore) {
}
return Pair.createNonNull(charset, ArrayUtil.EMPTY_BYTE_ARRAY);
}
示例2: charsetForWriting
import com.intellij.openapi.vfs.CharsetToolkit; //导入方法依赖的package包/类
@NotNull
private static Pair.NonNull<Charset, byte[]> charsetForWriting(@Nullable Project project,
@NotNull VirtualFile virtualFile,
@NotNull String text,
@NotNull Charset existing) {
Charset specified = extractCharsetFromFileContent(project, virtualFile, text);
Pair.NonNull<Charset, byte[]> chosen = chooseMostlyHarmlessCharset(existing, specified, text);
Charset charset = chosen.first;
// in case of "UTF-16", OutputStreamWriter sometimes adds BOM on it's own.
// see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6800103
byte[] bom = virtualFile.getBOM();
Charset fromBom = bom == null ? null : CharsetToolkit.guessFromBOM(bom);
if (fromBom != null && !fromBom.equals(charset)) {
chosen = Pair.createNonNull(fromBom, toBytes(text, fromBom));
}
return chosen;
}
示例3: tuple
import com.intellij.openapi.vfs.CharsetToolkit; //导入方法依赖的package包/类
@Nullable("null means no luck, otherwise it's tuple(guessed encoding, hint about content if was unable to guess, BOM)")
public static Trinity<Charset, CharsetToolkit.GuessedEncoding, byte[]> guessFromContent(@NotNull VirtualFile virtualFile, @NotNull byte[] content, int length) {
Charset defaultCharset = ObjectUtils.notNull(EncodingManager.getInstance().getEncoding(virtualFile, true), CharsetToolkit.getDefaultSystemCharset());
CharsetToolkit toolkit = GUESS_UTF ? new CharsetToolkit(content, defaultCharset) : null;
String detectedFromBytes = null;
try {
if (GUESS_UTF) {
toolkit.setEnforce8Bit(true);
Charset charset = toolkit.guessFromBOM();
if (charset != null) {
detectedFromBytes = AUTO_DETECTED_FROM_BOM;
byte[] bom = ObjectUtils.notNull(CharsetToolkit.getMandatoryBom(charset), CharsetToolkit.UTF8_BOM);
return Trinity.create(charset, null, bom);
}
CharsetToolkit.GuessedEncoding guessed = toolkit.guessFromContent(length);
if (guessed == CharsetToolkit.GuessedEncoding.VALID_UTF8) {
detectedFromBytes = "auto-detected from bytes";
return Trinity.create(CharsetToolkit.UTF8_CHARSET, guessed, null); //UTF detected, ignore all directives
}
if (guessed == CharsetToolkit.GuessedEncoding.SEVEN_BIT) {
return Trinity.create(null, guessed, null);
}
}
return null;
}
finally {
setCharsetWasDetectedFromBytes(virtualFile, detectedFromBytes);
}
}