@Test
public void testPutPrefixedString() throws Exception {
CharsetEncoder encoder;
IoBuffer buf = IoBuffer.allocate(16);
buf.fillAndReset(buf.remaining());
encoder = Charset.forName("ISO-8859-1").newEncoder();
// Without autoExpand
buf.putPrefixedString("ABC", encoder);
assertEquals(5, buf.position());
assertEquals(0, buf.get(0));
assertEquals(3, buf.get(1));
assertEquals('A', buf.get(2));
assertEquals('B', buf.get(3));
assertEquals('C', buf.get(4));
buf.clear();
try {
buf.putPrefixedString("123456789012345", encoder);
fail();
}
catch (BufferOverflowException e) {
// Expected an Exception, signifies test success
assertTrue(true);
}
// With autoExpand
buf.clear();
buf.setAutoExpand(true);
buf.putPrefixedString("123456789012345", encoder);
assertEquals(17, buf.position());
assertEquals(0, buf.get(0));
assertEquals(15, buf.get(1));
assertEquals('1', buf.get(2));
assertEquals('2', buf.get(3));
assertEquals('3', buf.get(4));
assertEquals('4', buf.get(5));
assertEquals('5', buf.get(6));
assertEquals('6', buf.get(7));
assertEquals('7', buf.get(8));
assertEquals('8', buf.get(9));
assertEquals('9', buf.get(10));
assertEquals('0', buf.get(11));
assertEquals('1', buf.get(12));
assertEquals('2', buf.get(13));
assertEquals('3', buf.get(14));
assertEquals('4', buf.get(15));
assertEquals('5', buf.get(16));
}