### Question:
LinkedBufferOutput extends BufferedOutput { public byte[] toByteArray() { byte[] bytes = new byte[size + filled]; int off = 0; for (Link l : link) { System.arraycopy(l.buffer, l.offset, bytes, off, l.size); off += l.size; } if (filled > 0) { System.arraycopy(buffer, 0, bytes, off, filled); } return bytes; } LinkedBufferOutput(int bufferSize); byte[] toByteArray(); int getSize(); void clear(); @Override void close();  }### Answer:
@Test public void testWritePrimitives() throws IOException { ByteArrayOutputStream bo = new ByteArrayOutputStream(); DataOutputStream o1 = new DataOutputStream(bo); LinkedBufferOutput o2 = new LinkedBufferOutput(10); o1.writeByte((byte)2); o2.writeByte((byte)2); o1.writeShort((short)2); o2.writeShort((short)2); o1.writeInt(2); o2.writeInt(2); o1.writeLong(2L); o2.writeLong(2L); o1.writeFloat(1.1f); o2.writeFloat(1.1f); o1.writeDouble(1.1); o2.writeDouble(1.1); byte[] b1 = bo.toByteArray(); byte[] b2 = o2.toByteArray(); assertEquals(b1.length, b2.length); assertArrayEquals(b1, b2); }
@Test public void testWriteByteAndPrimitives() throws IOException { ByteArrayOutputStream bo = new ByteArrayOutputStream(); DataOutputStream o1 = new DataOutputStream(bo); LinkedBufferOutput o2 = new LinkedBufferOutput(10); o1.writeByte((byte)9); o1.writeByte((byte)2); o2.writeByteAndByte((byte)9, (byte)2); o1.writeByte((byte)9); o1.writeShort((short)2); o2.writeByteAndShort((byte)9, (short)2); o1.writeByte((byte)9); o1.writeInt(2); o2.writeByteAndInt((byte)9, 2); o1.writeByte((byte)9); o1.writeLong(2L); o2.writeByteAndLong((byte)9, 2L); o1.writeByte((byte)9); o1.writeFloat(1.1f); o2.writeByteAndFloat((byte)9, 1.1f); o1.writeByte((byte)9); o1.writeDouble(1.1); o2.writeByteAndDouble((byte)9, 1.1); byte[] b1 = bo.toByteArray(); byte[] b2 = o2.toByteArray(); assertEquals(b1.length, b2.length); assertArrayEquals(b1, b2); }
@Test public void testWrite() throws IOException { ByteArrayOutputStream bo = new ByteArrayOutputStream(); DataOutputStream o1 = new DataOutputStream(bo); LinkedBufferOutput o2 = new LinkedBufferOutput(10); byte[] raw = new byte[9]; raw[0] = (byte)1; raw[7] = (byte)1; for(int i=0; i < 11; i++) { o1.write(raw, 0, raw.length); o2.write(raw, 0, raw.length); } byte[] b1 = bo.toByteArray(); byte[] b2 = o2.toByteArray(); assertEquals(b1.length, b2.length); assertArrayEquals(b1, b2); }