Make Buffer.ensureWritable throw exceptions consistent with the rest of the API

This commit is contained in:
Chris Vest 2021-06-01 11:35:01 +02:00
parent eaee4350b7
commit 289c9ebba1
7 changed files with 20 additions and 9 deletions

View File

@ -731,6 +731,9 @@ public final class CompositeBuffer extends ResourceSupport<Buffer, CompositeBuff
@Override @Override
public void ensureWritable(int size, int minimumGrowth, boolean allowCompaction) { public void ensureWritable(int size, int minimumGrowth, boolean allowCompaction) {
if (!isAccessible()) {
throw bufferIsClosed(this);
}
if (!isOwned()) { if (!isOwned()) {
throw new IllegalStateException("Buffer is not owned. Only owned buffers can call ensureWritable."); throw new IllegalStateException("Buffer is not owned. Only owned buffers can call ensureWritable.");
} }

View File

@ -392,6 +392,9 @@ class NioBuffer extends ResourceSupport<Buffer, NioBuffer> implements Buffer, Re
@Override @Override
public void ensureWritable(int size, int minimumGrowth, boolean allowCompaction) { public void ensureWritable(int size, int minimumGrowth, boolean allowCompaction) {
if (!isAccessible()) {
throw bufferIsClosed(this);
}
if (!isOwned()) { if (!isOwned()) {
throw attachTrace(new IllegalStateException( throw attachTrace(new IllegalStateException(
"Buffer is not owned. Only owned buffers can call ensureWritable.")); "Buffer is not owned. Only owned buffers can call ensureWritable."));

View File

@ -431,6 +431,9 @@ class UnsafeBuffer extends ResourceSupport<Buffer, UnsafeBuffer> implements Buff
@Override @Override
public void ensureWritable(int size, int minimumGrowth, boolean allowCompaction) { public void ensureWritable(int size, int minimumGrowth, boolean allowCompaction) {
if (!isAccessible()) {
throw bufferIsClosed(this);
}
if (!isOwned()) { if (!isOwned()) {
throw attachTrace(new IllegalStateException( throw attachTrace(new IllegalStateException(
"Buffer is not owned. Only owned buffers can call ensureWritable.")); "Buffer is not owned. Only owned buffers can call ensureWritable."));

View File

@ -518,6 +518,9 @@ class MemSegBuffer extends ResourceSupport<Buffer, MemSegBuffer> implements Buff
@Override @Override
public void ensureWritable(int size, int minimumGrowth, boolean allowCompaction) { public void ensureWritable(int size, int minimumGrowth, boolean allowCompaction) {
if (!isAccessible()) {
throw bufferIsClosed(this);
}
if (!isOwned()) { if (!isOwned()) {
throw attachTrace(new IllegalStateException( throw attachTrace(new IllegalStateException(
"Buffer is not owned. Only owned buffers can call ensureWritable.")); "Buffer is not owned. Only owned buffers can call ensureWritable."));

View File

@ -392,7 +392,7 @@ public class BufferCompositionTest extends BufferTestSupport {
Buffer b = allocator.allocate(4).makeReadOnly(); Buffer b = allocator.allocate(4).makeReadOnly();
Buffer composite = CompositeBuffer.compose(allocator, a.send(), b.send())) { Buffer composite = CompositeBuffer.compose(allocator, a.send(), b.send())) {
assertTrue(composite.readOnly()); assertTrue(composite.readOnly());
verifyWriteInaccessible(composite, BufferReadOnlyException.class, BufferReadOnlyException.class); verifyWriteInaccessible(composite, BufferReadOnlyException.class);
} }
} }

View File

@ -44,7 +44,7 @@ public class BufferReadOnlyTest extends BufferTestSupport {
Buffer buf = allocator.allocate(8)) { Buffer buf = allocator.allocate(8)) {
var b = buf.makeReadOnly(); var b = buf.makeReadOnly();
assertThat(b).isSameAs(buf); assertThat(b).isSameAs(buf);
verifyWriteInaccessible(buf, BufferReadOnlyException.class, BufferReadOnlyException.class); verifyWriteInaccessible(buf, BufferReadOnlyException.class);
} }
} }
@ -67,12 +67,12 @@ public class BufferReadOnlyTest extends BufferTestSupport {
assertFalse(buf.readOnly()); assertFalse(buf.readOnly());
buf.makeReadOnly(); buf.makeReadOnly();
assertTrue(buf.readOnly()); assertTrue(buf.readOnly());
verifyWriteInaccessible(buf, BufferReadOnlyException.class, BufferReadOnlyException.class); verifyWriteInaccessible(buf, BufferReadOnlyException.class);
buf.makeReadOnly(); buf.makeReadOnly();
assertTrue(buf.readOnly()); assertTrue(buf.readOnly());
verifyWriteInaccessible(buf, BufferReadOnlyException.class, BufferReadOnlyException.class); verifyWriteInaccessible(buf, BufferReadOnlyException.class);
} }
} }
@ -85,7 +85,7 @@ public class BufferReadOnlyTest extends BufferTestSupport {
var send = buf.send(); var send = buf.send();
try (Buffer receive = send.receive()) { try (Buffer receive = send.receive()) {
assertTrue(receive.readOnly()); assertTrue(receive.readOnly());
verifyWriteInaccessible(receive, BufferReadOnlyException.class, BufferReadOnlyException.class); verifyWriteInaccessible(receive, BufferReadOnlyException.class);
} }
} }
} }

View File

@ -324,7 +324,7 @@ public abstract class BufferTestSupport {
public static void verifyInaccessible(Buffer buf) { public static void verifyInaccessible(Buffer buf) {
verifyReadInaccessible(buf); verifyReadInaccessible(buf);
verifyWriteInaccessible(buf, BufferClosedException.class, IllegalStateException.class); verifyWriteInaccessible(buf, BufferClosedException.class);
try (BufferAllocator allocator = BufferAllocator.heap(); try (BufferAllocator allocator = BufferAllocator.heap();
Buffer target = allocator.allocate(24)) { Buffer target = allocator.allocate(24)) {
@ -375,8 +375,7 @@ public abstract class BufferTestSupport {
assertThrows(UnsupportedOperationException.class, () -> buf.getDouble(0)); assertThrows(UnsupportedOperationException.class, () -> buf.getDouble(0));
} }
public static void verifyWriteInaccessible(Buffer buf, Class<? extends RuntimeException> expected, public static void verifyWriteInaccessible(Buffer buf, Class<? extends RuntimeException> expected) {
Class<? extends RuntimeException> expectedEnsureWritable) {
assertThrows(expected, () -> buf.writeByte((byte) 32)); assertThrows(expected, () -> buf.writeByte((byte) 32));
assertThrows(expected, () -> buf.writeUnsignedByte(32)); assertThrows(expected, () -> buf.writeUnsignedByte(32));
assertThrows(expected, () -> buf.writeChar('3')); assertThrows(expected, () -> buf.writeChar('3'));
@ -403,7 +402,7 @@ public abstract class BufferTestSupport {
assertThrows(expected, () -> buf.setLong(0, 32)); assertThrows(expected, () -> buf.setLong(0, 32));
assertThrows(expected, () -> buf.setDouble(0, 32)); assertThrows(expected, () -> buf.setDouble(0, 32));
assertThrows(expectedEnsureWritable, () -> buf.ensureWritable(1)); assertThrows(expected, () -> buf.ensureWritable(1));
final RuntimeException f1e = assertThrows(RuntimeException.class, () -> buf.fill((byte) 0)); final RuntimeException f1e = assertThrows(RuntimeException.class, () -> buf.fill((byte) 0));
assertTrue(f1e instanceof IllegalStateException || f1e instanceof UnsupportedOperationException); assertTrue(f1e instanceof IllegalStateException || f1e instanceof UnsupportedOperationException);
try (BufferAllocator allocator = BufferAllocator.heap(); try (BufferAllocator allocator = BufferAllocator.heap();