FragmentStream { public void insertByteArray(byte[] bytesToAdd, int offset) { for (int i = 0; i < bytesToAdd.length; i++) { if (fragmentByteMap.containsKey(offset + i)) { fragmentByteMap.remove(offset + i); } fragmentByteMap.put(offset + i, bytesToAdd[i]); } } FragmentStream(int intendedSize); boolean canInsertByteArray(byte[] bytesToAdd, int offset); void insertByteArray(byte[] bytesToAdd, int offset); boolean isComplete(int tillIndex); byte[] getCompleteFilledStream(byte fillingByte); byte[] getCompleteTruncatedStream();  }
@Test public void testInsertByteArray() { stream.insertByteArray(new byte[] { 1, 2, 3 }, 0); stream.insertByteArray(new byte[] { 4, 5, 6 }, 3); assertFalse(stream.isComplete(10)); byte[] completeStream = stream.getCompleteTruncatedStream(); assertArrayEquals(new byte[] { 1, 2, 3, 4, 5, 6 }, completeStream); completeStream = stream.getCompleteFilledStream((byte) 0xFF); assertArrayEquals(new byte[] { 1, 2, 3, 4, 5, 6, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF }, completeStream); stream.insertByteArray(new byte[] { 7, 8, 9, 10 }, 6); assertTrue(stream.isComplete(10)); assertFalse(stream.isComplete(11)); completeStream = stream.getCompleteTruncatedStream(); assertArrayEquals(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, completeStream); completeStream = stream.getCompleteFilledStream((byte) 0xFF); assertArrayEquals(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, completeStream); }