private @NotNull FileType detectFromContent(@NotNull VirtualFile file, @NotNull ByteArraySequence bytes, @Nullable FileType fileTypeByName) throws IOException {
    List<FileTypeRegistry.FileTypeDetector> detectors = FileTypeRegistry.FileTypeDetector.EP_NAME.getExtensionList();
    // use PlainTextFileType because it doesn't supply its own charset detector
    // help set charset in the process to avoid double charset detection from content
    FileType fileType = LoadTextUtil.processTextFromBinaryPresentationOrNull(bytes,
                                                                             file, true, true,
                                                                             PlainTextFileType.INSTANCE, (@Nullable CharSequence text) -> {
        if (toLog()) {
          log("F: detectFromContentAndCache.processFirstBytes(" + file.getName() + "): bytes length=" + ((ByteSequence)bytes).length() +
              "; isText=" + (text != null) + "; text='" + (text == null ? null : StringUtil.first(text, 100, true)) + "'" +
              ", detectors=" + detectors);
        }
        FileType detected = null;
        for (FileTypeRegistry.FileTypeDetector detector : detectors) {
          try {
            detected = detector.detect(file, bytes, text);
          }
          catch (ProcessCanceledException e) {
            LOG.error("Detector " + detector + " (" + detector.getClass() + ") threw PCE. Bad detector, bad!", new RuntimeException(e));
          }
          catch (Exception e) {
            LOG.error("Detector " + detector + " (" + detector.getClass() + ") exception occurred:", e);
          }
          if (detected != null) {
            if (toLog()) {
              log("F: detectFromContentAndCache.processFirstBytes(" + file.getName() + "): " +
                  "detector " + detector + " type as " + detected.getName());
            }
            break;
          }
        }

        if (detected == null && !StringUtil.isEmpty(text)) {
          FileTypeManagerImpl.FileTypeWithDescriptor ftd = myFileTypeManager.patternsTable.findAssociatedFileTypeByHashBang(text);
          detected = ftd == null ? null : ftd.fileType();
        }
        if (detected == null) {
          detected = StringUtil.isEmpty(text) ? (bytes.getLength() == 0 ? DetectedByContentFileType.INSTANCE : UnknownFileType.INSTANCE): PlainTextFileType.INSTANCE;
          if (toLog()) {
            log("F: detectFromContentAndCache.processFirstBytes(" + file.getName() + "): " +
                "no detector was able to detect. assigned " + detected.getName());
          }
        }
        return detected;
      });

    if (toLog()) {
      try (InputStream newStream = ((FileSystemInterface)file.getFileSystem()).getInputStream(file)) {
        byte[] buffer2 = new byte[50];
        int n2 = newStream.read(buffer2, 0, buffer2.length);
        log("F: detectFromContentAndCache(" + file.getName() + "): result: " + fileType.getName() +
            "; newStream: " + streamInfo(newStream) +
            "; read: " + n2 +
            "; buffer: " + Arrays.toString(buffer2));
      }
    }

    if (LOG.isTraceEnabled()) {
      LOG.trace(file + "; type=" + fileType.getDescription() + "; " + counterAutoDetect);
    }

    // do not treat DetectedByContentFileType as a simple PlainTextFileType to give it a chance to re-detect itself later
    return preferDetectedByContent(fileType, fileTypeByName);
  }
private @NotNull FileType detectFromContent(@NotNull VirtualFile file, @NotNull ByteArraySequence bytes, @Nullable FileType fileTypeByName)