Java Code Examples for com.intellij.util.ArrayUtil#startsWith()

The following examples show how to use com.intellij.util.ArrayUtil#startsWith() . 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: ModulesAndLibrariesSourceItemsProvider.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static Collection<? extends PackagingSourceItem> createModuleItems(ArtifactEditorContext editorContext, Artifact artifact,
                                                                           @Nonnull String[] groupPath) {
  final Module[] modules = editorContext.getModulesProvider().getModules();
  final List<PackagingSourceItem> items = new ArrayList<PackagingSourceItem>();
  Set<String> groups = new HashSet<String>();
  for (Module module : modules) {
    String[] path = ModuleManager.getInstance(editorContext.getProject()).getModuleGroupPath(module);
    if (path == null) {
      path = ArrayUtil.EMPTY_STRING_ARRAY;
    }

    if (Comparing.equal(path, groupPath)) {
      items.add(new ModuleSourceItemGroup(module));
    }
    else if (ArrayUtil.startsWith(path, groupPath)) {
      groups.add(path[groupPath.length]);
    }
  }
  for (String group : groups) {
    items.add(0, new ModuleGroupItem(ArrayUtil.append(groupPath, group)));
  }
  return items;
}
 
Example 2
Source File: EncodingUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
static Magic8 isSafeToReloadIn(@Nonnull VirtualFile virtualFile, @Nonnull CharSequence text, @Nonnull byte[] bytes, @Nonnull Charset charset) {
  // file has BOM but the charset hasn't
  byte[] bom = virtualFile.getBOM();
  if (bom != null && !CharsetToolkit.canHaveBom(charset, bom)) return Magic8.NO_WAY;

  // the charset has mandatory BOM (e.g. UTF-xx) but the file hasn't or has wrong
  byte[] mandatoryBom = CharsetToolkit.getMandatoryBom(charset);
  if (mandatoryBom != null && !ArrayUtil.startsWith(bytes, mandatoryBom)) return Magic8.NO_WAY;

  String loaded = LoadTextUtil.getTextByBinaryPresentation(bytes, charset).toString();

  String separator = FileDocumentManager.getInstance().getLineSeparator(virtualFile, null);
  String toSave = StringUtil.convertLineSeparators(loaded, separator);

  LoadTextUtil.AutoDetectionReason failReason = LoadTextUtil.getCharsetAutoDetectionReason(virtualFile);
  if (failReason != null && StandardCharsets.UTF_8.equals(virtualFile.getCharset()) && !StandardCharsets.UTF_8.equals(charset)) {
    return Magic8.NO_WAY; // can't reload utf8-autodetected file in another charset
  }

  byte[] bytesToSave;
  try {
    bytesToSave = toSave.getBytes(charset);
  }
  // turned out some crazy charsets have incorrectly implemented .newEncoder() returning null
  catch (UnsupportedOperationException | NullPointerException e) {
    return Magic8.NO_WAY;
  }
  if (bom != null && !ArrayUtil.startsWith(bytesToSave, bom)) {
    bytesToSave = ArrayUtil.mergeArrays(bom, bytesToSave); // for 2-byte encodings String.getBytes(Charset) adds BOM automatically
  }

  return !Arrays.equals(bytesToSave, bytes) ? Magic8.NO_WAY : StringUtil.equals(loaded, text) ? Magic8.ABSOLUTELY : Magic8.WELL_IF_YOU_INSIST;
}
 
Example 3
Source File: XmlCharsetDetector.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static String extractXmlEncodingFromProlog(final byte[] bytes) {
  int index = 0;
  if (CharsetToolkit.hasUTF8Bom(bytes)) {
    index = CharsetToolkit.UTF8_BOM.length;
  }

  index = skipWhiteSpace(index, bytes);
  if (!ArrayUtil.startsWith(bytes, index, XML_PROLOG_START_BYTES)) return null;
  index += XML_PROLOG_START_BYTES.length;
  while (index < bytes.length) {
    index = skipWhiteSpace(index, bytes);
    if (ArrayUtil.startsWith(bytes, index, XML_PROLOG_END_BYTES)) return null;
    if (ArrayUtil.startsWith(bytes, index, ENCODING_BYTES)) {
      index += ENCODING_BYTES.length;
      index = skipWhiteSpace(index, bytes);
      if (index >= bytes.length || bytes[index] != '=') continue;
      index++;
      index = skipWhiteSpace(index, bytes);
      if (index >= bytes.length || bytes[index] != '\'' && bytes[index] != '\"') continue;
      byte quote = bytes[index];
      index++;
      StringBuilder encoding = new StringBuilder();
      while (index < bytes.length) {
        if (bytes[index] == quote) return encoding.toString();
        encoding.append((char)bytes[index++]);
      }
    }
    index++;
  }
  return null;
}
 
Example 4
Source File: CharsetToolkit.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static boolean hasUTF32BEBom(@Nonnull byte[] bom) {
  return ArrayUtil.startsWith(bom, UTF32BE_BOM);
}
 
Example 5
Source File: CharsetToolkit.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static boolean hasUTF32LEBom(@Nonnull byte[] bom) {
  return ArrayUtil.startsWith(bom, UTF32LE_BOM);
}
 
Example 6
Source File: CharsetToolkit.java    From consulo with Apache License 2.0 2 votes vote down vote up
/**
 * Has a Byte Order Marker for UTF-8 (Used by Microsoft's Notepad and other editors).
 *
 * @param bom a buffer.
 * @return true if the buffer has a BOM for UTF8.
 */
public static boolean hasUTF8Bom(@Nonnull byte[] bom) {
  return ArrayUtil.startsWith(bom, UTF8_BOM);
}
 
Example 7
Source File: CharsetToolkit.java    From consulo with Apache License 2.0 2 votes vote down vote up
/**
 * Has a Byte Order Marker for UTF-16 Low Endian
 * (ucs-2le, ucs-4le, and ucs-16le).
 *
 * @param bom a buffer.
 * @return true if the buffer has a BOM for UTF-16 Low Endian.
 */
public static boolean hasUTF16LEBom(@Nonnull byte[] bom) {
  return ArrayUtil.startsWith(bom, UTF16LE_BOM);
}
 
Example 8
Source File: CharsetToolkit.java    From consulo with Apache License 2.0 2 votes vote down vote up
/**
 * Has a Byte Order Marker for UTF-16 Big Endian
 * (utf-16 and ucs-2).
 *
 * @param bom a buffer.
 * @return true if the buffer has a BOM for UTF-16 Big Endian.
 */
public static boolean hasUTF16BEBom(@Nonnull byte[] bom) {
  return ArrayUtil.startsWith(bom, UTF16BE_BOM);
}