Make the buffer read-only state irreversible

This greatly simplifies the semantics around the const buffers.
When they can no longer be made writable, there is no longer any need for "deconstification".

I decided to call the method "makeReadOnly" to distinguish it from "asReadOnly" that is seen in ByteBuf and ByteBuffer. The latter two return read-only _views_ of the buffer, while makeReadOnly changes the state of the buffer in-place.
This commit is contained in:
Chris Vest 2021-05-05 12:30:52 +02:00
parent 51cc1e7cf4
commit 599c01b762
27 changed files with 194 additions and 331 deletions

View File

@ -226,11 +226,13 @@ public interface Buffer extends Rc<Buffer>, BufferAccessors {
long nativeAddress();
/**
* Set the read-only state of this buffer.
* Make this buffer read-only. This is irreversible.
* Unless a writable slice has previously been obtained from this buffer, there will no longer be any way to modify
* the data contained in this buffer.
*
* @return this buffer.
*/
Buffer readOnly(boolean readOnly);
Buffer makeReadOnly();
/**
* Query if this buffer is read-only or not.

View File

@ -15,8 +15,6 @@
*/
package io.netty.buffer.api;
import io.netty.buffer.api.internal.Statics;
import java.nio.ByteOrder;
import java.util.function.Supplier;
@ -77,31 +75,26 @@ public interface BufferAllocator extends AutoCloseable {
* The buffers produced by the supplier will have {@linkplain Buffer#isOwned() ownership}, and closing them will
* make them {@linkplain Buffer#isAccessible() inaccessible}, just like a normally allocated buffer.
* <p>
* The buffers produced are only "constants" in so far as they are {@linkplain Buffer#readOnly() read-only}.
* However, since all buffers are meant to behave the same, it is possible to make the returned buffers writeable
* again. Doing so will only impact the particular buffer instance, such that changing its contents will not impact
* any other buffer produced by the supplier.
* The buffers produced are "constants", in the sense that they are {@linkplain Buffer#readOnly() read-only}.
* <p>
* It can generally be expected, but is not guaranteed, that the returned supplier is more resource efficient than
* allocating and copying memory with other available APIs. In such optimised implementations, the underlying memory
* baking the buffers will be shared among all the buffers produced by the supplier. Each buffer will then allocate
* their own independent copy of the data only when needed, such as when making the buffer writable, or when slicing
* the buffer.
* baking the buffers will be shared among all the buffers produced by the supplier.
* <p>
* The primary use case for this API, is when you need to repeatedly produce buffers with the same contents, and
* you perhaps wish to keep a {@code static final} field with these contents. This use case has previously been
* solved by allocating a read-only buffer with the given contents, and then slicing or duplicating it on every use.
* This approach had several problems. For instance, if you forget to slice, the offsets of the buffer can change
* in unexpected ways, since the same buffer instance is shared and accessed from many places. The buffer could also
* be deallocated, making the data inaccessible. Lastly, the read-only state could be changed, allowing the
* supposedly constant buffer to change its contents. The supplier-based API solves all of these problems, by
* enforcing that each usage get their own distinct buffer instance.
* be deallocated, making the data inaccessible. The supplier-based API solves all of these problems, by enforcing
* that each usage get their own distinct buffer instance.
*
* @param bytes The byte contents of the buffers produced by the returned supplier.
* @return A supplier of read-only buffers with the given contents.
*/
default Supplier<Buffer> constBufferSupplier(byte[] bytes) {
return () -> allocate(bytes.length).writeBytes(bytes).readOnly(true);
byte[] safeCopy = bytes.clone(); // Prevent modifying the bytes after creating the supplier.
return () -> allocate(bytes.length).writeBytes(safeCopy).makeReadOnly();
}
/**

View File

@ -444,11 +444,11 @@ public final class CompositeBuffer extends RcSupport<Buffer, CompositeBuffer> im
}
@Override
public CompositeBuffer readOnly(boolean readOnly) {
public CompositeBuffer makeReadOnly() {
for (Buffer buf : bufs) {
buf.readOnly(readOnly);
buf.makeReadOnly();
}
this.readOnly = readOnly;
readOnly = true;
return this;
}

View File

@ -17,7 +17,6 @@ package io.netty.buffer.api;
import io.netty.buffer.api.internal.Statics;
import java.lang.ref.Cleaner;
import java.util.function.Supplier;
import static io.netty.buffer.api.internal.Statics.NO_OP_DROP;
@ -38,8 +37,8 @@ class ManagedBufferAllocator implements BufferAllocator, AllocatorControl {
@Override
public Supplier<Buffer> constBufferSupplier(byte[] bytes) {
Buffer constantBuffer = manager.allocateShared(this, bytes.length, manager.drop(), Statics.CLEANER);
constantBuffer.writeBytes(bytes).readOnly(true);
return () -> manager.allocateCopyOnWritable(constantBuffer);
constantBuffer.writeBytes(bytes).makeReadOnly();
return () -> manager.allocateConstChild(constantBuffer);
}
@Override

View File

@ -19,7 +19,7 @@ import java.lang.ref.Cleaner;
public interface MemoryManager {
Buffer allocateShared(AllocatorControl allocatorControl, long size, Drop<Buffer> drop, Cleaner cleaner);
Buffer allocateCopyOnWritable(Buffer ownedReadOnlyBuffer);
Buffer allocateConstChild(Buffer readOnlyConstParent);
Drop<Buffer> drop();
Object unwrapRecoverableMemory(Buffer buf);
int capacityOfRecoverableMemory(Object memory);

View File

@ -47,8 +47,6 @@ class SizeClassedMemoryPool implements BufferAllocator, AllocatorControl, Drop<B
Object memory = sizeClassPool.poll();
if (memory != null) {
return recoverMemoryIntoBuffer(memory)
.reset()
.readOnly(false)
.fill((byte) 0)
.order(ByteOrder.nativeOrder());
}
@ -58,8 +56,8 @@ class SizeClassedMemoryPool implements BufferAllocator, AllocatorControl, Drop<B
@Override
public Supplier<Buffer> constBufferSupplier(byte[] bytes) {
Buffer constantBuffer = manager.allocateShared(this, bytes.length, manager.drop(), Statics.CLEANER);
constantBuffer.writeBytes(bytes).readOnly(true);
return () -> manager.allocateCopyOnWritable(constantBuffer);
constantBuffer.writeBytes(bytes).makeReadOnly();
return () -> manager.allocateConstChild(constantBuffer);
}
protected MemoryManager getMemoryManager() {

View File

@ -43,9 +43,9 @@ public class ByteBufferMemoryManager implements MemoryManager {
}
@Override
public Buffer allocateCopyOnWritable(Buffer ownedReadOnlyBuffer) {
assert ownedReadOnlyBuffer.isOwned() && ownedReadOnlyBuffer.readOnly();
NioBuffer buf = (NioBuffer) ownedReadOnlyBuffer;
public Buffer allocateConstChild(Buffer readOnlyConstParent) {
assert readOnlyConstParent.readOnly();
NioBuffer buf = (NioBuffer) readOnlyConstParent;
return new NioBuffer(buf);
}

View File

@ -61,7 +61,7 @@ class NioBuffer extends RcSupport<Buffer, NioBuffer> implements Buffer, Readable
* Constructor for {@linkplain BufferAllocator#constBufferSupplier(byte[]) const buffers}.
*/
NioBuffer(NioBuffer parent) {
super(new MakeInaccisbleOnDrop(new ArcDrop<>(((ArcDrop<NioBuffer>) parent.unsafeGetDrop()).unwrap())));
super(new MakeInaccisbleOnDrop(new ArcDrop<>(ArcDrop.acquire(parent.unsafeGetDrop()))));
control = parent.control;
base = parent.base;
rmem = parent.rmem.slice(0, parent.rmem.capacity()); // Need to slice to get independent byte orders.
@ -175,15 +175,8 @@ class NioBuffer extends RcSupport<Buffer, NioBuffer> implements Buffer, Readable
}
@Override
public Buffer readOnly(boolean readOnly) {
if (!readOnly) {
deconstify();
}
if (readOnly && wmem == rmem) {
wmem = CLOSED_BUFFER;
} else if (!readOnly && wmem != rmem) {
wmem = rmem;
}
public Buffer makeReadOnly() {
wmem = CLOSED_BUFFER;
return this;
}
@ -200,14 +193,16 @@ class NioBuffer extends RcSupport<Buffer, NioBuffer> implements Buffer, Readable
if (!isAccessible()) {
throw new IllegalStateException("This buffer is closed: " + this + '.');
}
deconstify(); // Slice or parent could later be made writable, and if so, changes must be visible in both.
ByteBuffer slice = rmem.slice(offset, length);
ArcDrop<NioBuffer> drop = (ArcDrop<NioBuffer>) unsafeGetDrop();
drop.increment();
return new NioBuffer(base, slice, control, drop)
Buffer sliceBuffer = new NioBuffer(base, slice, control, drop)
.writerOffset(length)
.order(order())
.readOnly(readOnly());
.order(order());
if (readOnly()) {
sliceBuffer = sliceBuffer.makeReadOnly();
}
return sliceBuffer;
}
@Override
@ -465,7 +460,9 @@ class NioBuffer extends RcSupport<Buffer, NioBuffer> implements Buffer, Readable
splitBuffer.roff = Math.min(roff, splitOffset);
splitBuffer.order(order());
boolean readOnly = readOnly();
splitBuffer.readOnly(readOnly);
if (readOnly) {
splitBuffer.makeReadOnly();
}
// Note that split, unlike slice, does not deconstify, because data changes in either buffer are not visible
// in the other. The split buffers can later deconstify independently if needed.
splitBuffer.constBuffer = constBuffer;
@ -1104,7 +1101,9 @@ class NioBuffer extends RcSupport<Buffer, NioBuffer> implements Buffer, Readable
copy.order(order);
copy.roff = roff;
copy.woff = woff;
copy.readOnly(readOnly);
if (readOnly) {
copy.makeReadOnly();
}
copy.constBuffer = isConst;
return copy;
}
@ -1119,24 +1118,6 @@ class NioBuffer extends RcSupport<Buffer, NioBuffer> implements Buffer, Readable
woff = 0;
}
/**
* If this buffer is a {@linkplain BufferAllocator#constBufferSupplier(byte[]) const buffer}, turn it into a normal
* buffer, in order to protect the const parent.
* A const buffer is sharing its memory with some parent, whose contents cannot be allowed to change.
* To ensure this, we must allocate our own memory and copy the contents, because we will soon not be able to
* guarantee that the contents of <em>this</em> buffer won't change.
*/
private void deconstify() {
if (constBuffer) {
ByteBuffer byteBuffer = (ByteBuffer) control.allocateUntethered(this, capacity());
byteBuffer.put(rmem);
byteBuffer.order(rmem.order());
Drop<NioBuffer> drop = ArcDrop.wrap(ArcDrop.unwrapAllArcs(unsafeGetDrop()));
unsafeSetDrop(drop);
attachNewBuffer(byteBuffer, drop);
}
}
@Override
public boolean isOwned() {
return super.isOwned() && ((ArcDrop<NioBuffer>) unsafeGetDrop()).isOwned();

View File

@ -34,9 +34,9 @@ public abstract class AbstractMemorySegmentManager implements MemoryManager {
}
@Override
public Buffer allocateCopyOnWritable(Buffer ownedReadOnlyBuffer) {
assert ownedReadOnlyBuffer.isOwned() && ownedReadOnlyBuffer.readOnly();
MemSegBuffer buf = (MemSegBuffer) ownedReadOnlyBuffer;
public Buffer allocateConstChild(Buffer readOnlyConstParent) {
assert readOnlyConstParent.readOnly();
MemSegBuffer buf = (MemSegBuffer) readOnlyConstParent;
return new MemSegBuffer(buf);
}

View File

@ -106,7 +106,7 @@ class MemSegBuffer extends RcSupport<Buffer, MemSegBuffer> implements Buffer, Re
* Constructor for {@linkplain BufferAllocator#constBufferSupplier(byte[]) const buffers}.
*/
MemSegBuffer(MemSegBuffer parent) {
super(new MakeInaccisbleOnDrop(new ArcDrop<>(((ArcDrop<MemSegBuffer>) parent.unsafeGetDrop()).unwrap())));
super(new MakeInaccisbleOnDrop(new ArcDrop<>(ArcDrop.acquire(parent.unsafeGetDrop()))));
control = parent.control;
base = parent.base;
seg = parent.seg;
@ -114,7 +114,7 @@ class MemSegBuffer extends RcSupport<Buffer, MemSegBuffer> implements Buffer, Re
order = parent.order;
roff = parent.roff;
woff = parent.woff;
adaptor = null; // The adaptor must be independent, because we can de-constify.
adaptor = null;
constBuffer = true;
}
@ -298,11 +298,8 @@ class MemSegBuffer extends RcSupport<Buffer, MemSegBuffer> implements Buffer, Re
}
@Override
public Buffer readOnly(boolean readOnly) {
if (!readOnly) {
deconstify();
}
wseg = readOnly? CLOSED_SEGMENT : seg;
public Buffer makeReadOnly() {
wseg = CLOSED_SEGMENT;
return this;
}
@ -319,13 +316,15 @@ class MemSegBuffer extends RcSupport<Buffer, MemSegBuffer> implements Buffer, Re
if (!isAccessible()) {
throw new IllegalStateException("This buffer is closed: " + this + '.');
}
deconstify(); // Slice or parent could later be made writable, and if so, changes must be visible in both.
var slice = seg.asSlice(offset, length);
Drop<MemSegBuffer> drop = ArcDrop.acquire(unsafeGetDrop());
return new MemSegBuffer(base, slice, drop, control)
Buffer sliceBuffer = new MemSegBuffer(base, slice, drop, control)
.writerOffset(length)
.order(order())
.readOnly(readOnly());
.order(order());
if (readOnly()) {
sliceBuffer = sliceBuffer.makeReadOnly();
}
return sliceBuffer;
}
@Override
@ -595,7 +594,9 @@ class MemSegBuffer extends RcSupport<Buffer, MemSegBuffer> implements Buffer, Re
splitBuffer.roff = Math.min(roff, splitOffset);
splitBuffer.order(order);
boolean readOnly = readOnly();
splitBuffer.readOnly(readOnly);
if (readOnly) {
splitBuffer.makeReadOnly();
}
// Note that split, unlike slice, does not deconstify, because data changes in either buffer are not visible
// in the other. The split buffers can later deconstify independently if needed.
splitBuffer.constBuffer = constBuffer;
@ -1137,7 +1138,9 @@ class MemSegBuffer extends RcSupport<Buffer, MemSegBuffer> implements Buffer, Re
copy.order = order;
copy.roff = roff;
copy.woff = woff;
copy.readOnly(readOnly);
if (readOnly) {
copy.makeReadOnly();
}
copy.constBuffer = isConst;
return copy;
}
@ -1152,23 +1155,6 @@ class MemSegBuffer extends RcSupport<Buffer, MemSegBuffer> implements Buffer, Re
woff = 0;
}
/**
* If this buffer is a {@linkplain BufferAllocator#constBufferSupplier(byte[]) const buffer}, turn it into a normal
* buffer, in order to protect the const parent.
* A const buffer is sharing its memory with some parent, whose contents cannot be allowed to change.
* To ensure this, we must allocate our own memory and copy the contents, because we will soon not be able to
* guarantee that the contents of <em>this</em> buffer won't change.
*/
private void deconstify() {
if (constBuffer) {
MemorySegment newSegment = (MemorySegment) control.allocateUntethered(this, capacity());
newSegment.copyFrom(seg);
Drop<MemSegBuffer> drop = ArcDrop.wrap(ArcDrop.unwrapAllArcs(unsafeGetDrop()));
unsafeSetDrop(drop);
attachNewMemorySegment(newSegment, drop);
}
}
@Override
public boolean isOwned() {
return super.isOwned() && ((ArcDrop<MemSegBuffer>) unsafeGetDrop()).isOwned();

View File

@ -70,7 +70,7 @@ class UnsafeBuffer extends RcSupport<Buffer, UnsafeBuffer> implements Buffer, Re
}
UnsafeBuffer(UnsafeBuffer parent) {
super(new MakeInaccisbleOnDrop(new ArcDrop<>(((ArcDrop<UnsafeBuffer>) parent.unsafeGetDrop()).unwrap())));
super(new MakeInaccisbleOnDrop(new ArcDrop<>(ArcDrop.acquire(parent.unsafeGetDrop()))));
control = parent.control;
memory = parent.memory;
base = parent.base;
@ -152,12 +152,9 @@ class UnsafeBuffer extends RcSupport<Buffer, UnsafeBuffer> implements Buffer, Re
}
@Override
public Buffer readOnly(boolean readOnly) {
if (!readOnly) {
deconstify();
}
this.readOnly = readOnly;
wsize = readOnly? CLOSED_SIZE : rsize;
public Buffer makeReadOnly() {
readOnly = true;
wsize = CLOSED_SIZE;
return this;
}
@ -172,13 +169,15 @@ class UnsafeBuffer extends RcSupport<Buffer, UnsafeBuffer> implements Buffer, Re
throw new IllegalArgumentException("Length cannot be negative: " + length + '.');
}
checkGet(offset, length);
deconstify(); // Slice or parent could later be made writable, and if so, changes must be visible in both.
ArcDrop<UnsafeBuffer> drop = (ArcDrop<UnsafeBuffer>) unsafeGetDrop();
drop.increment();
return new UnsafeBuffer(memory, baseOffset + offset, length, control, drop)
Buffer sliceBuffer = new UnsafeBuffer(memory, baseOffset + offset, length, control, drop)
.writerOffset(length)
.order(order)
.readOnly(readOnly);
.order(order);
if (readOnly) {
sliceBuffer = sliceBuffer.makeReadOnly();
}
return sliceBuffer;
}
@Override
@ -506,7 +505,9 @@ class UnsafeBuffer extends RcSupport<Buffer, UnsafeBuffer> implements Buffer, Re
splitBuffer.roff = Math.min(roff, splitOffset);
splitBuffer.order(order());
boolean readOnly = readOnly();
splitBuffer.readOnly(readOnly);
if (readOnly) {
splitBuffer.makeReadOnly();
}
// Note that split, unlike slice, does not deconstify, because data changes in either buffer are not visible
// in the other. The split buffers can later deconstify independently if needed.
splitBuffer.constBuffer = constBuffer;
@ -1243,7 +1244,9 @@ class UnsafeBuffer extends RcSupport<Buffer, UnsafeBuffer> implements Buffer, Re
copy.order(order);
copy.roff = roff;
copy.woff = woff;
copy.readOnly(readOnly);
if (readOnly) {
copy.makeReadOnly();
}
copy.constBuffer = isConst;
return copy;
}
@ -1296,29 +1299,6 @@ class UnsafeBuffer extends RcSupport<Buffer, UnsafeBuffer> implements Buffer, Re
readOnly = false;
}
/**
* If this buffer is a {@linkplain BufferAllocator#constBufferSupplier(byte[]) const buffer}, turn it into a normal
* buffer, in order to protect the const parent.
* A const buffer is sharing its memory with some parent, whose contents cannot be allowed to change.
* To ensure this, we must allocate our own memory and copy the contents, because we will soon not be able to
* guarantee that the contents of <em>this</em> buffer won't change.
*/
private void deconstify() {
if (constBuffer) {
UnsafeMemory newMemory = (UnsafeMemory) control.allocateUntethered(this, capacity());
UnsafeMemory oldMemory = memory;
try {
PlatformDependent.copyMemory(base, address, newMemory.base, newMemory.address, newMemory.size);
Drop<UnsafeBuffer> drop = ArcDrop.wrap(ArcDrop.unwrapAllArcs(unsafeGetDrop()));
unsafeSetDrop(drop);
attachNewMemory(newMemory, drop);
} finally {
Reference.reachabilityFence(newMemory);
Reference.reachabilityFence(oldMemory);
}
}
}
@Override
public boolean isOwned() {
return super.isOwned() && ((ArcDrop<UnsafeBuffer>) unsafeGetDrop()).isOwned();

View File

@ -57,9 +57,9 @@ public class UnsafeMemoryManager implements MemoryManager {
}
@Override
public Buffer allocateCopyOnWritable(Buffer ownedReadOnlyBuffer) {
assert ownedReadOnlyBuffer.isOwned() && ownedReadOnlyBuffer.readOnly();
UnsafeBuffer buf = (UnsafeBuffer) ownedReadOnlyBuffer;
public Buffer allocateConstChild(Buffer readOnlyConstParent) {
assert readOnlyConstParent.readOnly();
UnsafeBuffer buf = (UnsafeBuffer) readOnlyConstParent;
return new UnsafeBuffer(buf);
}

View File

@ -37,7 +37,7 @@ public class BufferByteOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfByteReadOnlyMustBoundsCheckOnNegativeOffset(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getByte(-1));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getByte(-1));
}
}
@ -83,7 +83,7 @@ public class BufferByteOffsettedAccessorsTest extends BufferTestSupport {
Buffer buf = allocator.allocate(8)) {
byte value = 0x01;
buf.writeByte(value);
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getByte(-1));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getByte(-1));
}
}
@ -110,7 +110,7 @@ public class BufferByteOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfByteReadOnlyMustNotBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
buf.readOnly(true).getByte(0);
buf.makeReadOnly().getByte(0);
}
}
@ -119,7 +119,7 @@ public class BufferByteOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfByteReadOnlyMustBoundsCheckWhenReadOffsetIsGreaterThanCapacity(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getByte(8));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getByte(8));
}
}
@ -137,7 +137,7 @@ public class BufferByteOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfUnsignedByteReadOnlyMustBoundsCheckOnNegativeOffset(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getUnsignedByte(-1));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getUnsignedByte(-1));
}
}
@ -193,7 +193,7 @@ public class BufferByteOffsettedAccessorsTest extends BufferTestSupport {
Buffer buf = allocator.allocate(8)) {
int value = 0x01;
buf.writeUnsignedByte(value);
buf.readOnly(true).getUnsignedByte(1);
buf.makeReadOnly().getUnsignedByte(1);
}
}
@ -203,7 +203,7 @@ public class BufferByteOffsettedAccessorsTest extends BufferTestSupport {
Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getUnsignedByte(8));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getUnsignedByte(8));
}
}
@ -230,7 +230,7 @@ public class BufferByteOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfUnsignedByteReadOnlyMustNotBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
buf.readOnly(true).getUnsignedByte(0);
buf.makeReadOnly().getUnsignedByte(0);
}
}
@ -239,7 +239,7 @@ public class BufferByteOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfUnsignedByteReadOnlyMustBoundsCheckWhenReadOffsetIsGreaterThanCapacity(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getUnsignedByte(8));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getUnsignedByte(8));
}
}

