Java Code Examples for com.intellij.openapi.util.Pair.createNonNull()

The following are Jave code examples for showing how to use createNonNull() of the com.intellij.openapi.util.Pair class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: intellij-ce-playground   File: LoadTextUtil.java   Source Code and License Vote up 6 votes
@NotNull
private static Pair.NonNull<Charset,byte[]> getCharsetAndBOM(@NotNull byte[] content, @NotNull Charset charset) {
  if (charset.name().contains(CharsetToolkit.UTF8) && CharsetToolkit.hasUTF8Bom(content)) {
    return Pair.createNonNull(charset, CharsetToolkit.UTF8_BOM);
  }
  try {
    Charset fromBOM = CharsetToolkit.guessFromBOM(content);
    if (fromBOM != null) {
      return Pair.createNonNull(fromBOM, ObjectUtils.notNull(CharsetToolkit.getMandatoryBom(fromBOM), ArrayUtil.EMPTY_BYTE_ARRAY));
    }
  }
  catch (UnsupportedCharsetException ignore) {
  }

  return Pair.createNonNull(charset, ArrayUtil.EMPTY_BYTE_ARRAY);
}
 
Example 2
Project: intellij-ce-playground   File: LoadTextUtil.java   Source Code and License Vote up 6 votes
@NotNull
private static Pair.NonNull<Charset, byte[]> charsetForWriting(@Nullable Project project,
                                                       @NotNull VirtualFile virtualFile,
                                                       @NotNull String text,
                                                       @NotNull Charset existing) {
  Charset specified = extractCharsetFromFileContent(project, virtualFile, text);
  Pair.NonNull<Charset, byte[]> chosen = chooseMostlyHarmlessCharset(existing, specified, text);
  Charset charset = chosen.first;

  // in case of "UTF-16", OutputStreamWriter sometimes adds BOM on it's own.
  // see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6800103
  byte[] bom = virtualFile.getBOM();
  Charset fromBom = bom == null ? null : CharsetToolkit.guessFromBOM(bom);
  if (fromBom != null && !fromBom.equals(charset)) {
    chosen = Pair.createNonNull(fromBom, toBytes(text, fromBom));
  }
  return chosen;
}
 
Example 3
Project: intellij-ce-playground   File: LoadTextUtil.java   Source Code and License Vote up 6 votes
@NotNull
public static Pair.NonNull<Charset, byte[]> chooseMostlyHarmlessCharset(@NotNull Charset existing, @NotNull Charset specified, @NotNull String text) {
  try {
    if (specified.equals(existing)) {
      return Pair.createNonNull(specified, toBytes(text, existing));
    }

    byte[] out = isSupported(specified, text);
    if (out != null) {
      return Pair.createNonNull(specified, out); //if explicitly specified encoding is safe, return it
    }
    out = isSupported(existing, text);
    if (out != null) {
      return Pair.createNonNull(existing, out);   //otherwise stick to the old encoding if it's ok
    }
    return Pair.createNonNull(specified, toBytes(text, specified)); //if both are bad there is no difference
  }
  catch (RuntimeException e) {
    return Pair.createNonNull(Charset.defaultCharset(), toBytes(text, null)); //if both are bad and there is no hope, use the default charset
  }
}