<commit_before>package com.intellij.aws.cloudformation;

import com.google.common.base.Charsets;
import com.intellij.icons.AllIcons;
import com.intellij.json.JsonLanguage;
import com.intellij.openapi.fileTypes.LanguageFileType;
import com.intellij.openapi.fileTypes.ex.FileTypeIdentifiableByVirtualFile;
import com.intellij.openapi.util.io.ByteSequence;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.newvfs.FileSystemInterface;
import com.intellij.util.Processor;
import org.apache.commons.lang.ArrayUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;

public class CloudFormationFileType extends LanguageFileType implements FileTypeIdentifiableByVirtualFile {
  public static final CloudFormationFileType INSTANCE = new CloudFormationFileType();

  private static final String EXTENSION = "template";
  private static final byte[] BYTES_TO_DETECT_CFN_FILE = CloudFormationSections.FormatVersion.getBytes(Charsets.US_ASCII);

  public CloudFormationFileType() {
    super(JsonLanguage.INSTANCE);
  }

  @NotNull
  @Override
  public String getName() {
    return "AWSCloudFormation";
  }

  @NotNull
  @Override
  public String getDescription() {
    return "AWS CloudFormation templates";
  }

  @NotNull
  @Override
  public String getDefaultExtension() {
    return "";
  }

  @Nullable
  @Override
  public Icon getIcon() {
    return AllIcons.FileTypes.Json;
  }

  @Override
  public boolean isMyFileType(@NotNull VirtualFile file) {
    return CloudFormationFileType.EXTENSION.equalsIgnoreCase(file.getExtension()) && detectFromContent(file);
  }

  private boolean detectFromContent(@NotNull VirtualFile file) {
    try {
      final InputStream inputStream = ((FileSystemInterface) file.getFileSystem()).getInputStream(file);
      try {
        return FileUtil.processFirstBytes(inputStream, 1024, new Processor<ByteSequence>() {
          @Override
          public boolean process(ByteSequence byteSequence) {
            return findArray(byteSequence.getBytes(), BYTES_TO_DETECT_CFN_FILE) >= 0;
          }
        });
      } finally {
        inputStream.close();<commit_msg>EA-71640 - UOE: StubVirtualFile.getFileSystem EA-71638 - CCE: CloudFormationFileType.detectFromContent<commit_after>package com.intellij.aws.cloudformation;

import com.google.common.base.Charsets;
import com.intellij.icons.AllIcons;
import com.intellij.json.JsonLanguage;
import com.intellij.openapi.fileTypes.LanguageFileType;
import com.intellij.openapi.fileTypes.ex.FileTypeIdentifiableByVirtualFile;
import com.intellij.openapi.util.io.ByteSequence;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileSystem;
import com.intellij.openapi.vfs.newvfs.FileSystemInterface;
import com.intellij.util.Processor;
import org.apache.commons.lang.ArrayUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;

public class CloudFormationFileType extends LanguageFileType implements FileTypeIdentifiableByVirtualFile {
  public static final CloudFormationFileType INSTANCE = new CloudFormationFileType();

  private static final String EXTENSION = "template";
  private static final byte[] BYTES_TO_DETECT_CFN_FILE = CloudFormationSections.FormatVersion.getBytes(Charsets.US_ASCII);

  public CloudFormationFileType() {
    super(JsonLanguage.INSTANCE);
  }

  @NotNull
  @Override
  public String getName() {
    return "AWSCloudFormation";
  }

  @NotNull
  @Override
  public String getDescription() {
    return "AWS CloudFormation templates";
  }

  @NotNull
  @Override
  public String getDefaultExtension() {
    return "";
  }

  @Nullable
  @Override
  public Icon getIcon() {
    return AllIcons.FileTypes.Json;
  }

  @Override
  public boolean isMyFileType(@NotNull VirtualFile file) {
    return CloudFormationFileType.EXTENSION.equalsIgnoreCase(file.getExtension()) && detectFromContent(file);
  }

  private boolean detectFromContent(@NotNull VirtualFile file) {
    VirtualFileSystem virtualFileSystem;
    try {
      virtualFileSystem = file.getFileSystem();
    } catch (UnsupportedOperationException ignored) {
      return false;
    }

    if (!(virtualFileSystem instanceof FileSystemInterface)) {
      return false;
    }

    try {
      final FileSystemInterface fileSystem = (FileSystemInterface) virtualFileSystem;
      final InputStream inputStream = fileSystem.getInputStream(file);
      try {
        return FileUtil.processFirstBytes(inputStream, 1024, new Processor<ByteSequence>() {
          @Override
          public boolean process(ByteSequence byteSequence) {
            return findArray(byteSequence.getBytes(), BYTES_TO_DETECT_CFN_FILE) >= 0;
          }
        });
      } finally {
        inputStream.close();
{'fraction_non_alphanumeric': 0.09039657853810264, 'fraction_numerical': 0.0038880248833592537, 'mean_word_length': 4.580260303687636, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1567362', 'n_tokens_mistral': 1529, 'n_tokens_neox': 1470, 'n_words': 292}