View File

@ -36,7 +36,7 @@ public class BufferCharOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfCharReadOnlyMustBoundsCheckOnNegativeOffset(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getChar(-1));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getChar(-1));
}
}
@ -91,7 +91,7 @@ public class BufferCharOffsettedAccessorsTest extends BufferTestSupport {
Buffer buf = allocator.allocate(8)) {
char value = 0x0102;
buf.writeChar(value);
buf.readOnly(true).getChar(1);
buf.makeReadOnly().getChar(1);
}
}
@ -100,7 +100,7 @@ public class BufferCharOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfCharReadOnlyMustBoundsCheckWhenReadOffsetAndSizeIsGreaterThanCapacity(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getChar(7));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getChar(7));
}
}
@ -127,7 +127,7 @@ public class BufferCharOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfCharReadOnlyMustNotBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
buf.readOnly(true).getChar(0);
buf.makeReadOnly().getChar(0);
}
}
@ -136,7 +136,7 @@ public class BufferCharOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfCharReadOnlyMustBoundsCheckWhenReadOffsetIsGreaterThanCapacity(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getChar(8));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getChar(8));
}
}

View File

@ -103,8 +103,8 @@ public class BufferComponentIterationTest extends BufferTestSupport {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer bufBERW = allocator.allocate(8).order(BIG_ENDIAN).writeLong(value);
Buffer bufLERW = allocator.allocate(8).order(LITTLE_ENDIAN).writeLong(value);
Buffer bufBERO = allocator.allocate(8).order(BIG_ENDIAN).writeLong(value).readOnly(true);
Buffer bufLERO = allocator.allocate(8).order(LITTLE_ENDIAN).writeLong(value).readOnly(true)) {
Buffer bufBERO = allocator.allocate(8).order(BIG_ENDIAN).writeLong(value).makeReadOnly();
Buffer bufLERO = allocator.allocate(8).order(LITTLE_ENDIAN).writeLong(value).makeReadOnly()) {
verifyForEachReadableSingleComponent(fixture, bufBERW);
verifyForEachReadableSingleComponent(fixture, bufLERW);
verifyForEachReadableSingleComponent(fixture, bufBERO);
@ -361,7 +361,7 @@ public class BufferComponentIterationTest extends BufferTestSupport {
@MethodSource("allocators")
public void forEachWritableOnReadOnlyBufferMustThrow(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8).readOnly(true)) {
Buffer buf = allocator.allocate(8).makeReadOnly()) {
assertThrows(IllegalStateException.class, () -> buf.forEachWritable(0, (index, component) -> true));
}
}

