public static void setDetectedFromBytesFlagBack(
     @NotNull VirtualFile virtualFile, @NotNull byte[] content) {
   if (virtualFile.getBOM() == null) {
     guessFromContent(virtualFile, content, content.length);
   } else {
     // prevent file to be reloaded in other encoding after save with BOM
     setCharsetWasDetectedFromBytes(virtualFile, AUTO_DETECTED_FROM_BOM);
   }
 }
  @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;
  }
 private static void checkFileLoadedInWrongEncoding(
     PsiFile file,
     InspectionManager manager,
     boolean isOnTheFly,
     VirtualFile virtualFile,
     Charset charset,
     List<ProblemDescriptor> descriptors) {
   if (FileDocumentManager.getInstance()
           .isFileModified(virtualFile) // when file is modified, it's too late to reload it
       || ChooseFileEncodingAction.isEnabledAndWhyNot(virtualFile)
           != null // can't reload in another encoding, no point trying
   ) {
     return;
   }
   // check if file was loaded in correct encoding
   byte[] bytes;
   try {
     bytes = virtualFile.contentsToByteArray();
   } catch (IOException e) {
     return;
   }
   String separator =
       FileDocumentManager.getInstance().getLineSeparator(virtualFile, file.getProject());
   String toSave = StringUtil.convertLineSeparators(file.getText(), separator);
   byte[] bom = virtualFile.getBOM();
   byte[] bytesToSave =
       ArrayUtil.mergeArrays(
           bom == null ? ArrayUtil.EMPTY_BYTE_ARRAY : bom, toSave.getBytes(charset));
   if (!Arrays.equals(bytesToSave, bytes)) {
     descriptors.add(
         manager.createProblemDescriptor(
             file,
             "File was loaded in a wrong encoding: '" + charset + "'",
             RELOAD_ENCODING_FIX,
             ProblemHighlightType.GENERIC_ERROR,
             isOnTheFly));
   }
 }