### Question:
BufUnwrapper implements AutoCloseable { ByteBuffer[] writableNioBuffers(ByteBuf buf) { int readerIndex = buf.readerIndex(); int writerIndex = buf.writerIndex(); buf.readerIndex(writerIndex); buf.writerIndex(buf.capacity()); try { return nioBuffers(buf, singleWriteBuffer); } finally { buf.readerIndex(readerIndex); buf.writerIndex(writerIndex); } }  @Override void close();  }### Answer:
@Test public void writableNioBuffers_indexesPreserved() { ByteBuf buf = alloc.buffer(1); int ridx = buf.readerIndex(); int widx = buf.writerIndex(); int cap = buf.capacity(); try (BufUnwrapper unwrapper = new BufUnwrapper()) { ByteBuffer[] internalBufs = unwrapper.writableNioBuffers(buf); Truth.assertThat(internalBufs).hasLength(1); internalBufs[0].put((byte) 'a'); assertEquals(ridx, buf.readerIndex()); assertEquals(widx, buf.writerIndex()); assertEquals(cap, buf.capacity()); } finally { buf.release(); } }
@Test public void writableNioBuffers_worksWithNormal() { ByteBuf buf = alloc.buffer(1); try (BufUnwrapper unwrapper = new BufUnwrapper()) { ByteBuffer[] internalBufs = unwrapper.writableNioBuffers(buf); Truth.assertThat(internalBufs).hasLength(1); internalBufs[0].put((byte) 'a'); buf.writerIndex(1); assertEquals('a', buf.readByte()); } finally { buf.release(); } }
@Test public void writableNioBuffers_worksWithComposite() { CompositeByteBuf buf = alloc.compositeBuffer(); buf.addComponent(alloc.buffer(1)); buf.capacity(1); try (BufUnwrapper unwrapper = new BufUnwrapper()) { ByteBuffer[] internalBufs = unwrapper.writableNioBuffers(buf); Truth.assertThat(internalBufs).hasLength(1); internalBufs[0].put((byte) 'a'); buf.writerIndex(1); assertEquals('a', buf.readByte()); } finally { buf.release(); } }