@Override
    public void ensureWritableBytes(int minWritableBytes) {
        if (minWritableBytes <= writableBytes()) {
            return;
        }

        int newCapacity = capacity() == 0 ? 1 : capacity();
        int minNewCapacity = writerIndex() + minWritableBytes;
        while (newCapacity < minNewCapacity) {
            newCapacity <<= 1;
        }

        ChannelBuffer newBuffer = factory().getBuffer(newCapacity);
        newBuffer.writeBytes(buffer, 0, writerIndex());
        buffer = newBuffer;
    }
@Test
    void ensureWritableBytes() {
        ChannelBuffer buf = new DynamicChannelBuffer(16);
        buf.ensureWritableBytes(30);
        Assertions.assertEquals(buf.capacity(), 32);

        Random random = new Random();
        byte[] bytes = new byte[126];
        random.nextBytes(bytes);
        buf.writeBytes(bytes);
        Assertions.assertEquals(buf.capacity(), 128);
    }