View File

@ -316,7 +316,7 @@ public class BufferCompositionTest extends BufferTestSupport {
public void emptyCompositeBufferMustAllowExtendingWithReadOnlyBuffer() {
try (BufferAllocator allocator = BufferAllocator.heap()) {
try (CompositeBuffer composite = CompositeBuffer.compose(allocator)) {
try (Buffer b = allocator.allocate(8).readOnly(true)) {
try (Buffer b = allocator.allocate(8).makeReadOnly()) {
composite.extendWith(b);
assertTrue(composite.readOnly());
}
@ -425,8 +425,8 @@ public class BufferCompositionTest extends BufferTestSupport {
@Test
public void composingReadOnlyBuffersMustCreateReadOnlyCompositeBuffer() {
try (BufferAllocator allocator = BufferAllocator.heap();
Buffer a = allocator.allocate(4).readOnly(true);
Buffer b = allocator.allocate(4).readOnly(true);
Buffer a = allocator.allocate(4).makeReadOnly();
Buffer b = allocator.allocate(4).makeReadOnly();
Buffer composite = CompositeBuffer.compose(allocator, a, b)) {
assertTrue(composite.readOnly());
verifyWriteInaccessible(composite);
@ -436,7 +436,7 @@ public class BufferCompositionTest extends BufferTestSupport {
@Test
public void composingReadOnlyAndWritableBuffersMustThrow() {
try (BufferAllocator allocator = BufferAllocator.heap();
Buffer a = allocator.allocate(8).readOnly(true);
Buffer a = allocator.allocate(8).makeReadOnly();
Buffer b = allocator.allocate(8)) {
assertThrows(IllegalArgumentException.class, () -> CompositeBuffer.compose(allocator, a, b));
assertThrows(IllegalArgumentException.class, () -> CompositeBuffer.compose(allocator, b, a));
@ -452,7 +452,7 @@ public class BufferCompositionTest extends BufferTestSupport {
try (Buffer a = allocator.allocate(8)) {
composite = CompositeBuffer.compose(allocator, a);
}
try (composite; Buffer b = allocator.allocate(8).readOnly(true)) {
try (composite; Buffer b = allocator.allocate(8).makeReadOnly()) {
assertThrows(IllegalArgumentException.class, () -> composite.extendWith(b));
}
}
@ -462,7 +462,7 @@ public class BufferCompositionTest extends BufferTestSupport {
public void compositeReadOnlyBufferCannotBeExtendedWithWritableBuffer() {
try (BufferAllocator allocator = BufferAllocator.heap()) {
CompositeBuffer composite;
try (Buffer a = allocator.allocate(8).readOnly(true)) {
try (Buffer a = allocator.allocate(8).makeReadOnly()) {
composite = CompositeBuffer.compose(allocator, a);
}
try (composite; Buffer b = allocator.allocate(8)) {

View File

@ -36,7 +36,7 @@ public class BufferDoubleOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfDoubleReadOnlyMustBoundsCheckOnNegativeOffset(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getDouble(-1));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getDouble(-1));
}
}
@ -82,7 +82,7 @@ public class BufferDoubleOffsettedAccessorsTest extends BufferTestSupport {
Buffer buf = allocator.allocate(8)) {
double value = Double.longBitsToDouble(0x0102030405060708L);
buf.writeDouble(value);
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getDouble(1));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getDouble(1));
}
}
@ -109,7 +109,7 @@ public class BufferDoubleOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfDoubleReadOnlyMustNotBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
buf.readOnly(true).getDouble(0);
buf.makeReadOnly().getDouble(0);
}
}
@ -118,7 +118,7 @@ public class BufferDoubleOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfDoubleReadOnlyMustBoundsCheckWhenReadOffsetIsGreaterThanCapacity(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getDouble(8));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getDouble(8));
}
}

View File

@ -37,7 +37,7 @@ public class BufferFloatOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfFloatReadOnlyMustBoundsCheckOnNegativeOffset(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getFloat(-1));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getFloat(-1));
}
}
@ -92,7 +92,7 @@ public class BufferFloatOffsettedAccessorsTest extends BufferTestSupport {
Buffer buf = allocator.allocate(8)) {
float value = Float.intBitsToFloat(0x01020304);
buf.writeFloat(value);
buf.readOnly(true).getFloat(1);
buf.makeReadOnly().getFloat(1);
}
}
@ -101,7 +101,7 @@ public class BufferFloatOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfFloatReadOnlyMustBoundsCheckWhenReadOffsetAndSizeIsGreaterThanCapacity(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getFloat(5));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getFloat(5));
}
}
@ -128,7 +128,7 @@ public class BufferFloatOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfFloatReadOnlyMustNotBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
buf.readOnly(true).getFloat(0);
buf.makeReadOnly().getFloat(0);
}
}
@ -137,7 +137,7 @@ public class BufferFloatOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfFloatReadOnlyMustBoundsCheckWhenReadOffsetIsGreaterThan(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getFloat(8));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getFloat(8));
}
}

