### Question:
ChunkReader implements Closeable { public int readChunk(Writable str, int maxBytesToConsume) throws IOException { ChunkWritable cw = (ChunkWritable) str; List<Node> list = new LinkedList<Node>(); long bytesConsumed = 0; do { if (bufferLength > 0) { int remaining = bufferLength - bufferPosn; Node nd = new Node(); nd.slice = new byte[remaining]; nd.len = remaining; System.arraycopy(buffer, bufferPosn, nd.slice, 0, nd.len); list.add(nd); bytesConsumed += nd.len; } else { Node nd = new Node(); nd.slice = new byte[buffer.length]; nd.len = in.read(nd.slice); if (nd.len <= 0) { break; } bytesConsumed += nd.len; list.add(nd); } bufferLength = bufferPosn = 0; } while (bytesConsumed < maxBytesToConsume); if (list.size() > 0) { cw.box = new byte[(int) bytesConsumed]; int pos = 0; for (int i = 0; i < list.size(); i++) { Node n = list.get(i); System.arraycopy(n.slice, 0, cw.box, pos, n.len); pos += n.len; } } return (int) bytesConsumed; } ChunkReader(InputStream in); @Override void close(); int readChunk(Writable str, int maxBytesToConsume); int readLine(Writable str, int maxBytesToConsume); static final int DEFAULT_BUFFER_SIZE; }### Answer:
@Test public void readChunk() throws Exception { reader = new ChunkReader(mockStream); when( mockStream.read( (byte [])Mockito.anyObject()) ).thenAnswer(new Answer<java.lang.Number>() { @Override public java.lang.Number answer(InvocationOnMock invocation) throws Throwable { byte[] buf = (byte[]) invocation.getArguments()[0]; byte [] source = "OneLine\nTwoLine\n".getBytes(); System.arraycopy(source, 0, buf, 0, source.length); return new java.lang.Integer(source.length); } }); ChunkWritable out = new ChunkWritable(); int maxBytesToConsume = 10; assertEquals("OneLine\nTwoLine\n".length() , reader.readChunk(out, maxBytesToConsume) ); assertEquals("OneLine\nTwoLine\n", new String(out.box) ); }