CharArrayBuffer implements CharSequence, Serializable { public void setLength(final int len) { if (len < 0 || len > this.array.length) { throw new IndexOutOfBoundsException("len: "+len+" < 0 or > buffer len: "+this.array.length); } this.len = len; } CharArrayBuffer(final int capacity); void append(final char[] b, final int off, final int len); void append(final String str); void append(final CharArrayBuffer b, final int off, final int len); void append(final CharArrayBuffer b); void append(final char ch); void append(final byte[] b, final int off, final int len); void append(final ByteArrayBuffer b, final int off, final int len); void append(final Object obj); void clear(); char[] toCharArray(); @Override char charAt(final int i); char[] array(); int capacity(); @Override int length(); void ensureCapacity(final int required); void setLength(final int len); boolean isEmpty(); boolean isFull(); int indexOf(final int ch, final int from, final int to); int indexOf(final int ch); String substring(final int beginIndex, final int endIndex); String substringTrimmed(final int beginIndex, final int endIndex); @Override CharSequence subSequence(final int beginIndex, final int endIndex); @Override String toString();  }
@Test public void testSetLength() throws Exception { final CharArrayBuffer buffer = new CharArrayBuffer(4); buffer.setLength(2); Assert.assertEquals(2, buffer.length()); }
@Test public void testSetInvalidLength() throws Exception { final CharArrayBuffer buffer = new CharArrayBuffer(4); try { buffer.setLength(-2); Assert.fail("IndexOutOfBoundsException should have been thrown"); } catch (final IndexOutOfBoundsException ex) { } try { buffer.setLength(200); Assert.fail("IndexOutOfBoundsException should have been thrown"); } catch (final IndexOutOfBoundsException ex) { } }