@Test public void testBlockCopy() throws Exception { String testString = "This is a string"; ByteArrayInputStream inputStream = new ByteArrayInputStream(testString.getBytes(Charsets.UTF_8)); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); long maxBytes = 4L; new StreamCopier(inputStream, outputStream, maxBytes).copy(); Assert.assertEquals(new String(outputStream.toByteArray(), Charsets.UTF_8), testString.substring(0, (int) maxBytes)); }
public synchronized long copy() throws IOException { if (this.copied) { throw new IllegalStateException(String.format("%s already copied.", StreamCopier.class.getName())); } this.copied = true; try { long numBytes = 0; long totalBytes = 0; final ByteBuffer buffer = ByteBuffer.allocateDirect(this.bufferSize); while ((this.maxBytes == null || this.maxBytes > totalBytes) && (numBytes = fillBufferFromInputChannel(buffer)) != -1) { totalBytes += numBytes; buffer.flip(); if (this.maxBytes != null && totalBytes > this.maxBytes) { buffer.limit(buffer.limit() - (int) (totalBytes - this.maxBytes)); totalBytes = this.maxBytes; } this.outputChannel.write(buffer); buffer.compact(); if (this.copySpeedMeter != null) { this.copySpeedMeter.mark(numBytes); } } buffer.flip(); while (buffer.hasRemaining()) { this.outputChannel.write(buffer); } return totalBytes; } finally { if (this.closeChannelsOnComplete) { this.inputChannel.close(); this.outputChannel.close(); } } }
StreamCopier { public synchronized long copy() throws IOException { if (this.copied) { throw new IllegalStateException(String.format("%s already copied.", StreamCopier.class.getName())); } this.copied = true; try { long numBytes = 0; long totalBytes = 0; final ByteBuffer buffer = ByteBuffer.allocateDirect(this.bufferSize); while ((this.maxBytes == null || this.maxBytes > totalBytes) && (numBytes = fillBufferFromInputChannel(buffer)) != -1) { totalBytes += numBytes; buffer.flip(); if (this.maxBytes != null && totalBytes > this.maxBytes) { buffer.limit(buffer.limit() - (int) (totalBytes - this.maxBytes)); totalBytes = this.maxBytes; } this.outputChannel.write(buffer); buffer.compact(); if (this.copySpeedMeter != null) { this.copySpeedMeter.mark(numBytes); } } buffer.flip(); while (buffer.hasRemaining()) { this.outputChannel.write(buffer); } return totalBytes; } finally { if (this.closeChannelsOnComplete) { this.inputChannel.close(); this.outputChannel.close(); } } } }
StreamCopier { public synchronized long copy() throws IOException { if (this.copied) { throw new IllegalStateException(String.format("%s already copied.", StreamCopier.class.getName())); } this.copied = true; try { long numBytes = 0; long totalBytes = 0; final ByteBuffer buffer = ByteBuffer.allocateDirect(this.bufferSize); while ((this.maxBytes == null || this.maxBytes > totalBytes) && (numBytes = fillBufferFromInputChannel(buffer)) != -1) { totalBytes += numBytes; buffer.flip(); if (this.maxBytes != null && totalBytes > this.maxBytes) { buffer.limit(buffer.limit() - (int) (totalBytes - this.maxBytes)); totalBytes = this.maxBytes; } this.outputChannel.write(buffer); buffer.compact(); if (this.copySpeedMeter != null) { this.copySpeedMeter.mark(numBytes); } } buffer.flip(); while (buffer.hasRemaining()) { this.outputChannel.write(buffer); } return totalBytes; } finally { if (this.closeChannelsOnComplete) { this.inputChannel.close(); this.outputChannel.close(); } } } StreamCopier(InputStream inputStream, OutputStream outputStream);  StreamCopier(InputStream inputStream, OutputStream outputStream, Long maxBytes);  StreamCopier(ReadableByteChannel inputChannel, WritableByteChannel outputChannel);  StreamCopier(ReadableByteChannel inputChannel, WritableByteChannel outputChannel, Long maxBytes); }
StreamCopier { public synchronized long copy() throws IOException { if (this.copied) { throw new IllegalStateException(String.format("%s already copied.", StreamCopier.class.getName())); } this.copied = true; try { long numBytes = 0; long totalBytes = 0; final ByteBuffer buffer = ByteBuffer.allocateDirect(this.bufferSize); while ((this.maxBytes == null || this.maxBytes > totalBytes) && (numBytes = fillBufferFromInputChannel(buffer)) != -1) { totalBytes += numBytes; buffer.flip(); if (this.maxBytes != null && totalBytes > this.maxBytes) { buffer.limit(buffer.limit() - (int) (totalBytes - this.maxBytes)); totalBytes = this.maxBytes; } this.outputChannel.write(buffer); buffer.compact(); if (this.copySpeedMeter != null) { this.copySpeedMeter.mark(numBytes); } } buffer.flip(); while (buffer.hasRemaining()) { this.outputChannel.write(buffer); } return totalBytes; } finally { if (this.closeChannelsOnComplete) { this.inputChannel.close(); this.outputChannel.close(); } } } StreamCopier(InputStream inputStream, OutputStream outputStream);  StreamCopier(InputStream inputStream, OutputStream outputStream, Long maxBytes);  StreamCopier(ReadableByteChannel inputChannel, WritableByteChannel outputChannel);  StreamCopier(ReadableByteChannel inputChannel, WritableByteChannel outputChannel, Long maxBytes); StreamCopier withBufferSize(int bufferSize); StreamCopier withCopySpeedMeter(Meter copySpeedMeter); StreamCopier closeChannelsOnComplete(); synchronized long copy(); }
StreamCopier { public synchronized long copy() throws IOException { if (this.copied) { throw new IllegalStateException(String.format("%s already copied.", StreamCopier.class.getName())); } this.copied = true; try { long numBytes = 0; long totalBytes = 0; final ByteBuffer buffer = ByteBuffer.allocateDirect(this.bufferSize); while ((this.maxBytes == null || this.maxBytes > totalBytes) && (numBytes = fillBufferFromInputChannel(buffer)) != -1) { totalBytes += numBytes; buffer.flip(); if (this.maxBytes != null && totalBytes > this.maxBytes) { buffer.limit(buffer.limit() - (int) (totalBytes - this.maxBytes)); totalBytes = this.maxBytes; } this.outputChannel.write(buffer); buffer.compact(); if (this.copySpeedMeter != null) { this.copySpeedMeter.mark(numBytes); } } buffer.flip(); while (buffer.hasRemaining()) { this.outputChannel.write(buffer); } return totalBytes; } finally { if (this.closeChannelsOnComplete) { this.inputChannel.close(); this.outputChannel.close(); } } } StreamCopier(InputStream inputStream, OutputStream outputStream);  StreamCopier(InputStream inputStream, OutputStream outputStream, Long maxBytes);  StreamCopier(ReadableByteChannel inputChannel, WritableByteChannel outputChannel);  StreamCopier(ReadableByteChannel inputChannel, WritableByteChannel outputChannel, Long maxBytes); StreamCopier withBufferSize(int bufferSize); StreamCopier withCopySpeedMeter(Meter copySpeedMeter); StreamCopier closeChannelsOnComplete(); synchronized long copy(); static final int DEFAULT_BUFFER_SIZE; }