List of usage examples for org.apache.commons.lang3 CharEncoding US_ASCII
String US_ASCII
To view the source code for org.apache.commons.lang3 CharEncoding US_ASCII.
Click Source Link
Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set.
Every implementation of the Java platform is required to support this character encoding.
From source file:com.silverpeas.ical.StringUtils.java
static byte[] encodeArray(char[] chars, Charset encoding) throws CharacterCodingException { if (CharEncoding.US_ASCII.equals(encoding.name())) { byte[] array = new byte[chars.length]; for (int i = 0; i < array.length; i++) { array[i] = (byte) chars[i]; }//from ww w . jav a 2s . c om return array; } ByteBuffer buffer = encoding.newEncoder().encode(CharBuffer.wrap(chars)); byte[] array = new byte[buffer.limit()]; System.arraycopy(buffer.array(), 0, array, 0, array.length); return array; }
From source file:com.norconex.commons.lang.io.ByteArrayOutputStreamTest.java
@Test public void testByteArrayOutputStream() throws IOException { String val1 = "0123456789"; String val2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; byte[] b = null; ByteArrayOutputStream out = new ByteArrayOutputStream(5); String enc = CharEncoding.US_ASCII; // test nothing written b = new byte[5]; Assert.assertEquals("no-write-yet-getByte", -1, out.getByte(5)); Assert.assertEquals("no-write-yet-getBytes", -1, out.getBytes(b, 0)); // test getByte() out.write(val1.getBytes(enc)); Assert.assertEquals("getByte-0", '0', (char) out.getByte(0)); Assert.assertEquals("getByte-5", '5', (char) out.getByte(5)); Assert.assertEquals("getByte-9", '9', (char) out.getByte(9)); Assert.assertEquals("getByte-10", -1, out.getByte(10)); Assert.assertEquals("val1-count", 10, out.size()); // test getBytes() b = new byte[6]; out.getBytes(b, 0);//from w w w .j a va 2 s . c om Assert.assertEquals("getBytes-012345", "012345", new String(b, enc)); b = new byte[3]; out.getBytes(b, 6); Assert.assertEquals("getBytes-678", "678", new String(b, enc)); b = new byte[3]; out.getBytes(b, 6); Assert.assertEquals("getBytes-678", "678", new String(b, enc)); b = new byte[12]; int read = out.getBytes(b, 8); Assert.assertEquals("getBytes-89", "89", new String(b, 0, read, enc)); out.write(val2.getBytes(enc)); out.getBytes(b, 8); Assert.assertEquals("getBytes-89ABCDEFGHIJ", "89ABCDEFGHIJ", new String(b, enc)); // toByteArray() Assert.assertEquals("toByteArray", val1 + val2, new String(out.toByteArray(), enc)); out.close(); }
From source file:com.silverpeas.ical.StringUtils.java
static char[] decodeToArray(byte[] bytes, Charset encoding) { if (CharEncoding.US_ASCII.equals(encoding.name())) { char[] array = new char[bytes.length]; for (int i = 0; i < array.length; i++) { array[i] = (char) bytes[i]; }//from ww w . j a v a2 s . co m return array; } try { CharBuffer buffer = encoding.newDecoder().decode(ByteBuffer.wrap(bytes)); char[] array = new char[buffer.limit()]; System.arraycopy(buffer.array(), 0, array, 0, array.length); return array; } catch (Exception nioException) { return (new String(bytes, encoding)).toCharArray(); } }
From source file:com.norconex.commons.lang.io.CachedInputStreamTest.java
@Test public void testMarking() throws IOException { String content = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; ByteArrayInputStream is = new ByteArrayInputStream(content.getBytes()); CachedStreamFactory factory = new CachedStreamFactory(200, 100); CachedInputStream cache = factory.newInputStream(is); try {/*from w w w . j a v a 2 s.c o m*/ String enc = CharEncoding.US_ASCII; byte[] bytes = null; bytes = new byte[5]; cache.read(bytes); Assert.assertEquals("01234", new String(bytes, enc)); cache.mark(999); cache.read(bytes); Assert.assertEquals("56789", new String(bytes, enc)); cache.reset(); bytes = new byte[7]; cache.read(bytes); Assert.assertEquals("56789AB", new String(bytes, enc)); } finally { IOUtils.closeQuietly(cache); cache.dispose(); } }