class BufUnwrapper implements AutoCloseable {

  ByteBuffer[] writableNioBuffers(ByteBuf buf) {
    // Set the writer index to the capacity to guarantee that the returned NIO buffers will have
    // the capacity available.
    int readerIndex = buf.readerIndex();
    int writerIndex = buf.writerIndex();
    buf.readerIndex(writerIndex);
    buf.writerIndex(buf.capacity());

    try {
      return nioBuffers(buf, singleWriteBuffer);
    } finally {
      // Restore the writer index before returning.
      buf.readerIndex(readerIndex);
      buf.writerIndex(writerIndex);
    }
  }

}