### Question:
ResumableRandomAccessFileListener implements ResumableListener { public void onBytesReceived(ByteBuffer buffer) throws IOException { file.seek(file.length()); if (buffer.hasArray()) { file.write(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining()); } else { byte[] b = new byte[buffer.remaining()]; int pos = buffer.position(); buffer.get(b); buffer.position(pos); file.write(b); } } ResumableRandomAccessFileListener(RandomAccessFile file); void onBytesReceived(ByteBuffer buffer); void onAllBytesReceived(); long length();  }### Answer:
@Test public void testOnBytesReceivedBufferHasArray() throws IOException { RandomAccessFile file = mock(RandomAccessFile.class); ResumableRandomAccessFileListener listener = new ResumableRandomAccessFileListener(file); byte[] array = new byte[]{1, 2, 23, 33}; ByteBuffer buf = ByteBuffer.wrap(array); listener.onBytesReceived(buf); verify(file).write(array, 0, 4); }
@Test public void testOnBytesReceivedBufferHasNoArray() throws IOException { RandomAccessFile file = mock(RandomAccessFile.class); ResumableRandomAccessFileListener listener = new ResumableRandomAccessFileListener(file); byte[] byteArray = new byte[]{1, 2, 23, 33}; ByteBuffer buf = ByteBuffer.allocateDirect(4); buf.put(byteArray); buf.flip(); listener.onBytesReceived(buf); verify(file).write(byteArray); }