ReadOnlyUnsafeDirectByteBuf.memoryAddress() should not throw

Motivation:

We need the memoryAddress of a direct buffer when using our native transports. For this reason ReadOnlyUnsafeDirectByteBuf.memoryAddress() should not throw.

Modifications:

- Correctly override ReadOnlyUnsafeDirectByteBuf.memoryAddress() and hasMemoryAddress()
- Add test case

Result:

Fixes [#7672].
This commit is contained in:
Norman Maurer 2018-02-01 19:43:16 +01:00
parent c04d2a529b
commit 8dada6a5ac
3 changed files with 40 additions and 0 deletions

View File

@ -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;
}

View File

@ -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();
}
}
}

View File

@ -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();
}
}
}