View File

@ -36,7 +36,7 @@ public class BufferIntOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfIntReadOnlyMustBoundsCheckOnNegativeOffset(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getInt(-1));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getInt(-1));
}
}
@ -82,7 +82,7 @@ public class BufferIntOffsettedAccessorsTest extends BufferTestSupport {
Buffer buf = allocator.allocate(8)) {
int value = 0x01020304;
buf.writeInt(value);
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getInt(-1));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getInt(-1));
}
}
@ -109,7 +109,7 @@ public class BufferIntOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfIntReadOnlyMustNotBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
buf.readOnly(true).getInt(0);
buf.makeReadOnly().getInt(0);
}
}
@ -118,7 +118,7 @@ public class BufferIntOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfIntReadOnlyMustBoundsCheckWhenReadOffsetIsGreaterThanCapacity(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getInt(8));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getInt(8));
}
}
@ -136,7 +136,7 @@ public class BufferIntOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfUnsignedIntReadOnlyMustBoundsCheckOnNegativeOffset(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getUnsignedInt(-1));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getUnsignedInt(-1));
}
}
@ -192,7 +192,7 @@ public class BufferIntOffsettedAccessorsTest extends BufferTestSupport {
Buffer buf = allocator.allocate(8)) {
long value = 0x01020304;
buf.writeUnsignedInt(value);
buf.readOnly(true).getUnsignedInt(1);
buf.makeReadOnly().getUnsignedInt(1);
}
}
@ -202,7 +202,7 @@ public class BufferIntOffsettedAccessorsTest extends BufferTestSupport {
Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getUnsignedInt(5));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getUnsignedInt(5));
}
}
@ -229,7 +229,7 @@ public class BufferIntOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfUnsignedIntReadOnlyMustNotBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
buf.readOnly(true).getUnsignedInt(0);
buf.makeReadOnly().getUnsignedInt(0);
}
}
@ -238,7 +238,7 @@ public class BufferIntOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfUnsignedIntReadOnlyMustBoundsCheckWhenReadOffsetIsGreaterThanCapacity(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getUnsignedInt(8));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getUnsignedInt(8));
}
}

