Repository: netty/netty
File: buffer/src/main/java/io/netty/buffer/UnpooledUnsafeDirectByteBuf.java (Java)

Code before:
        // We also call slice() explicitly here to preserve behaviour with previous netty releases.
        super(alloc, initialBuffer, maxCapacity, /* doFree = */ false, /* slice = */ true);
    }

    /**
     * Creates a new direct ByteBuf by wrapping the specified initial buffer.
     * Allows subclasses to control if initialBuffer.slice() should be invoked.
     *
     * @param maxCapacity the maximum capacity of the underlying direct buffer
     */
    protected UnpooledUnsafeDirectByteBuf(ByteBufAllocator alloc, boolean slice,
                                          ByteBuffer initialBuffer, int maxCapacity) {
        super(alloc, initialBuffer, maxCapacity, false, slice);
    }

    UnpooledUnsafeDirectByteBuf(ByteBufAllocator alloc, ByteBuffer initialBuffer, int maxCapacity, boolean doFree) {
        super(alloc, initialBuffer, maxCapacity, doFree, false);
    }

    @Override
    final void setByteBuffer(ByteBuffer buffer, boolean tryFree) {
        super.setByteBuffer(buffer, tryFree);
        memoryAddress = PlatformDependent.directBufferAddress(buffer);
    }

    @Override
    public boolean hasMemoryAddress() {
        return true;
    }

    @Override
    public long memoryAddress() {
        ensureAccessible();
        return memoryAddress;
    }

    @Override
    public byte getByte(int index) {
        checkIndex(index);
        return _getByte(index);
    }

    @Override
    protected byte _getByte(int index) {
        return UnsafeByteBufUtil.getByte(addr(index));
    }

    @Override
    public short getShort(int index) {
        checkIndex(index, 2);
        return _getShort(index);

Review comment (suggestion):
Please add `@UnstableApi` and also add a note that this is dangerous and should only be used if someone really knows the pitfalls etc. 

Code after:
        // a slice. This is done to prevent an IllegalArgumentException when using Java9 as Unsafe.invokeCleaner(...)
        // will check if the given buffer is either a duplicate or slice and in this case throw an
        // IllegalArgumentException.
        //
        // See https://hg.openjdk.java.net/jdk9/hs-demo/jdk/file/0d2ab72ba600/src/jdk.unsupported/share/classes/
        // sun/misc/Unsafe.java#l1250
        //
        // We also call slice() explicitly here to preserve behaviour with previous netty releases.
        super(alloc, initialBuffer, maxCapacity, /* doFree = */ false, /* slice = */ true);
    }

    /**
     * Creates a new direct ByteBuf by wrapping the specified initial buffer.
     * Allows subclasses to control if initialBuffer.slice() should be invoked.
     *
     *  Attention: this is a dangerous API and should only be used by someone
     *  who really knows the possible consequences.
     *  It allows to disable a protective slicing for the provided ByteBuffer instance
     *  and can cause sharing of this ByteBuffer instance between several UnpooledUnsafeDirectByteBuf objects,
     *  as a result modifications would be racy and unsafe.
     *
     * @param slice true means slice() should be called for the provided initialBuffer
     * @param maxCapacity the maximum capacity of the underlying direct buffer
     */
    @UnstableApi
    protected UnpooledUnsafeDirectByteBuf(ByteBufAllocator alloc, boolean slice,
                                          ByteBuffer initialBuffer, int maxCapacity) {
        super(alloc, initialBuffer, maxCapacity, /* doFree = */ false, slice);
    }

    UnpooledUnsafeDirectByteBuf(ByteBufAllocator alloc, ByteBuffer initialBuffer, int maxCapacity, boolean doFree) {
        super(alloc, initialBuffer, maxCapacity, doFree, false);
    }

    @Override
    final void setByteBuffer(ByteBuffer buffer, boolean tryFree) {
        super.setByteBuffer(buffer, tryFree);
        memoryAddress = PlatformDependent.directBufferAddress(buffer);
    }

    @Override
    final void setByteBuffer(CleanableDirectBuffer cleanableDirectBuffer, boolean tryFree) {
        super.setByteBuffer(cleanableDirectBuffer, tryFree);
        memoryAddress = PlatformDependent.directBufferAddress(cleanableDirectBuffer.buffer());
    }

    @Override
    public boolean hasMemoryAddress() {
        return true;
    }