java.nio.charset.Charset.isSupported()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(9.8k)|赞(0)|评价(0)|浏览(119)

本文整理了Java中java.nio.charset.Charset.isSupported()方法的一些代码示例,展示了Charset.isSupported()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Charset.isSupported()方法的具体详情如下:
包路径:java.nio.charset.Charset
类名称:Charset
方法名:isSupported

Charset.isSupported介绍

[英]Determines whether the specified charset is supported by this runtime.
[中]确定此运行时是否支持指定的字符集。

代码示例

代码示例来源:origin: checkstyle/checkstyle

/**
 * Set the charset to use for loading the header from a file.
 * @param charset the charset to use for loading the header from a file
 * @throws UnsupportedEncodingException if charset is unsupported
 */
public void setCharset(String charset) throws UnsupportedEncodingException {
  if (!Charset.isSupported(charset)) {
    final String message = "unsupported charset: '" + charset + "'";
    throw new UnsupportedEncodingException(message);
  }
  this.charset = charset;
}

代码示例来源:origin: checkstyle/checkstyle

/**
 * Sets a named charset.
 * @param charset the name of a charset
 * @throws UnsupportedEncodingException if charset is unsupported.
 */
public void setCharset(String charset)
    throws UnsupportedEncodingException {
  if (!Charset.isSupported(charset)) {
    final String message = "unsupported charset: '" + charset + "'";
    throw new UnsupportedEncodingException(message);
  }
  this.charset = charset;
}

代码示例来源:origin: code4craft/webmagic

public static String getCharset(String contentType) {
  Matcher matcher = patternForCharset.matcher(contentType);
  if (matcher.find()) {
    String charset = matcher.group(1);
    if (Charset.isSupported(charset)) {
      return charset;
    }
  }
  return null;
}

代码示例来源:origin: org.apache.commons/commons-lang3

/**
 * <p>Returns whether the named charset is supported.</p>
 *
 * <p>This is similar to <a
 * href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html#isSupported%28java.lang.String%29">
 * java.nio.charset.Charset.isSupported(String)</a> but handles more formats</p>
 *
 * @param name  the name of the requested charset; may be either a canonical name or an alias, null returns false
 * @return {@code true} if the charset is available in the current Java virtual machine
 * @deprecated Please use {@link Charset#isSupported(String)} instead, although be aware that {@code null}
 * values are not accepted by that method and an {@link IllegalCharsetNameException} may be thrown.
 */
@Deprecated
public static boolean isSupported(final String name) {
  if (name == null) {
    return false;
  }
  try {
    return Charset.isSupported(name);
  } catch (final IllegalCharsetNameException ex) {
    return false;
  }
}

代码示例来源:origin: perwendel/spark

public static String toString(byte[] bytes, String encoding) {
  String str;
  if (encoding != null && Charset.isSupported(encoding)) {
    try {
      str = new String(bytes, encoding);
    } catch (UnsupportedEncodingException e) {
      // Uses same func as Charset.isSupported (cannot happen)
      str = new String(bytes);
    }
  } else {
    str = new String(bytes);
  }
  return str;
}

代码示例来源:origin: org.jsoup/jsoup

private static String validateCharset(String cs) {
  if (cs == null || cs.length() == 0) return null;
  cs = cs.trim().replaceAll("[\"']", "");
  try {
    if (Charset.isSupported(cs)) return cs;
    cs = cs.toUpperCase(Locale.ENGLISH);
    if (Charset.isSupported(cs)) return cs;
  } catch (IllegalCharsetNameException e) {
    // if our this charset matching fails.... we just take the default
  }
  return null;
}

代码示例来源:origin: apache/flink

@Override
public void configure(Configuration parameters) {
  super.configure(parameters);
  if (charsetName == null || !Charset.isSupported(charsetName)) {
    throw new RuntimeException("Unsupported charset: " + charsetName);
  }
  if (charsetName.equalsIgnoreCase(StandardCharsets.US_ASCII.name())) {
    ascii = true;
  }
  this.decoder = Charset.forName(charsetName).newDecoder();
  this.byteWrapper = ByteBuffer.allocate(1);
}