View File

@ -36,7 +36,7 @@ public class BufferLongOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfLongReadOnlyMustBoundsCheckOnNegativeOffset(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getLong(-1));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getLong(-1));
}
}
@ -82,7 +82,7 @@ public class BufferLongOffsettedAccessorsTest extends BufferTestSupport {
Buffer buf = allocator.allocate(8)) {
long value = 0x0102030405060708L;
buf.writeLong(value);
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getLong(1));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getLong(1));
}
}
@ -109,7 +109,7 @@ public class BufferLongOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfLongReadOnlyMustNotBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
buf.readOnly(true).getLong(0);
buf.makeReadOnly().getLong(0);
}
}
@ -118,7 +118,7 @@ public class BufferLongOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfLongReadOnlyMustBoundsCheckWhenReadOffsetIsGreaterThanCapacity(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getLong(8));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getLong(8));
}
}

View File

@ -36,7 +36,7 @@ public class BufferMediumOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfMediumReadOnlyMustBoundsCheckOnNegativeOffset(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getMedium(-1));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getMedium(-1));
}
}
@ -93,7 +93,7 @@ public class BufferMediumOffsettedAccessorsTest extends BufferTestSupport {
Buffer buf = allocator.allocate(8)) {
int value = 0x010203;
buf.writeMedium(value);
buf.readOnly(true).getMedium(1);
buf.makeReadOnly().getMedium(1);
}
}
@ -102,7 +102,7 @@ public class BufferMediumOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfMediumReadOnlyMustBoundsCheckWhenReadOffsetAndSizeIsGreaterThanCapacity(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getMedium(6));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getMedium(6));
}
}
@ -129,7 +129,7 @@ public class BufferMediumOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfMediumReadOnlyMustNotBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
buf.readOnly(true).getMedium(0);
buf.makeReadOnly().getMedium(0);
}
}
@ -138,7 +138,7 @@ public class BufferMediumOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfMediumReadOnlyMustBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getMedium(8));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getMedium(8));
}
}
@ -156,7 +156,7 @@ public class BufferMediumOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfUnsignedMediumReadOnlyMustBoundsCheckOnNegativeOffset(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getUnsignedMedium(-1));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getUnsignedMedium(-1));
}
}
@ -212,7 +212,7 @@ public class BufferMediumOffsettedAccessorsTest extends BufferTestSupport {
Buffer buf = allocator.allocate(8)) {
int value = 0x010203;
buf.writeUnsignedMedium(value);
buf.readOnly(true).getUnsignedMedium(1);
buf.makeReadOnly().getUnsignedMedium(1);
}
}
@ -222,7 +222,7 @@ public class BufferMediumOffsettedAccessorsTest extends BufferTestSupport {
Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getUnsignedMedium(6));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getUnsignedMedium(6));
}
}
@ -249,7 +249,7 @@ public class BufferMediumOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfUnsignedMediumReadOnlyMustNotBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
buf.readOnly(true).getUnsignedMedium(0);
buf.makeReadOnly().getUnsignedMedium(0);
}
}
@ -258,7 +258,7 @@ public class BufferMediumOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfUnsignedMediumReadOnlyMustBoundsCheckWhenReadOffsetIsGreaterThanCapacity(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getUnsignedMedium(8));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getUnsignedMedium(8));
}
}

