@Test
public void testPutString() throws Exception {
CharsetEncoder encoder;
IoBuffer buf = IoBuffer.allocate(16);
encoder = Charset.forName("ISO-8859-1").newEncoder();
buf.putString("ABC", encoder);
assertEquals(3, buf.position());
buf.clear();
assertEquals('A', buf.get(0));
assertEquals('B', buf.get(1));
assertEquals('C', buf.get(2));
buf.putString("D", 5, encoder);
assertEquals(5, buf.position());
buf.clear();
assertEquals('D', buf.get(0));
assertEquals(0, buf.get(1));
buf.putString("EFG", 2, encoder);
assertEquals(2, buf.position());
buf.clear();
assertEquals('E', buf.get(0));
assertEquals('F', buf.get(1));
assertEquals('C', buf.get(2)); // C may not be overwritten
// UTF-16: We specify byte order to omit BOM.
encoder = Charset.forName("UTF-16BE").newEncoder();
buf.clear();
buf.putString("ABC", encoder);
assertEquals(6, buf.position());
buf.clear();
assertEquals(0, buf.get(0));
assertEquals('A', buf.get(1));
assertEquals(0, buf.get(2));
assertEquals('B', buf.get(3));
assertEquals(0, buf.get(4));
assertEquals('C', buf.get(5));
buf.putString("D", 10, encoder);
assertEquals(10, buf.position());
buf.clear();
assertEquals(0, buf.get(0));
assertEquals('D', buf.get(1));
assertEquals(0, buf.get(2));
assertEquals(0, buf.get(3));
buf.putString("EFG", 4, encoder);
assertEquals(4, buf.position());
buf.clear();
assertEquals(0, buf.get(0));
assertEquals('E', buf.get(1));
assertEquals(0, buf.get(2));
assertEquals('F', buf.get(3));
assertEquals(0, buf.get(4)); // C may not be overwritten
assertEquals('C', buf.get(5)); // C may not be overwritten
// Test putting an emptry string
buf.putString("", encoder);
assertEquals(0, buf.position());
buf.putString("", 4, encoder);
assertEquals(4, buf.position());
assertEquals(0, buf.get(0));
assertEquals(0, buf.get(1));
}