com.intellij.openapi.util.io.ByteSequence Java Examples

The following examples show how to use com.intellij.openapi.util.io.ByteSequence. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Unity3dAssetFileTypeDetector.java    From consulo-unity3d with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public FileType detect(@Nonnull VirtualFile file, @Nonnull ByteSequence firstBytes, @Nullable CharSequence firstCharsIfText)
{
	CharSequence extension = FileUtil.getExtension(file.getNameSequence());

	if(ourAssetExtensions.contains(StringUtil.hashCode(extension)))
	{
		if(firstCharsIfText == null)
		{
			return Unity3dBinaryAssetFileType.INSTANCE;
		}
		if(firstCharsIfText.length() > 5)
		{
			if(StringUtil.startsWith(firstCharsIfText, "%YAML"))
			{
				return Unity3dYMLAssetFileType.INSTANCE;
			}
		}

		return Unity3dBinaryAssetFileType.INSTANCE;
	}
	return null;
}
 
Example #2
Source File: BashFileTypeDetector.java    From BashSupport with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public FileType detect(@NotNull VirtualFile file, @NotNull ByteSequence firstBytes, @Nullable CharSequence textContent) {
    return detect(file, textContent);
}
 
Example #3
Source File: BUILDFileTypeDetector.java    From intellij-pants-plugin with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public FileType detect(@NotNull VirtualFile file, @NotNull ByteSequence firstBytes, @Nullable CharSequence firstCharsIfText) {
  return PantsUtil.isBUILDFileName(file.getName()) ? PythonFileType.INSTANCE : null;
}
 
Example #4
Source File: FileTypeManagerImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
private FileType detect(@Nonnull VirtualFile file, @Nonnull byte[] bytes, int length, @Nonnull Iterable<? extends FileTypeDetector> detectors) {
  if (length <= 0) return UnknownFileType.INSTANCE;

  // use PlainTextFileType because it doesn't supply its own charset detector
  // help set charset in the process to avoid double charset detection from content
  return LoadTextUtil.processTextFromBinaryPresentationOrNull(bytes, length, file, true, true, PlainTextFileType.INSTANCE, (@Nullable CharSequence text) -> {
    if (toLog()) {
      log("F: detectFromContentAndCache.processFirstBytes(" +
          file.getName() +
          "): bytes length=" +
          length +
          "; isText=" +
          (text != null) +
          "; text='" +
          (text == null ? null : StringUtil.first(text, 100, true)) +
          "'" +
          ", detectors=" +
          detectors);
    }
    FileType detected = null;
    ByteSequence firstBytes = new ByteArraySequence(bytes, 0, length);
    for (FileTypeDetector detector : detectors) {
      try {
        detected = detector.detect(file, firstBytes, text);
      }
      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) {
      detected = text == null ? UnknownFileType.INSTANCE : PlainTextFileType.INSTANCE;
      if (toLog()) {
        log("F: detectFromContentAndCache.processFirstBytes(" + file.getName() + "): " + "no detector was able to detect. assigned " + detected.getName());
      }
    }
    return detected;
  });
}
 
Example #5
Source File: FileTypeRegistry.java    From consulo with Apache License 2.0 2 votes vote down vote up
/**
 * Detects file type by its content
 *
 * @param file             to analyze
 * @param firstBytes       of the file for identifying its file type
 * @param firstCharsIfText - characters, converted from first bytes parameter if the file content was determined to be text, or null otherwise
 * @return detected file type, or null if was unable to detect
 */
@Nullable
FileType detect(@Nonnull VirtualFile file, @Nonnull ByteSequence firstBytes, @Nullable CharSequence firstCharsIfText);