View File

@ -137,7 +137,7 @@ public class BufferPrimitiveRelativeAccessorsTest extends BufferTestSupport {
buf.readerOffset(1);
assertEquals(0, buf.readableBytes());
assertEquals(7, buf.writableBytes());
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).readUnsignedByte());
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().readUnsignedByte());
assertEquals(0, buf.readableBytes());
}
}
@ -275,7 +275,7 @@ public class BufferPrimitiveRelativeAccessorsTest extends BufferTestSupport {
buf.readerOffset(1);
assertEquals(1, buf.readableBytes());
assertEquals(6, buf.writableBytes());
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).readChar());
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().readChar());
assertEquals(1, buf.readableBytes());
}
}
@ -378,7 +378,7 @@ public class BufferPrimitiveRelativeAccessorsTest extends BufferTestSupport {
buf.readerOffset(1);
assertEquals(1, buf.readableBytes());
assertEquals(6, buf.writableBytes());
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).readShort());
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().readShort());
assertEquals(1, buf.readableBytes());
}
}
@ -446,7 +446,7 @@ public class BufferPrimitiveRelativeAccessorsTest extends BufferTestSupport {
buf.readerOffset(1);
assertEquals(1, buf.readableBytes());
assertEquals(6, buf.writableBytes());
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).readUnsignedShort());
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().readUnsignedShort());
assertEquals(1, buf.readableBytes());
}
}
@ -584,7 +584,7 @@ public class BufferPrimitiveRelativeAccessorsTest extends BufferTestSupport {
buf.readerOffset(1);
assertEquals(2, buf.readableBytes());
assertEquals(5, buf.writableBytes());
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).readMedium());
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().readMedium());
assertEquals(2, buf.readableBytes());
}
}
@ -652,7 +652,7 @@ public class BufferPrimitiveRelativeAccessorsTest extends BufferTestSupport {
buf.readerOffset(1);
assertEquals(2, buf.readableBytes());
assertEquals(5, buf.writableBytes());
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).readUnsignedMedium());
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().readUnsignedMedium());
assertEquals(2, buf.readableBytes());
}
}
@ -790,7 +790,7 @@ public class BufferPrimitiveRelativeAccessorsTest extends BufferTestSupport {
buf.readerOffset(1);
assertEquals(3, buf.readableBytes());
assertEquals(4, buf.writableBytes());
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).readInt());
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().readInt());
assertEquals(3, buf.readableBytes());
}
}
@ -858,7 +858,7 @@ public class BufferPrimitiveRelativeAccessorsTest extends BufferTestSupport {
buf.readerOffset(1);
assertEquals(3, buf.readableBytes());
assertEquals(4, buf.writableBytes());
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).readUnsignedInt());
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().readUnsignedInt());
assertEquals(3, buf.readableBytes());
}
}
@ -996,7 +996,7 @@ public class BufferPrimitiveRelativeAccessorsTest extends BufferTestSupport {
buf.readerOffset(1);
assertEquals(3, buf.readableBytes());
assertEquals(4, buf.writableBytes());
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).readFloat());
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().readFloat());
assertEquals(3, buf.readableBytes());
}
}
@ -1099,7 +1099,7 @@ public class BufferPrimitiveRelativeAccessorsTest extends BufferTestSupport {
buf.readerOffset(1);
assertEquals(7, buf.readableBytes());
assertEquals(0, buf.writableBytes());
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).readLong());
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().readLong());
assertEquals(7, buf.readableBytes());
}
}
@ -1202,7 +1202,7 @@ public class BufferPrimitiveRelativeAccessorsTest extends BufferTestSupport {
buf.readerOffset(1);
assertEquals(7, buf.readableBytes());
assertEquals(0, buf.writableBytes());
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).readDouble());
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().readDouble());
assertEquals(7, buf.readableBytes());
}
}

View File

