private static @NotNull DetectResult detectInternalCharsetAndSetBOM(@NotNull VirtualFile file,
                                                                      byte @NotNull [] internalBuffer,
                                                                      int length,
                                                                      boolean saveBOM,
                                                                      @NotNull FileType fileType) {
    DetectResult info = detectHardCharset(file, internalBuffer, length, fileType);

    Charset charset;
    if (info.hardCodedCharset == null) {
      charset = file.isCharsetSet() ? file.getCharset() : getDefaultCharsetFromEncodingManager(file);
    }
    else {
      charset = info.hardCodedCharset;
    }

    byte[] bom = info.BOM;
    if (saveBOM && bom != null && bom.length != 0) {
      file.setBOM(bom);
      setCharsetAutoDetectionReason(file, AutoDetectionReason.FROM_BOM);
    }

    file.setCharset(charset);

    Charset result = charset;
    // optimisation
    if (info.guessed == CharsetToolkit.GuessedEncoding.SEVEN_BIT) {
      if (charset == StandardCharsets.UTF_8) {
        result = INTERNAL_SEVEN_BIT_UTF8;
      }
      else if (charset == StandardCharsets.ISO_8859_1) {
        result = INTERNAL_SEVEN_BIT_ISO_8859_1;
      }
      else if (charset == CharsetToolkit.WIN_1251_CHARSET) {
        result = INTERNAL_SEVEN_BIT_WIN_1251;
      }
    }

    return new DetectResult(result, info.guessed, bom);
  }
private static @NotNull DetectResult detectInternalCharsetAndSetBOM(@NotNull VirtualFile file,
                                                                      byte @NotNull [] internalBuffer,
                                                                      int length,
                                                                      boolean saveBOM,
                                                                      @NotNull FileType fileType)