当前位置: 首页>>代码示例>>Java>>正文


Java LoadTextUtil.wasCharsetDetectedFromBytes方法代码示例

本文整理汇总了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;
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:33,代码来源:EncodingUtil.java

示例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);
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:32,代码来源:EncodingUtil.java

示例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;
}
 
开发者ID:consulo,项目名称:consulo,代码行数:33,代码来源:EncodingUtil.java

示例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);
}
 
开发者ID:consulo,项目名称:consulo,代码行数:32,代码来源:EncodingUtil.java


注:本文中的com.intellij.openapi.fileEditor.impl.LoadTextUtil.wasCharsetDetectedFromBytes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。