CustomizableByteArrayOutputStream extends OutputStream { @Override @Synchronized public void write(int byteToWrite) { ensureCapacity(this.count + 1); this.buffer[count] = (byte) byteToWrite; this.count += 1; } CustomizableByteArrayOutputStream(int initialCapacity, int maxArraySize, double fillFactorToCopy); @Override @Synchronized void write(int byteToWrite); @Override @Synchronized void write(byte[] buffer, int off, int len); @Synchronized byte[] getBufferOrCopy(); @Synchronized int size(); @Override void close(); @Synchronized void reset();  }
@Test void writeSingleByteNoGrow() { int written = 10; tested.write(written); assertThat(tested.buffer).hasSize(1); assertThat(tested.buffer).containsExactly(written); }
@Test void writeSingleByteGrows() { int written1 = 10; int written2 = 20; tested.write(written1); tested.write(written2); assertThat(tested.buffer).hasSize(2); assertThat(tested.buffer).containsExactly(written1, written2); }
@Test void writeSingleByteGrowsExponentially() { int written1 = 10; int written2 = 20; int written3 = 20; tested.write(written1); tested.write(written2); tested.write(written3); assertThat(tested.buffer).hasSize(4); assertThat(tested.count).isEqualTo(3); assertThat(tested.buffer).startsWith(written1, written2, written3); }
@Test void writeSingleArrayNoGrow() { byte[] written = {10}; tested.write(written, 0, written.length); assertThat(tested.buffer).hasSize(1); assertThat(tested.count).isEqualTo(1); assertThat(tested.buffer).containsExactly(written); }
@Test void writeSingleArrayGrows() { byte[] written = {10, 20}; tested.write(written, 0, written.length); assertThat(tested.buffer).hasSize(2); assertThat(tested.count).isEqualTo(2); assertThat(tested.buffer).containsExactly(written); }
@Test void writeSingleArrayMatchesWrittenSizeOnOverflow() { byte[] written = {10, 20, 30}; tested.write(written, 0, written.length); assertThat(tested.buffer).hasSize(3); assertThat(tested.count).isEqualTo(3); assertThat(tested.buffer).startsWith(written); }
@Test void writeSingleArrayGrowsExponentially() { byte[] written1 = {10, 20}; byte[] written2 = {30}; tested.write(written1, 0, written1.length); tested.write(written2, 0, written2.length); assertThat(tested.buffer).hasSize(4); assertThat(tested.count).isEqualTo(3); assertThat(tested.buffer).startsWith(10, 20, 30); }