@NotNull
  FileType getOrDetectFromContent(@NotNull VirtualFile file, byte @Nullable [] content, @Nullable FileType fileTypeByName) {
    if (!isDetectable(file)) {
      if (fileTypeByName == DetectedByContentFileType.INSTANCE) {
        // allow opening empty file in IDEA's editor
        return DetectedByContentFileType.INSTANCE;
      }
      if (ScratchUtil.isScratch(file)) {
        return PlainTextFileType.INSTANCE;
      }
      return UnknownFileType.INSTANCE;
    }

    // while vfs events are processing do not access cache, it can be in invalid state;
    if (!myCanUseCachedDetectedFileType) {
      try {
        return detectFromContent(file, getFirstBytes(file, content), fileTypeByName);
      }
      catch (IOException e) {
        return UnknownFileType.INSTANCE;
      }
    }

    finishRedetectionIfEnqueued(file);

    if (file instanceof VirtualFileWithId) {
      int id = ((VirtualFileWithId)file).getId();

      long flags = packedFlags.get(id);
      if (!BitUtil.isSet(flags, ATTRIBUTES_WERE_LOADED_MASK)) {
        flags = readFlagsFromCache(file);
        flags = BitUtil.set(flags, ATTRIBUTES_WERE_LOADED_MASK, true);

        packedFlags.set(id, flags);
        if (toLog()) {
          log("F: getOrDetectFromContent(" + file.getName() + "): readFlagsFromCache() = " + readableFlags(flags));
        }
      }
      boolean autoDetectWasRun = BitUtil.isSet(flags, AUTO_DETECT_WAS_RUN_MASK);
      if (autoDetectWasRun) {
        FileType type = textOrBinaryFromCachedFlags(flags);
        if (toLog()) {
          log("F: getOrDetectFromContent(" + file.getName() + "):" +
              " cached type = " + (type==null?null:type.getName()) +
              "; packedFlags.get(id):" + readableFlags(flags) +
              "; getUserData(DETECTED_FROM_CONTENT_FILE_TYPE_KEY): " + file.getUserData(DETECTED_FROM_CONTENT_FILE_TYPE_KEY));
        }
        if (type != null) {
          return preferDetectedByContent(type, fileTypeByName);
        }
      }
    }
    FileType fileType = getFileTypeDetectedFromContent(file);
    if (toLog()) {
      log("F: getOrDetectFromContent(" + file.getName() + "): " +
          "getUserData(DETECTED_FROM_CONTENT_FILE_TYPE_KEY) = " + (fileType == null ? null : fileType.getName()));
    }
    if (fileType == null || fileType == DetectedByContentFileType.INSTANCE) {
      // run autodetection
      try {
        fileType = detectFromContentAndCache(file, content, fileTypeByName);
      }
      catch (IOException e) {
        fileType = UnknownFileType.INSTANCE;
      }
    }

    if (toLog()) {
      log("F: getOrDetectFromContent(" + file.getName() + "): getFileType after detect run = " + fileType.getName());
    }

    return fileType;
  }
@NotNull
  FileType getOrDetectFromContent(@NotNull VirtualFile file, byte @Nullable [] content, @Nullable FileType fileTypeByName)