@Test public void testWithSpy() throws IOException { ByteBuffer[] bufs = new ByteBuffer[] { stringBuf("XXXhelloYYY", 3, 5), stringBuf(" ", 0, 1), stringBuf("XXXXworldY", 4, 5) }; BufferChain chain = new BufferChain(bufs); FileOutputStream fos = new FileOutputStream(tmpFile); FileChannel ch = Mockito.spy(fos.getChannel()); try { chain.write(ch, 2); assertEquals("he", Files.toString(tmpFile, Charsets.UTF_8)); chain.write(ch, 2); assertEquals("hell", Files.toString(tmpFile, Charsets.UTF_8)); chain.write(ch, 3); assertEquals("hello w", Files.toString(tmpFile, Charsets.UTF_8)); chain.write(ch, 8); assertEquals("hello world", Files.toString(tmpFile, Charsets.UTF_8)); } finally { ch.close(); fos.close(); } }
long write(GatheringByteChannel channel, int chunkSize) throws IOException { int chunkRemaining = chunkSize; ByteBuffer lastBuffer = null; int bufCount = 0; int restoreLimit = -1; while (chunkRemaining > 0 && bufferOffset + bufCount < buffers.length) { lastBuffer = buffers[bufferOffset + bufCount]; if (!lastBuffer.hasRemaining()) { bufferOffset++; continue; } bufCount++; if (lastBuffer.remaining() > chunkRemaining) { restoreLimit = lastBuffer.limit(); lastBuffer.limit(lastBuffer.position() + chunkRemaining); chunkRemaining = 0; break; } else { chunkRemaining -= lastBuffer.remaining(); } } assert lastBuffer != null; if (chunkRemaining == chunkSize) { assert !hasRemaining(); return 0; } try { long ret = channel.write(buffers, bufferOffset, bufCount); if (ret > 0) { remaining -= ret; } return ret; } finally { if (restoreLimit >= 0) { lastBuffer.limit(restoreLimit); } } }
BufferChain { long write(GatheringByteChannel channel, int chunkSize) throws IOException { int chunkRemaining = chunkSize; ByteBuffer lastBuffer = null; int bufCount = 0; int restoreLimit = -1; while (chunkRemaining > 0 && bufferOffset + bufCount < buffers.length) { lastBuffer = buffers[bufferOffset + bufCount]; if (!lastBuffer.hasRemaining()) { bufferOffset++; continue; } bufCount++; if (lastBuffer.remaining() > chunkRemaining) { restoreLimit = lastBuffer.limit(); lastBuffer.limit(lastBuffer.position() + chunkRemaining); chunkRemaining = 0; break; } else { chunkRemaining -= lastBuffer.remaining(); } } assert lastBuffer != null; if (chunkRemaining == chunkSize) { assert !hasRemaining(); return 0; } try { long ret = channel.write(buffers, bufferOffset, bufCount); if (ret > 0) { remaining -= ret; } return ret; } finally { if (restoreLimit >= 0) { lastBuffer.limit(restoreLimit); } } } }
BufferChain { long write(GatheringByteChannel channel, int chunkSize) throws IOException { int chunkRemaining = chunkSize; ByteBuffer lastBuffer = null; int bufCount = 0; int restoreLimit = -1; while (chunkRemaining > 0 && bufferOffset + bufCount < buffers.length) { lastBuffer = buffers[bufferOffset + bufCount]; if (!lastBuffer.hasRemaining()) { bufferOffset++; continue; } bufCount++; if (lastBuffer.remaining() > chunkRemaining) { restoreLimit = lastBuffer.limit(); lastBuffer.limit(lastBuffer.position() + chunkRemaining); chunkRemaining = 0; break; } else { chunkRemaining -= lastBuffer.remaining(); } } assert lastBuffer != null; if (chunkRemaining == chunkSize) { assert !hasRemaining(); return 0; } try { long ret = channel.write(buffers, bufferOffset, bufCount); if (ret > 0) { remaining -= ret; } return ret; } finally { if (restoreLimit >= 0) { lastBuffer.limit(restoreLimit); } } } BufferChain(ByteBuffer ... buffers); }
BufferChain { long write(GatheringByteChannel channel, int chunkSize) throws IOException { int chunkRemaining = chunkSize; ByteBuffer lastBuffer = null; int bufCount = 0; int restoreLimit = -1; while (chunkRemaining > 0 && bufferOffset + bufCount < buffers.length) { lastBuffer = buffers[bufferOffset + bufCount]; if (!lastBuffer.hasRemaining()) { bufferOffset++; continue; } bufCount++; if (lastBuffer.remaining() > chunkRemaining) { restoreLimit = lastBuffer.limit(); lastBuffer.limit(lastBuffer.position() + chunkRemaining); chunkRemaining = 0; break; } else { chunkRemaining -= lastBuffer.remaining(); } } assert lastBuffer != null; if (chunkRemaining == chunkSize) { assert !hasRemaining(); return 0; } try { long ret = channel.write(buffers, bufferOffset, bufCount); if (ret > 0) { remaining -= ret; } return ret; } finally { if (restoreLimit >= 0) { lastBuffer.limit(restoreLimit); } } } BufferChain(ByteBuffer ... buffers);  }
BufferChain { long write(GatheringByteChannel channel, int chunkSize) throws IOException { int chunkRemaining = chunkSize; ByteBuffer lastBuffer = null; int bufCount = 0; int restoreLimit = -1; while (chunkRemaining > 0 && bufferOffset + bufCount < buffers.length) { lastBuffer = buffers[bufferOffset + bufCount]; if (!lastBuffer.hasRemaining()) { bufferOffset++; continue; } bufCount++; if (lastBuffer.remaining() > chunkRemaining) { restoreLimit = lastBuffer.limit(); lastBuffer.limit(lastBuffer.position() + chunkRemaining); chunkRemaining = 0; break; } else { chunkRemaining -= lastBuffer.remaining(); } } assert lastBuffer != null; if (chunkRemaining == chunkSize) { assert !hasRemaining(); return 0; } try { long ret = channel.write(buffers, bufferOffset, bufCount); if (ret > 0) { remaining -= ret; } return ret; } finally { if (restoreLimit >= 0) { lastBuffer.limit(restoreLimit); } } } BufferChain(ByteBuffer ... buffers);   }