代码示例来源:origin: apache/flink

@Override
public void configure(Configuration parameters) {
  super.configure(parameters);
  if (charsetName == null || !Charset.isSupported(charsetName)) {
    throw new RuntimeException("Unsupported charset: " + charsetName);
  }
}

代码示例来源:origin: apache/flink

public void setCharsetName(String charsetName) throws IllegalCharsetNameException, UnsupportedCharsetException {
  if (charsetName == null) {
    throw new NullPointerException();
  }
  if (!Charset.isSupported(charsetName)) {
    throw new UnsupportedCharsetException("The charset " + charsetName + " is not supported.");
  }
  this.charsetName = charsetName;
}

代码示例来源:origin: org.assertj/assertj-core

private static void checkArgumentCharsetIsSupported(String charsetName) {
 checkArgument(Charset.isSupported(charsetName), "Charset:<'%s'> is not supported on this system", charsetName);
}

代码示例来源:origin: org.assertj/assertj-core

private static void checkArgumentCharsetIsSupported(String charsetName) {
 checkArgument(Charset.isSupported(charsetName), "Charset:<'%s'> is not supported on this system", charsetName);
}

代码示例来源:origin: k9mail/k-9

public static String getCharsetFromAddress(String address) {
  String variant = JisSupport.getJisVariantFromAddress(address);
  if (variant != null) {
    String charset = "x-" + variant + "-shift_jis-2007";
    if (Charset.isSupported(charset))
      return charset;
  }
  return "UTF-8";
}

代码示例来源:origin: stanfordnlp/CoreNLP

/**
 * Uses a MemoryTreebank with a CHTBTokenizer and a
 * BobChrisTreeNormalizer
 */
@Override
public MemoryTreebank memoryTreebank() {
 String encoding = inputEncoding;
 if (!java.nio.charset.Charset.isSupported(encoding)) {
  System.out.println("Warning: desired encoding " + encoding + " not accepted. ");
  System.out.println("Using UTF-8 to construct MemoryTreebank");
  encoding = "UTF-8";
 }
 return new MemoryTreebank(treeReaderFactory(), encoding);
}

代码示例来源:origin: org.assertj/assertj-core

/**
 * Specifies the name of the charset to use for text-based assertions on the file's contents.
 *
 * @param charsetName the name of the charset to use.
 * @return {@code this} assertion object.
 * @throws IllegalArgumentException if the given encoding is not supported on this platform.
 */
@CheckReturnValue
public SELF usingCharset(String charsetName) {
 checkArgument(Charset.isSupported(charsetName), "Charset:<'%s'> is not supported on this system", charsetName);
 return usingCharset(Charset.forName(charsetName));
}

代码示例来源:origin: commons-io/commons-io

private boolean jvmAndSaxBothSupportCharset(final String charSetName) throws ParserConfigurationException, SAXException, IOException {
  return Charset.isSupported(charSetName) &&  doesSaxSupportCharacterSet(charSetName);
}

代码示例来源:origin: stanfordnlp/CoreNLP

/**
 * Uses a DiskTreebank with a CHTBTokenizer and a
 * BobChrisTreeNormalizer.
 */
