diff --git a/buffer/src/main/java/io/netty/buffer/ReadOnlyUnsafeDirectByteBuf.java b/buffer/src/main/java/io/netty/buffer/ReadOnlyUnsafeDirectByteBuf.java index 16320aae31..316760eca7 100644 --- a/buffer/src/main/java/io/netty/buffer/ReadOnlyUnsafeDirectByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/ReadOnlyUnsafeDirectByteBuf.java @@ -125,6 +125,16 @@ final class ReadOnlyUnsafeDirectByteBuf extends ReadOnlyByteBufferBuf { return copy; } + @Override + public boolean hasMemoryAddress() { + return true; + } + + @Override + public long memoryAddress() { + return memoryAddress; + } + private long addr(int index) { return memoryAddress + index; } diff --git a/buffer/src/test/java/io/netty/buffer/ReadOnlyDirectByteBufferBufTest.java b/buffer/src/test/java/io/netty/buffer/ReadOnlyDirectByteBufferBufTest.java index e1b727872b..cbee31c448 100644 --- a/buffer/src/test/java/io/netty/buffer/ReadOnlyDirectByteBufferBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/ReadOnlyDirectByteBufferBufTest.java @@ -274,4 +274,20 @@ public class ReadOnlyDirectByteBufferBufTest { file.delete(); } } + + @Test + public void testMemoryAddress() { + ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer()); + try { + Assert.assertFalse(buf.hasMemoryAddress()); + try { + buf.memoryAddress(); + Assert.fail(); + } catch (UnsupportedOperationException expected) { + // expected + } + } finally { + buf.release(); + } + } } diff --git a/buffer/src/test/java/io/netty/buffer/ReadOnlyUnsafeDirectByteBufferBufTest.java b/buffer/src/test/java/io/netty/buffer/ReadOnlyUnsafeDirectByteBufferBufTest.java index c9100d12e2..efab33f469 100644 --- a/buffer/src/test/java/io/netty/buffer/ReadOnlyUnsafeDirectByteBufferBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/ReadOnlyUnsafeDirectByteBufferBufTest.java @@ -16,7 +16,9 @@ package io.netty.buffer; import io.netty.util.internal.PlatformDependent; +import org.junit.Assert; import org.junit.BeforeClass; +import org.junit.Test; import java.nio.ByteBuffer; @@ -36,4 +38,16 @@ public class ReadOnlyUnsafeDirectByteBufferBufTest extends ReadOnlyDirectByteBuf protected ByteBuf buffer(ByteBuffer buffer) { return new ReadOnlyUnsafeDirectByteBuf(UnpooledByteBufAllocator.DEFAULT, buffer); } + + @Test + @Override + public void testMemoryAddress() { + ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer()); + try { + Assert.assertTrue(buf.hasMemoryAddress()); + buf.memoryAddress(); + } finally { + buf.release(); + } + } }