@ -19,12 +19,10 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.function.Supplier;
import static java.nio.ByteOrder.BIG_ENDIAN;
import static java.nio.ByteOrder.LITTLE_ENDIAN;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
@ -37,7 +35,7 @@ public class BufferReadOnlyTest extends BufferTestSupport {
public void readOnlyBufferMustPreventWriteAccess(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
var b = buf.readOnly(true);
var b = buf.makeReadOnly();
assertThat(b).isSameAs(buf);
verifyWriteInaccessible(buf);
}
@ -48,7 +46,7 @@ public class BufferReadOnlyTest extends BufferTestSupport {
public void closedBuffersAreNotReadOnly(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator()) {
Buffer buf = allocator.allocate(8);
buf.readOnly(true);
buf.makeReadOnly();
buf.close();
assertFalse(buf.readOnly());
}
@ -56,18 +54,18 @@ public class BufferReadOnlyTest extends BufferTestSupport {
@ParameterizedTest
@MethodSource("allocators")
public void readOnlyBufferMustBecomeWritableAgainAfterTogglingReadOnlyOff(Fixture fixture) {
public void readOnlyBufferMustMustStayReadOnlyAfterRepeatedToggles(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertFalse(buf.readOnly());
buf.readOnly(true);
buf.makeReadOnly();
assertTrue(buf.readOnly());
verifyWriteInaccessible(buf);
buf.readOnly(false);
assertFalse(buf.readOnly());
buf.makeReadOnly();
assertTrue(buf.readOnly());
verifyWriteAccessible(buf);
verifyWriteInaccessible(buf);
}
}
@ -76,7 +74,7 @@ public class BufferReadOnlyTest extends BufferTestSupport {
public void readOnlyBufferMustRemainReadOnlyAfterSend(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
buf.readOnly(true);
buf.makeReadOnly();
var send = buf.send();
try (Buffer receive = send.receive()) {
assertTrue(receive.readOnly());
@ -89,7 +87,7 @@ public class BufferReadOnlyTest extends BufferTestSupport {
public void readOnlyBufferMustRemainReadOnlyAfterSendForEmptyCompositeBuffer() {
try (BufferAllocator allocator = BufferAllocator.heap();
Buffer buf = CompositeBuffer.compose(allocator)) {
buf.readOnly(true);
buf.makeReadOnly();
var send = buf.send();
try (Buffer receive = send.receive()) {
assertTrue(receive.readOnly());
@ -104,7 +102,7 @@ public class BufferReadOnlyTest extends BufferTestSupport {
for (int i = 0; i < 1000; i++) {
try (Buffer buf = allocator.allocate(8)) {
assertFalse(buf.readOnly());
buf.readOnly(true);
buf.makeReadOnly();
assertTrue(buf.readOnly());
}
}
@ -116,7 +114,7 @@ public class BufferReadOnlyTest extends BufferTestSupport {
public void compactOnReadOnlyBufferMustThrow(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
buf.readOnly(true);
buf.makeReadOnly();
assertThrows(IllegalStateException.class, () -> buf.compact());
}
}
@ -126,7 +124,7 @@ public class BufferReadOnlyTest extends BufferTestSupport {
public void ensureWritableOnReadOnlyBufferMustThrow(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
buf.readOnly(true);
buf.makeReadOnly();
assertThrows(IllegalStateException.class, () -> buf.ensureWritable(1));
}
}
@ -136,7 +134,7 @@ public class BufferReadOnlyTest extends BufferTestSupport {
public void copyIntoOnReadOnlyBufferMustThrow(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer dest = allocator.allocate(8)) {
dest.readOnly(true);
dest.makeReadOnly();
try (Buffer src = allocator.allocate(8)) {
assertThrows(IllegalStateException.class, () -> src.copyInto(0, dest, 0, 1));
}
@ -147,7 +145,7 @@ public class BufferReadOnlyTest extends BufferTestSupport {
@MethodSource("allocators")
public void readOnlyBuffersCannotChangeWriteOffset(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8).readOnly(true)) {
Buffer buf = allocator.allocate(8).makeReadOnly()) {
assertThrows(IllegalStateException.class, () -> buf.writerOffset(4));
}
}
@ -175,67 +173,30 @@ public class BufferReadOnlyTest extends BufferTestSupport {
@ParameterizedTest
@MethodSource("initialNoConstAllocators")
public void modifyingConstBufferDoesNotImpactSiblings(Fixture fixture) {
public void constBuffersCanBeSplit(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator()) {
Supplier<Buffer> supplier = allocator.constBufferSupplier(new byte[] {1, 2, 3, 4});
try (Buffer a = supplier.get().order(BIG_ENDIAN);
Buffer b = supplier.get().order(LITTLE_ENDIAN)) {
assertThat(a.order()).isEqualTo(BIG_ENDIAN);
assertThat(b.order()).isEqualTo(LITTLE_ENDIAN);
a.readOnly(false);
a.setInt(0, 0xA1A2A3A4);
a.readerOffset(2);
assertThat(toByteArray(a)).containsExactly(0xA1, 0xA2, 0xA3, 0xA4);
assertThat(toByteArray(b)).containsExactly(1, 2, 3, 4);
assertThat(b.readerOffset()).isZero();
assertThat(a.order()).isEqualTo(BIG_ENDIAN);
assertThat(b.order()).isEqualTo(LITTLE_ENDIAN);
assertThat(a.writerOffset()).isEqualTo(4);
assertThat(b.writerOffset()).isEqualTo(4);
}
Supplier<Buffer> supplier = allocator.constBufferSupplier(new byte[16]);
verifyConstBufferSplit(supplier);
// These shenanigans must not interfere with the parent const buffer.
verifyConstBufferSplit(supplier);
}
}
@ParameterizedTest
@MethodSource("initialNoConstAllocators")
public void sliceOfConstBufferMustObserveChangesInParent(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer parent = allocator.constBufferSupplier(new byte[] {1, 2, 3, 4}).get();
Buffer slice = parent.slice(0, 4)) {
parent.readOnly(false);
parent.setByte(0, (byte) 42);
assertThat(slice.getByte(0)).isEqualTo((byte) 42);
}
}
@ParameterizedTest
@MethodSource("initialNoConstAllocators")
public void parentOfConstBufferSliceMustObserveChangesInSlice(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer parent = allocator.constBufferSupplier(new byte[] {1, 2, 3, 4}).get();
Buffer slice = parent.slice(0, 4)) {
slice.readOnly(false);
slice.setByte(0, (byte) 42);
assertThat(parent.getByte(0)).isEqualTo((byte) 42);
}
}
@ParameterizedTest
@MethodSource("initialNoConstAllocators")
public void splitsOfConstBuffersCanBecomeWritable(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer a = allocator.constBufferSupplier(new byte[16]).get();
private static void verifyConstBufferSplit(Supplier<Buffer> supplier) {
try (Buffer a = supplier.get();
Buffer b = a.split(8)) {
assertTrue(a.readOnly());
assertTrue(b.readOnly());
assertTrue(a.isOwned());
assertTrue(b.isOwned());
assertThat(a.capacity()).isEqualTo(8);
assertThat(b.capacity()).isEqualTo(8);
a.readOnly(false);
b.readOnly(false);
a.setInt(0, 1);
b.setInt(0, 2);
assertEquals(1, a.getInt(0));
assertEquals(2, b.getInt(0));
try (Buffer c = b.slice()) {
assertTrue(c.readOnly());
assertFalse(c.isOwned());
assertFalse(b.isOwned());
assertThat(c.capacity()).isEqualTo(8);
}
}
}
@ -245,18 +206,25 @@ public class BufferReadOnlyTest extends BufferTestSupport {
try (BufferAllocator allocator = fixture.createAllocator()) {
Supplier<Buffer> supplier = allocator.constBufferSupplier(new byte[] {1, 2, 3, 4});
try (Buffer a = supplier.get();
Buffer b = supplier.get()) {
a.readShort();
Buffer b = supplier.get();
Buffer c = a.slice()) {
assertEquals(1, a.readByte());
assertEquals(2, a.readByte());
assertThrows(IllegalStateException.class, () -> a.compact()); // Can't compact read-only buffer.
a.readOnly(false).compact(); // Setting read-only to false will deconstify the buffer.
assertEquals(3, a.readByte());
assertEquals(4, a.readByte());
assertEquals(0, a.readableBytes());
assertEquals(1, b.readByte());
assertEquals(2, b.readByte());
assertThrows(IllegalStateException.class, () -> b.compact()); // Can't compact read-only buffer.
assertEquals(3, b.readByte());
assertEquals(4, b.readByte());
assertEquals(1, c.readByte());
assertEquals(2, c.readByte());
assertThrows(IllegalStateException.class, () -> c.compact()); // Can't compact read-only buffer.
assertEquals(3, c.readByte());
assertEquals(4, c.readByte());
}
}
}

View File

@ -694,7 +694,7 @@ public class BufferReferenceCountingTest extends BufferTestSupport {
public void acquireOfReadOnlyBufferMustBeReadOnly(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
buf.readOnly(true);
buf.makeReadOnly();
try (Buffer acquire = buf.acquire()) {
assertTrue(acquire.readOnly());
}
@ -707,7 +707,7 @@ public class BufferReferenceCountingTest extends BufferTestSupport {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
buf.writeLong(0x0102030405060708L);
buf.readOnly(true);
buf.makeReadOnly();
try (Buffer slice = buf.slice()) {
assertTrue(slice.readOnly());
}
@ -720,7 +720,7 @@ public class BufferReferenceCountingTest extends BufferTestSupport {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(16)) {
buf.writeLong(0x0102030405060708L);
buf.readOnly(true);
buf.makeReadOnly();
try (Buffer split = buf.split()) {
assertTrue(split.readOnly());
assertTrue(buf.readOnly());

View File

@ -36,7 +36,7 @@ public class BufferShortOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfShortReadOnlyMustBoundsCheckOnNegativeOffset(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getShort(-1));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getShort(-1));
}
}
@ -91,7 +91,7 @@ public class BufferShortOffsettedAccessorsTest extends BufferTestSupport {
Buffer buf = allocator.allocate(8)) {
short value = 0x0102;
buf.writeShort(value);
buf.readOnly(true).getShort(1);
buf.makeReadOnly().getShort(1);
}
}
@ -100,7 +100,7 @@ public class BufferShortOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfShortReadOnlyMustBoundsCheckWhenReadOffsetAndSizeIsGreaterThanCapacity(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getShort(7));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getShort(7));
}
}
@ -127,7 +127,7 @@ public class BufferShortOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfShortReadOnlyMustNotBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
buf.readOnly(true).getShort(0);
buf.makeReadOnly().getShort(0);
}
}
@ -136,7 +136,7 @@ public class BufferShortOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfShortReadOnlyMustBoundsCheckWhenReadOffsetIsGreaterThanCapacity(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getShort(7));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getShort(7));
}
}
@ -154,7 +154,7 @@ public class BufferShortOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfUnsignedShortReadOnlyMustBoundsCheckOnNegativeOffset(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getUnsignedShort(-1));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getUnsignedShort(-1));
}
}
@ -210,7 +210,7 @@ public class BufferShortOffsettedAccessorsTest extends BufferTestSupport {
Buffer buf = allocator.allocate(8)) {
int value = 0x0102;
buf.writeUnsignedShort(value);
buf.readOnly(true).getUnsignedShort(1);
buf.makeReadOnly().getUnsignedShort(1);
}
}
@ -220,7 +220,7 @@ public class BufferShortOffsettedAccessorsTest extends BufferTestSupport {
Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getUnsignedShort(7));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getUnsignedShort(7));
}
}
@ -247,7 +247,7 @@ public class BufferShortOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfUnsignedShortReadOnlyMustNotBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
buf.readOnly(true).getUnsignedShort(0);
buf.makeReadOnly().getUnsignedShort(0);
}
}
@ -256,7 +256,7 @@ public class BufferShortOffsettedAccessorsTest extends BufferTestSupport {
void offsettedGetOfUnsignedShortReadOnlyMustBoundsCheckWhenReadOffsetIsGreaterThanCapacity(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThrows(IndexOutOfBoundsException.class, () -> buf.readOnly(true).getUnsignedShort(7));
assertThrows(IndexOutOfBoundsException.class, () -> buf.makeReadOnly().getUnsignedShort(7));
}
}

