Java Code Examples for com.intellij.openapi.vfs.CharsetToolkit#GuessedEncoding

The following examples show how to use com.intellij.openapi.vfs.CharsetToolkit#GuessedEncoding . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LoadTextUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
public static FileType processTextFromBinaryPresentationOrNull(@Nonnull byte[] bytes, int length,
                                                               @Nonnull VirtualFile virtualFile,
                                                               boolean saveDetectedSeparators,
                                                               boolean saveBOM,
                                                               @Nonnull FileType fileType,
                                                               @Nonnull NotNullFunction<? super CharSequence, ? extends FileType> fileTextProcessor) {
  DetectResult info = detectInternalCharsetAndSetBOM(virtualFile, bytes, length, saveBOM, fileType);
  Charset internalCharset = info.hardCodedCharset;
  CharsetToolkit.GuessedEncoding guessed = info.guessed;
  CharSequence toProcess;
  if (internalCharset == null || guessed == CharsetToolkit.GuessedEncoding.BINARY || guessed == CharsetToolkit.GuessedEncoding.INVALID_UTF8) {
    // the charset was not detected so the file is likely binary
    toProcess = null;
  }
  else {
    byte[] bom = info.BOM;
    int BOMEndOffset = Math.min(length, bom == null ? 0 : bom.length);
    ConvertResult result = convertBytes(bytes, BOMEndOffset, length, internalCharset);
    if (saveDetectedSeparators) {
      virtualFile.setDetectedLineSeparator(result.lineSeparator);
    }
    toProcess = result.text;
  }
  return fileTextProcessor.fun(toProcess);
}
 
Example 2
Source File: LoadTextUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static DetectResult guessFromBytes(@Nonnull byte[] content, int startOffset, int endOffset, @Nonnull Charset defaultCharset) {
  CharsetToolkit toolkit = new CharsetToolkit(content, defaultCharset);
  toolkit.setEnforce8Bit(true);
  Charset charset = toolkit.guessFromBOM();
  if (charset != null) {
    byte[] bom = ObjectUtils.notNull(CharsetToolkit.getMandatoryBom(charset), CharsetToolkit.UTF8_BOM);
    return new DetectResult(charset, null, bom);
  }
  CharsetToolkit.GuessedEncoding guessed = toolkit.guessFromContent(startOffset, endOffset);
  if (guessed == CharsetToolkit.GuessedEncoding.VALID_UTF8) {
    return new DetectResult(StandardCharsets.UTF_8, CharsetToolkit.GuessedEncoding.VALID_UTF8, null); //UTF detected, ignore all directives
  }
  return new DetectResult(null, guessed, null);
}
 
Example 3
Source File: LoadTextUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
DetectResult(Charset hardCodedCharset, CharsetToolkit.GuessedEncoding guessed, @Nullable byte[] BOM) {
  this.hardCodedCharset = hardCodedCharset;
  this.guessed = guessed;
  this.BOM = BOM;
}