本文整理汇总了Java中com.intellij.openapi.fileEditor.impl.LoadTextUtil.wasCharsetDetectedFromBytes方法的典型用法代码示例。如果您正苦于以下问题:Java LoadTextUtil.wasCharsetDetectedFromBytes方法的具体用法?Java LoadTextUtil.wasCharsetDetectedFromBytes怎么用?Java LoadTextUtil.wasCharsetDetectedFromBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.intellij.openapi.fileEditor.impl.LoadTextUtil
的用法示例。
在下文中一共展示了LoadTextUtil.wasCharsetDetectedFromBytes方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: isSafeToReloadIn
import com.intellij.openapi.fileEditor.impl.LoadTextUtil; //导入方法依赖的package包/类
static Magic8 isSafeToReloadIn(@NotNull VirtualFile virtualFile, @NotNull String text, @NotNull byte[] bytes, @NotNull Charset charset) {
// file has BOM but the charset hasn't
byte[] bom = virtualFile.getBOM();
if (bom != null && !CharsetToolkit.canHaveBom(charset, bom)) return Magic8.NO_WAY;
// the charset has mandatory BOM (e.g. UTF-xx) but the file hasn't or has wrong
byte[] mandatoryBom = CharsetToolkit.getMandatoryBom(charset);
if (mandatoryBom != null && !ArrayUtil.startsWith(bytes, mandatoryBom)) return Magic8.NO_WAY;
String loaded = LoadTextUtil.getTextByBinaryPresentation(bytes, charset).toString();
String separator = FileDocumentManager.getInstance().getLineSeparator(virtualFile, null);
String toSave = StringUtil.convertLineSeparators(loaded, separator);
String failReason = LoadTextUtil.wasCharsetDetectedFromBytes(virtualFile);
if (failReason != null && CharsetToolkit.UTF8_CHARSET.equals(virtualFile.getCharset()) && !CharsetToolkit.UTF8_CHARSET.equals(charset)) {
return Magic8.NO_WAY; // can't reload utf8-autodetected file in another charset
}
byte[] bytesToSave;
try {
bytesToSave = toSave.getBytes(charset);
}
catch (UnsupportedOperationException e) {
return Magic8.NO_WAY;
}
if (bom != null && !ArrayUtil.startsWith(bytesToSave, bom)) {
bytesToSave = ArrayUtil.mergeArrays(bom, bytesToSave); // for 2-byte encodings String.getBytes(Charset) adds BOM automatically
}
return !Arrays.equals(bytesToSave, bytes) ? Magic8.NO_WAY : loaded.equals(text) ? Magic8.ABSOLUTELY : Magic8.WELL_IF_YOU_INSIST;
}
示例2: checkCanReload
import com.intellij.openapi.fileEditor.impl.LoadTextUtil; //导入方法依赖的package包/类
@NotNull
// returns existing charset (null means N/A), failReason: null means enabled, notnull means disabled and contains error message
public static Pair<Charset, String> checkCanReload(@NotNull VirtualFile virtualFile) {
if (virtualFile.isDirectory()) {
return Pair.create(null, "file is a directory");
}
FileDocumentManager documentManager = FileDocumentManager.getInstance();
Document document = documentManager.getDocument(virtualFile);
if (document == null) return Pair.create(null, "binary file");
Charset charsetFromContent = ((EncodingManagerImpl)EncodingManager.getInstance()).computeCharsetFromContent(virtualFile);
Charset existing = charsetFromContent;
String failReason = LoadTextUtil.wasCharsetDetectedFromBytes(virtualFile);
if (failReason != null) {
// no point changing encoding if it was auto-detected
existing = virtualFile.getCharset();
}
else if (charsetFromContent != null) {
failReason = "hard coded in text";
}
else {
Pair<Charset, String> fileTypeCheck = checkHardcodedCharsetFileType(virtualFile);
if (fileTypeCheck.second != null) {
failReason = fileTypeCheck.second;
existing = fileTypeCheck.first;
}
}
if (failReason != null) {
return Pair.create(existing, failReason);
}
return Pair.create(virtualFile.getCharset(), null);
}
示例3: isSafeToReloadIn
import com.intellij.openapi.fileEditor.impl.LoadTextUtil; //导入方法依赖的package包/类
static Magic8 isSafeToReloadIn(@Nonnull VirtualFile virtualFile, @Nonnull String text, @Nonnull byte[] bytes, @Nonnull Charset charset) {
// file has BOM but the charset hasn't
byte[] bom = virtualFile.getBOM();
if (bom != null && !CharsetToolkit.canHaveBom(charset, bom)) return Magic8.NO_WAY;
// the charset has mandatory BOM (e.g. UTF-xx) but the file hasn't or has wrong
byte[] mandatoryBom = CharsetToolkit.getMandatoryBom(charset);
if (mandatoryBom != null && !ArrayUtil.startsWith(bytes, mandatoryBom)) return Magic8.NO_WAY;
String loaded = LoadTextUtil.getTextByBinaryPresentation(bytes, charset).toString();
String separator = FileDocumentManager.getInstance().getLineSeparator(virtualFile, null);
String toSave = StringUtil.convertLineSeparators(loaded, separator);
String failReason = LoadTextUtil.wasCharsetDetectedFromBytes(virtualFile);
if (failReason != null && CharsetToolkit.UTF8_CHARSET.equals(virtualFile.getCharset()) && !CharsetToolkit.UTF8_CHARSET.equals(charset)) {
return Magic8.NO_WAY; // can't reload utf8-autodetected file in another charset
}
byte[] bytesToSave;
try {
bytesToSave = toSave.getBytes(charset);
}
catch (UnsupportedOperationException e) {
return Magic8.NO_WAY;
}
if (bom != null && !ArrayUtil.startsWith(bytesToSave, bom)) {
bytesToSave = ArrayUtil.mergeArrays(bom, bytesToSave); // for 2-byte encodings String.getBytes(Charset) adds BOM automatically
}
return !Arrays.equals(bytesToSave, bytes) ? Magic8.NO_WAY : loaded.equals(text) ? Magic8.ABSOLUTELY : Magic8.WELL_IF_YOU_INSIST;
}
示例4: checkCanReload
import com.intellij.openapi.fileEditor.impl.LoadTextUtil; //导入方法依赖的package包/类
@Nonnull
// returns pair (existing charset (null means N/A); failReason: null means enabled, notnull means disabled and contains error message)
public static Pair<Charset, String> checkCanReload(@Nonnull VirtualFile virtualFile) {
if (virtualFile.isDirectory()) {
return Pair.create(null, "file is a directory");
}
FileDocumentManager documentManager = FileDocumentManager.getInstance();
Document document = documentManager.getDocument(virtualFile);
if (document == null) return Pair.create(null, "binary file");
Charset charsetFromContent = ((EncodingManagerImpl)EncodingManager.getInstance()).computeCharsetFromContent(virtualFile);
Charset existing = charsetFromContent;
String failReason = LoadTextUtil.wasCharsetDetectedFromBytes(virtualFile);
if (failReason != null) {
// no point changing encoding if it was auto-detected
existing = virtualFile.getCharset();
}
else if (charsetFromContent != null) {
failReason = "hard coded in text";
}
else {
Pair<Charset, String> fileTypeCheck = checkHardcodedCharsetFileType(virtualFile);
if (fileTypeCheck.second != null) {
failReason = fileTypeCheck.second;
existing = fileTypeCheck.first;
}
}
if (failReason != null) {
return Pair.create(existing, failReason);
}
return Pair.create(virtualFile.getCharset(), null);
}