@Override
public DiskTreebank diskTreebank() {
 String encoding = inputEncoding;
 if (!java.nio.charset.Charset.isSupported(encoding)) {
  printlnErr("Warning: desired encoding " + encoding + " not accepted. ");
  printlnErr("Using UTF-8 to construct DiskTreebank");
  encoding = "UTF-8";
 }
 return new DiskTreebank(treeReaderFactory(), encoding);
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testReadXmlWithBOMUcs2() throws Exception {
  Assume.assumeFalse("This test does not pass on some IBM VMs xml parsers", System.getProperty("java.vendor").contains("IBM"));
  // UCS-2 is BE.
  Assume.assumeTrue(Charset.isSupported("ISO-10646-UCS-2"));
  final byte[] data = "<?xml version=\"1.0\" encoding=\"ISO-10646-UCS-2\"?><X/>".getBytes("ISO-10646-UCS-2");
  parseXml(new BOMInputStream(createUtf16BeDataStream(data, true), ByteOrderMark.UTF_16BE));
  parseXml(createUtf16BeDataStream(data, true));
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testReadXmlWithBOMUcs4() throws Exception {
  // UCS-4 is BE or LE?
  // Hm: ISO-10646-UCS-4 is not supported on Oracle 1.6.0_31
  Assume.assumeTrue(Charset.isSupported("ISO-10646-UCS-4"));
  final byte[] data = "<?xml version=\"1.0\" encoding=\"ISO-10646-UCS-4\"?><X/>".getBytes("ISO-10646-UCS-4");
  // XML parser does not know what to do with UTF-32
  parseXml(new BOMInputStream(createUtf32BeDataStream(data, true), ByteOrderMark.UTF_32BE));
  // XML parser does not know what to do with UTF-32
  Assume.assumeTrue("JVM and SAX need to support UTF_32LE for this", jvmAndSaxBothSupportCharset("UTF_32LE"));
  parseXml(createUtf32BeDataStream(data, true));
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testReadWithBOMUtf32Be() throws Exception {
  Assume.assumeTrue(Charset.isSupported("UTF_32BE"));
  final byte[] data = "ABC".getBytes("UTF_32BE");
  final BOMInputStream in = new BOMInputStream(createUtf32BeDataStream(data, true), ByteOrderMark.UTF_32BE);
  assertEquals(0, in.read());
  assertEquals(0, in.read());
  assertEquals(0, in.read());
  assertEquals('A', in.read());
  assertEquals(0, in.read());
  assertEquals(0, in.read());
  assertEquals(0, in.read());
  assertEquals('B', in.read());
  assertEquals(0, in.read());
  assertEquals(0, in.read());
  assertEquals(0, in.read());
  assertEquals('C', in.read());
  assertEquals(-1, in.read());
  assertTrue("hasBOM()", in.hasBOM());
  assertTrue("hasBOM(UTF-32BE)", in.hasBOM(ByteOrderMark.UTF_32BE));
  assertEquals("getBOM", ByteOrderMark.UTF_32BE, in.getBOM());
  try {
    in.hasBOM(ByteOrderMark.UTF_32LE);
    fail("Expected IllegalArgumentException");
  } catch (final IllegalArgumentException e) {
    // expected - not configured for UTF-32LE
  }
  in.close();
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testReadWithBOMUtf32Le() throws Exception {
  Assume.assumeTrue(Charset.isSupported("UTF_32LE"));
  final byte[] data = "ABC".getBytes("UTF_32LE");
  final BOMInputStream in = new BOMInputStream(createUtf32LeDataStream(data, true), ByteOrderMark.UTF_32LE);
  assertEquals('A', in.read());
  assertEquals(0, in.read());
  assertEquals(0, in.read());
  assertEquals(0, in.read());
  assertEquals('B', in.read());
  assertEquals(0, in.read());
  assertEquals(0, in.read());
  assertEquals(0, in.read());
  assertEquals('C', in.read());
  assertEquals(0, in.read());
  assertEquals(0, in.read());
  assertEquals(0, in.read());
  assertEquals(-1, in.read());
  assertTrue("hasBOM()", in.hasBOM());
  assertTrue("hasBOM(UTF-32LE)", in.hasBOM(ByteOrderMark.UTF_32LE));
  assertEquals("getBOM", ByteOrderMark.UTF_32LE, in.getBOM());
  try {
    in.hasBOM(ByteOrderMark.UTF_32BE);
    fail("Expected IllegalArgumentException");
  } catch (final IllegalArgumentException e) {
    // expected - not configured for UTF-32BE
  }
  in.close();
}

相关文章