code: 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);
        // Only keep copying if we've read less than maxBytes (if maxBytes exists)
        while ((this.maxBytes == null || this.maxBytes > totalBytes) &&
            (numBytes = fillBufferFromInputChannel(buffer)) != -1) {
          totalBytes += numBytes;
          // flip the buffer to be written
          buffer.flip();
  
          // If we've read more than maxBytes, discard enough bytes to only write maxBytes.
          if (this.maxBytes != null && totalBytes > this.maxBytes) {
            buffer.limit(buffer.limit() - (int) (totalBytes - this.maxBytes));
            totalBytes = this.maxBytes;
          }
  
          this.outputChannel.write(buffer);
          // Clear if empty
          buffer.compact();
          if (this.copySpeedMeter != null) {
            this.copySpeedMeter.mark(numBytes);
          }
        }
        // Done writing, now flip to read again
        buffer.flip();
        // check that buffer is fully written.
        while (buffer.hasRemaining()) {
          this.outputChannel.write(buffer);
        }
  
        return totalBytes;
      } finally {
        if (this.closeChannelsOnComplete) {
          this.inputChannel.close();
          this.outputChannel.close();
        }
      }
    }
docs: Execute the copy of bytes from the input to the output stream. If maxBytes is specified, limits the number of
  bytes copied to maxBytes.
  Note: this method should only be called once. Further calls will throw a {@link IllegalStateException}.
  @return Number of bytes copied.