private static FutureResult<Long> writeBufferToChannel(Object buf, RetainingAsyncWritableChannel channel,
      FutureResult<Long> callbackResult) {
    if (buf instanceof ByteBuffer) {
      return (FutureResult<Long>) channel.write((ByteBuffer) buf, callbackResult::done);
    } else {
      return (FutureResult<Long>) channel.write((ByteBuf) buf, (result, exception) -> {
        ((ByteBuf) buf).release();
        callbackResult.done(result, exception);
      });
    }
  }
Write the buffer to the given {@link RetainingAsyncWritableChannel}.
@param buf The buffer.
@param channel The given channel.
@param callbackResult The callback function to invoke when finishing writing.
@return The {@link FutureResult} returned from write method.
ambry-commons/src/test/java/com/github/ambry/commons/RetainingAsyncWritableChannelTest.java