public long readFrom(ReadableByteChannel channel) throws IOException {
    long bytesRead = 0;
    if (buffer == null) {
      if (sizeBuffer == null) {
        sizeBuffer = ByteBufAllocator.DEFAULT.heapBuffer(Long.BYTES);
      }
      try {
        bytesRead = readBytesFromReadableByteChannel(channel, sizeBuffer);
        if (bytesRead < 0) {
          throw new EOFException();
        }
      } catch (IOException e) {
        sizeBuffer.release();
        sizeBuffer = null;
        throw e;
      }
      if (sizeBuffer.writerIndex() == sizeBuffer.capacity()) {
        sizeToRead = sizeBuffer.readLong();
        sizeRead += Long.BYTES;
        sizeBuffer.release();
        if (sizeToRead > maxRequestSize) {
          throw new IOException("Request size larger than the maximum size! " + sizeToRead + " > " + maxRequestSize);
        }
        buffer = ByteBufAllocator.DEFAULT.heapBuffer((int) sizeToRead - Long.BYTES);
      }
    }
    if (buffer != null && sizeRead < sizeToRead) {
      long bytesReadFromChannel;
      try {
        bytesReadFromChannel = readBytesFromReadableByteChannel(channel, buffer);
        if (bytesReadFromChannel < 0) {
          throw new EOFException();
        }
      } catch (IOException e) {
        buffer.release();
        buffer = null;
        throw e;
      }
      sizeRead += bytesReadFromChannel;
      bytesRead += bytesReadFromChannel;
    }
    logger.trace("size read from channel {}", sizeRead);
    return bytesRead;
  }
Reading bytes from the {@link ReadableByteChannel} and writing those bytes to the given {@link ByteBuf}.
This method would change the writerIndex of the given buffer.
@param channel The {@link ReadableByteChannel} to read the bytes out.
@param buffer The {@link ByteBuf} to write bytes to.
@return The number of bytes read from channel.
@throws IOException Any I/O error.
ambry-api/src/main/java/com/github/ambry/network/BoundedNettyByteBufReceive.java