void writeFrom(ReadableByteChannel channel, long offset, long size) throws IOException {
    if (offset < startOffset || offset >= capacityInBytes) {
      throw new IndexOutOfBoundsException(
          "Provided offset [" + offset + "] is out of bounds for the segment [" + file.getAbsolutePath()
              + "] with capacity [" + capacityInBytes + "]");
    }
    if (offset + size > capacityInBytes) {
      metrics.overflowWriteError.inc();
      throw new IndexOutOfBoundsException(
          "Cannot write to segment [" + file.getAbsolutePath() + "] from offset [" + offset + "] for size [" + size
              + "] because it exceeds the capacity [" + capacityInBytes + "]");
    }
    long bytesWritten = 0;
    while (bytesWritten < size) {
      bytesWritten += fileChannel.transferFrom(channel, offset + bytesWritten, size - bytesWritten);
    }
    if (offset + size > endOffset.get()) {
      endOffset.set(offset + size);
    }
  }
Writes {@code size} number of bytes from the channel {@code channel} into the segment at {@code offset}.
<p/>
The write is not started if it cannot be completed.
@param channel The channel from which data needs to be written from.
@param offset The offset in the segment at which to start writing.
@param size The amount of data in bytes to be written from the channel.
@throws IOException if data could not be written to the file because of I/O errors
@throws IndexOutOfBoundsException if {@code offset} < header size or if there is not enough space for
{@code offset } + {@code size} data.
ambry-store/src/main/java/com/github/ambry/store/LogSegment.java