View File

@ -137,23 +137,11 @@ public abstract class BufferTestSupport {
static List<Fixture> initialAllocators() {
return List.of(
new Fixture("heap", BufferAllocator::heap, HEAP),
new Fixture("constHeap", () -> constantBufferBasedAllocator(BufferAllocator.heap()), HEAP, CONST),
new Fixture("constDirect", () -> constantBufferBasedAllocator(BufferAllocator.direct()),
DIRECT, CONST, CLEANER),
new Fixture("direct", BufferAllocator::direct, DIRECT, CLEANER),
new Fixture("pooledHeap", BufferAllocator::pooledHeap, POOLED, HEAP),
new Fixture("pooledDirect", BufferAllocator::pooledDirect, POOLED, DIRECT, CLEANER));
}
private static BufferAllocator constantBufferBasedAllocator(BufferAllocator allocator) {
return size -> {
if (size < 0) {
throw new IllegalArgumentException();
}
return allocator.constBufferSupplier(new byte[size]).get().readOnly(false).reset();
};
}
static List<Fixture> initialFixturesForEachImplementation() {
List<Fixture> initFixtures = initialAllocators();
@ -258,37 +246,6 @@ public abstract class BufferTestSupport {
return buf;
}
@Override
public void close() {
allocator.close();
}
};
}, COMPOSITE));
builder.add(new Fixture(fixture + ".readOnly(true/false)", () -> {
var allocator = fixture.get();
return new BufferAllocator() {
@Override
public Buffer allocate(int size) {
return allocator.allocate(size).readOnly(true).readOnly(false);
}
@Override
public void close() {
allocator.close();
}
};
}, fixture.getProperties()));
builder.add(new Fixture(fixture + ".compose.readOnly(true/false)", () -> {
var allocator = fixture.get();
return new BufferAllocator() {
@Override
public Buffer allocate(int size) {
try (Buffer buf = allocator.allocate(size)) {
CompositeBuffer composite = CompositeBuffer.compose(allocator, buf);
return composite.readOnly(true).readOnly(false);
}
}
@Override
public void close() {
allocator.close();

View File

@ -17,7 +17,6 @@ package io.netty.buffer.api.examples.bytetomessagedecoder;
import io.netty.buffer.api.Buffer;
import io.netty.buffer.api.BufferAllocator;
import io.netty.buffer.api.CompositeBuffer;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.embedded.EmbeddedChannel;
@ -334,7 +333,7 @@ public class ByteToMessageDecoderTest {
@Override
protected void decode(ChannelHandlerContext ctx, Buffer in) { }
});
assertFalse(channel.writeInbound(heap().allocate(8).writeByte((byte) 1).readOnly(true)));
assertFalse(channel.writeInbound(heap().allocate(8).writeByte((byte) 1).makeReadOnly()));
assertFalse(channel.writeInbound(heap().allocate(1).writeByte((byte) 2)));
assertFalse(channel.finish());
}