UnsyncCharArrayWriter extends Writer { public int writeTo(CharBuffer charBuffer) { int length = charBuffer.remaining(); if (length > _index) { length = _index; } if (length == 0) { return 0; } charBuffer.put(_buffer, 0, length); return length; } UnsyncCharArrayWriter();  UnsyncCharArrayWriter(int initialSize); @Override Writer append(char c); @Override Writer append(CharSequence charSequence); @Override Writer append(CharSequence charSequence, int start, int end); @Override void close(); @Override void flush(); void reset(); int size(); CharBuffer toCharBuffer(); @Override String toString(); @Override void write(char[] chars); @Override void write(char[] chars, int offset, int length); @Override void write(int c); @Override void write(String string); @Override void write(String string, int offset, int length); int writeTo(CharBuffer charBuffer); int writeTo(OutputStream outputStream, String charsetName); int writeTo(Writer writer);  }
@Test public void testWriteTo() throws IOException { UnsyncCharArrayWriter unsyncCharArrayWriter = new UnsyncCharArrayWriter(); unsyncCharArrayWriter.write("abcd"); CharBuffer charBuffer = CharBuffer.allocate(2); int length = unsyncCharArrayWriter.writeTo(charBuffer); Assert.assertEquals(2, length); Assert.assertEquals(2, charBuffer.position()); Assert.assertEquals(2, charBuffer.limit()); charBuffer.position(0); Assert.assertEquals("ab", charBuffer.toString()); charBuffer = CharBuffer.allocate(5); length = unsyncCharArrayWriter.writeTo(charBuffer); Assert.assertEquals(4, length); Assert.assertEquals(4, charBuffer.position()); Assert.assertEquals(5, charBuffer.limit()); charBuffer.position(0); charBuffer.limit(4); Assert.assertEquals("abcd", charBuffer.toString()); charBuffer = CharBuffer.allocate(0); length = unsyncCharArrayWriter.writeTo(charBuffer); Assert.assertEquals(0, length); Assert.assertEquals(0, charBuffer.position()); Assert.assertEquals(0, charBuffer.limit()); charBuffer.position(0); Assert.assertEquals("", charBuffer.toString()); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); length = unsyncCharArrayWriter.writeTo( byteArrayOutputStream, StringPool.UTF8); Assert.assertEquals(4, length); Assert.assertEquals(4, byteArrayOutputStream.size()); Assert.assertTrue( Arrays.equals( "abcd".getBytes(), byteArrayOutputStream.toByteArray())); StringWriter stringWriter = new StringWriter(); length = unsyncCharArrayWriter.writeTo(stringWriter); Assert.assertEquals(4, length); Assert.assertEquals("abcd", stringWriter.toString()); }