FragmentIterator implements Iterator<byte[]> { @Override public byte[] next() { byte[] buffer = new byte[Math.min(data.length - offset, maxFragmentSize)]; System.arraycopy(data, offset, buffer, 0, buffer.length); offset += buffer.length; return buffer; } FragmentIterator(byte[] data, int maxFragmentSize); @Override boolean hasNext(); @Override byte[] next(); @Override void remove();  }
@Test public void testNext() throws Exception { FragmentIterator iterator = new FragmentIterator(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 4); assertTrue(iterator.hasNext()); assertArrayEquals(new byte[] { 1, 2, 3, 4 }, iterator.next()); assertTrue(iterator.hasNext()); assertArrayEquals(new byte[] { 5, 6, 7, 8 }, iterator.next()); assertTrue(iterator.hasNext()); assertArrayEquals(new byte[] { 9, 10 }, iterator.next()); assertFalse(iterator.hasNext()); }