/** * 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; }
/** * 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; }
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; }
/** * <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; } }
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; }
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; }
@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); }
@Override public void configure(Configuration parameters) { super.configure(parameters); if (charsetName == null || !Charset.isSupported(charsetName)) { throw new RuntimeException("Unsupported charset: " + charsetName); } }
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; }
private static void checkArgumentCharsetIsSupported(String charsetName) { checkArgument(Charset.isSupported(charsetName), "Charset:<'%s'> is not supported on this system", charsetName); }
private static void checkArgumentCharsetIsSupported(String charsetName) { checkArgument(Charset.isSupported(charsetName), "Charset:<'%s'> is not supported on this system", charsetName); }
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"; }
/** * 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); }
/** * 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)); }
private boolean jvmAndSaxBothSupportCharset(final String charSetName) throws ParserConfigurationException, SAXException, IOException { return Charset.isSupported(charSetName) && doesSaxSupportCharacterSet(charSetName); }
/** * 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); }
@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)); }
@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)); }
@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(); }
@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(); }