public long copy(final InputStream from, final OutputStream to, final long bytesToCopy)
      throws IOException {
    Preconditions.checkState(bytesToCopy > 0);
    long copied = 0;
    byte[] tmp = mByteArrayPool.get(mTempBufSize);

    try {
      while (copied < bytesToCopy) {
        int read = from.read(tmp, 0, (int) Math.min(mTempBufSize, bytesToCopy - copied));
        if (read == -1) {
          return copied;
        }
        to.write(tmp, 0, read);
        copied += read;
      }
      return copied;
    } finally {
      mByteArrayPool.release(tmp);
    }
  }
Copy at most number of bytes from InputStream to OutputStream.

@param from InputStream
@param to OutputStream
@param bytesToCopy bytes to copy
@return number of copied bytes
@throws IOException
fbcore/src/main/java/com/facebook/common/memory/PooledByteStreams.java