From c840a41e319a792c69d60b840103d7a27f7e9d0e Mon Sep 17 00:00:00 2001 From: Riley Park Date: Fri, 28 May 2021 06:13:16 -0700 Subject: [PATCH] Move to custom exception types --- .../main/java/io/netty/buffer/api/Buffer.java | 16 ++- .../buffer/api/BufferClosedException.java | 27 ++++ .../buffer/api/BufferReadOnlyException.java | 27 ++++ .../io/netty/buffer/api/CompositeBuffer.java | 24 ++-- .../buffer/api/adaptor/ByteBufAdaptor.java | 101 +++++++------- .../buffer/api/bytebuffer/NioBuffer.java | 68 +++++---- .../buffer/api/internal/ResourceSupport.java | 4 +- .../io/netty/buffer/api/internal/Statics.java | 10 +- .../netty/buffer/api/unsafe/UnsafeBuffer.java | 26 ++-- .../netty/buffer/api/memseg/MemSegBuffer.java | 25 ++-- .../tests/BufferComponentIterationTest.java | 8 +- .../api/tests/BufferCompositionTest.java | 3 +- .../buffer/api/tests/BufferReadOnlyTest.java | 23 +-- .../netty/buffer/api/tests/BufferRefTest.java | 40 +----- .../tests/BufferReferenceCountingTest.java | 3 +- .../buffer/api/tests/BufferTestSupport.java | 132 +++++++++--------- .../io/netty/buffer/api/tests/ScopeTest.java | 5 + 17 files changed, 307 insertions(+), 235 deletions(-) create mode 100644 buffer-api/src/main/java/io/netty/buffer/api/BufferClosedException.java create mode 100644 buffer-api/src/main/java/io/netty/buffer/api/BufferReadOnlyException.java diff --git a/buffer-api/src/main/java/io/netty/buffer/api/Buffer.java b/buffer-api/src/main/java/io/netty/buffer/api/Buffer.java index 88cba0b..665a986 100644 --- a/buffer-api/src/main/java/io/netty/buffer/api/Buffer.java +++ b/buffer-api/src/main/java/io/netty/buffer/api/Buffer.java @@ -149,7 +149,8 @@ public interface Buffer extends Resource, BufferAccessors { * @return This Buffer. * @throws IndexOutOfBoundsException if the specified {@code offset} is less than the current * {@link #readerOffset()} or greater than {@link #capacity()}. - * @throws IllegalStateException if this buffer is {@linkplain #readOnly() read-only}. + * @throws BufferClosedException if this buffer is closed. + * @throws BufferReadOnlyException if this buffer is {@linkplain #readOnly() read-only}. */ Buffer writerOffset(int offset); @@ -174,7 +175,7 @@ public interface Buffer extends Resource, BufferAccessors { * * @param value The byte value to write at every position in the buffer. * @return This Buffer. - * @throws IllegalStateException if this buffer is {@linkplain #readOnly() read-only}. + * @throws BufferReadOnlyException if this buffer is {@linkplain #readOnly() read-only}. */ Buffer fill(byte value); @@ -377,7 +378,9 @@ public interface Buffer extends Resource, BufferAccessors { * {@code false}. * * @param size The requested number of bytes of space that should be available for writing. - * @throws IllegalStateException if this buffer is in a bad state, or is {@linkplain #readOnly() read-only}. + * @throws IllegalStateException if this buffer is in a bad state. + * @throws BufferClosedException if this buffer is closed. + * @throws BufferReadOnlyException if this buffer is {@linkplain #readOnly() read-only}. */ default void ensureWritable(int size) { ensureWritable(size, 1, true); @@ -418,7 +421,9 @@ public interface Buffer extends Resource, BufferAccessors { * @param allowCompaction {@code true} if the method is allowed to modify the * {@linkplain #readerOffset() reader offset} and * {@linkplain #writerOffset() writer offset}, otherwise {@code false}. - * @throws IllegalStateException if this buffer is in a bad state, or is {@linkplain #readOnly() read-only}. + * @throws BufferReadOnlyException if this buffer is {@linkplain #readOnly() read-only}. + * @throws IllegalArgumentException if {@code size} or {@code minimumGrowth} are negative. + * @throws IllegalStateException if this buffer is in a bad state. */ void ensureWritable(int size, int minimumGrowth, boolean allowCompaction); @@ -553,7 +558,8 @@ public interface Buffer extends Resource, BufferAccessors { /** * Discards the read bytes, and moves the buffer contents to the beginning of the buffer. * - * @throws IllegalStateException if this buffer is in a bad state, or is {@linkplain #readOnly() read-only}. + * @throws BufferReadOnlyException if this buffer is {@linkplain #readOnly() read-only}. + * @throws IllegalStateException if this buffer is in a bad state. */ void compact(); diff --git a/buffer-api/src/main/java/io/netty/buffer/api/BufferClosedException.java b/buffer-api/src/main/java/io/netty/buffer/api/BufferClosedException.java new file mode 100644 index 0000000..a47058e --- /dev/null +++ b/buffer-api/src/main/java/io/netty/buffer/api/BufferClosedException.java @@ -0,0 +1,27 @@ +/* + * Copyright 2021 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.buffer.api; + +/** + * An exception thrown when an operation is attempted on a {@link Buffer} when it has been closed. + */ +public final class BufferClosedException extends UnsupportedOperationException { + private static final long serialVersionUID = 85913332711192868L; + + public BufferClosedException(final String message) { + super(message); + } +} diff --git a/buffer-api/src/main/java/io/netty/buffer/api/BufferReadOnlyException.java b/buffer-api/src/main/java/io/netty/buffer/api/BufferReadOnlyException.java new file mode 100644 index 0000000..aaf712a --- /dev/null +++ b/buffer-api/src/main/java/io/netty/buffer/api/BufferReadOnlyException.java @@ -0,0 +1,27 @@ +/* + * Copyright 2021 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.buffer.api; + +/** + * An exception thrown when an operation is attempted on a {@linkplain Buffer#readOnly() read-only} {@link Buffer}. + */ +public final class BufferReadOnlyException extends UnsupportedOperationException { + private static final long serialVersionUID = 4855825594125231593L; + + public BufferReadOnlyException(final String message) { + super(message); + } +} diff --git a/buffer-api/src/main/java/io/netty/buffer/api/CompositeBuffer.java b/buffer-api/src/main/java/io/netty/buffer/api/CompositeBuffer.java index d9746e3..76713ac 100644 --- a/buffer-api/src/main/java/io/netty/buffer/api/CompositeBuffer.java +++ b/buffer-api/src/main/java/io/netty/buffer/api/CompositeBuffer.java @@ -27,6 +27,9 @@ import java.util.Objects; import java.util.Set; import java.util.stream.Stream; +import static io.netty.buffer.api.internal.Statics.bufferIsClosed; +import static io.netty.buffer.api.internal.Statics.bufferIsReadOnly; + /** * The {@code CompositeBuffer} is a concrete {@link Buffer} implementation that make a number of other buffers appear * as one. A composite buffer behaves the same as a normal, non-composite buffer in every way, so you normally don't @@ -332,6 +335,11 @@ public final class CompositeBuffer extends ResourceSupport= size) { // We already have enough space. @@ -975,7 +983,7 @@ public final class CompositeBuffer extends ResourceSupport implements Buffer, Re return "Buffer[roff:" + roff + ", woff:" + woff + ", cap:" + rmem.capacity() + ']'; } + @Override + protected RuntimeException createResourceClosedException() { + return bufferIsClosed(this); + } + @Override public Buffer order(ByteOrder order) { rmem.order(order); @@ -170,7 +176,7 @@ class NioBuffer extends ResourceSupport implements Buffer, Re int capacity = capacity(); checkSet(0, capacity); if (rmem == CLOSED_BUFFER) { - throw bufferIsClosed(); + throw bufferIsClosed(this); } for (int i = 0; i < capacity; i++) { wmem.put(i, value); @@ -218,7 +224,7 @@ class NioBuffer extends ResourceSupport implements Buffer, Re @Override public void copyInto(int srcPos, ByteBuffer dest, int destPos, int length) { if (rmem == CLOSED_BUFFER) { - throw bufferIsClosed(); + throw bufferIsClosed(this); } if (srcPos < 0) { throw new IllegalArgumentException("The srcPos cannot be negative: " + srcPos + '.'); @@ -254,7 +260,7 @@ class NioBuffer extends ResourceSupport implements Buffer, Re @Override public ByteCursor openCursor(int fromOffset, int length) { if (rmem == CLOSED_BUFFER) { - throw bufferIsClosed(); + throw bufferIsClosed(this); } if (fromOffset < 0) { throw new IllegalArgumentException("The fromOffset cannot be negative: " + fromOffset + '.'); @@ -319,7 +325,7 @@ class NioBuffer extends ResourceSupport implements Buffer, Re @Override public ByteCursor openReverseCursor(int fromOffset, int length) { if (rmem == CLOSED_BUFFER) { - throw bufferIsClosed(); + throw bufferIsClosed(this); } if (fromOffset < 0) { throw new IllegalArgumentException("The fromOffset cannot be negative: " + fromOffset + '.'); @@ -397,7 +403,7 @@ class NioBuffer extends ResourceSupport implements Buffer, Re throw new IllegalArgumentException("The minimum growth cannot be negative: " + minimumGrowth + '.'); } if (rmem != wmem) { - throw bufferIsReadOnly(); + throw bufferIsReadOnly(this); } if (writableBytes() >= size) { // We already have enough space. @@ -487,7 +493,7 @@ class NioBuffer extends ResourceSupport implements Buffer, Re throw attachTrace(new IllegalStateException("Buffer must be owned in order to compact.")); } if (readOnly()) { - throw new IllegalStateException("Buffer must be writable in order to compact, but was read-only."); + throw new BufferReadOnlyException("Buffer must be writable in order to compact, but was read-only."); } if (roff == 0) { return; @@ -622,7 +628,7 @@ class NioBuffer extends ResourceSupport implements Buffer, Re } catch (IndexOutOfBoundsException e) { throw checkWriteState(e, woff); } catch (ReadOnlyBufferException e) { - throw bufferIsReadOnly(); + throw bufferIsReadOnly(this); } } @@ -634,7 +640,7 @@ class NioBuffer extends ResourceSupport implements Buffer, Re } catch (IndexOutOfBoundsException e) { throw checkWriteState(e, woff); } catch (ReadOnlyBufferException e) { - throw bufferIsReadOnly(); + throw bufferIsReadOnly(this); } } @@ -647,7 +653,7 @@ class NioBuffer extends ResourceSupport implements Buffer, Re } catch (IndexOutOfBoundsException e) { throw checkWriteState(e, woff); } catch (ReadOnlyBufferException e) { - throw bufferIsReadOnly(); + throw bufferIsReadOnly(this); } } @@ -659,7 +665,7 @@ class NioBuffer extends ResourceSupport implements Buffer, Re } catch (IndexOutOfBoundsException e) { throw checkWriteState(e, woff); } catch (ReadOnlyBufferException e) { - throw bufferIsReadOnly(); + throw bufferIsReadOnly(this); } } @@ -686,7 +692,7 @@ class NioBuffer extends ResourceSupport implements Buffer, Re } catch (IndexOutOfBoundsException e) { throw checkWriteState(e, woff); } catch (ReadOnlyBufferException e) { - throw bufferIsReadOnly(); + throw bufferIsReadOnly(this); } } @@ -698,7 +704,7 @@ class NioBuffer extends ResourceSupport implements Buffer, Re } catch (IndexOutOfBoundsException e) { throw checkWriteState(e, woff); } catch (ReadOnlyBufferException e) { - throw bufferIsReadOnly(); + throw bufferIsReadOnly(this); } } @@ -739,7 +745,7 @@ class NioBuffer extends ResourceSupport implements Buffer, Re } catch (IndexOutOfBoundsException e) { throw checkWriteState(e, woff); } catch (ReadOnlyBufferException e) { - throw bufferIsReadOnly(); + throw bufferIsReadOnly(this); } } @@ -751,7 +757,7 @@ class NioBuffer extends ResourceSupport implements Buffer, Re } catch (IndexOutOfBoundsException e) { throw checkWriteState(e, woff); } catch (ReadOnlyBufferException e) { - throw bufferIsReadOnly(); + throw bufferIsReadOnly(this); } } @@ -764,7 +770,7 @@ class NioBuffer extends ResourceSupport implements Buffer, Re } catch (IndexOutOfBoundsException e) { throw checkWriteState(e, woff); } catch (ReadOnlyBufferException e) { - throw bufferIsReadOnly(); + throw bufferIsReadOnly(this); } } @@ -776,7 +782,7 @@ class NioBuffer extends ResourceSupport implements Buffer, Re } catch (IndexOutOfBoundsException e) { throw checkWriteState(e, woff); } catch (ReadOnlyBufferException e) { - throw bufferIsReadOnly(); + throw bufferIsReadOnly(this); } } @@ -931,7 +937,7 @@ class NioBuffer extends ResourceSupport implements Buffer, Re } catch (IndexOutOfBoundsException e) { throw checkWriteState(e, woff); } catch (ReadOnlyBufferException e) { - throw bufferIsReadOnly(); + throw bufferIsReadOnly(this); } } @@ -943,7 +949,7 @@ class NioBuffer extends ResourceSupport implements Buffer, Re } catch (IndexOutOfBoundsException e) { throw checkWriteState(e, this.woff); } catch (ReadOnlyBufferException e) { - throw bufferIsReadOnly(); + throw bufferIsReadOnly(this); } } @@ -956,7 +962,7 @@ class NioBuffer extends ResourceSupport implements Buffer, Re } catch (IndexOutOfBoundsException e) { throw checkWriteState(e, woff); } catch (ReadOnlyBufferException e) { - throw bufferIsReadOnly(); + throw bufferIsReadOnly(this); } } @@ -968,7 +974,7 @@ class NioBuffer extends ResourceSupport implements Buffer, Re } catch (IndexOutOfBoundsException e) { throw checkWriteState(e, this.woff); } catch (ReadOnlyBufferException e) { - throw bufferIsReadOnly(); + throw bufferIsReadOnly(this); } } @@ -995,7 +1001,7 @@ class NioBuffer extends ResourceSupport implements Buffer, Re } catch (IndexOutOfBoundsException e) { throw checkWriteState(e, woff); } catch (ReadOnlyBufferException e) { - throw bufferIsReadOnly(); + throw bufferIsReadOnly(this); } } @@ -1007,7 +1013,7 @@ class NioBuffer extends ResourceSupport implements Buffer, Re } catch (IndexOutOfBoundsException e) { throw checkWriteState(e, woff); } catch (ReadOnlyBufferException e) { - throw bufferIsReadOnly(); + throw bufferIsReadOnly(this); } } @@ -1034,7 +1040,7 @@ class NioBuffer extends ResourceSupport implements Buffer, Re } catch (IndexOutOfBoundsException e) { throw checkWriteState(e, woff); } catch (ReadOnlyBufferException e) { - throw bufferIsReadOnly(); + throw bufferIsReadOnly(this); } } @@ -1046,7 +1052,7 @@ class NioBuffer extends ResourceSupport implements Buffer, Re } catch (IndexOutOfBoundsException e) { throw checkWriteState(e, woff); } catch (ReadOnlyBufferException e) { - throw bufferIsReadOnly(); + throw bufferIsReadOnly(this); } } @@ -1073,7 +1079,7 @@ class NioBuffer extends ResourceSupport implements Buffer, Re } catch (IndexOutOfBoundsException e) { throw checkWriteState(e, woff); } catch (ReadOnlyBufferException e) { - throw bufferIsReadOnly(); + throw bufferIsReadOnly(this); } } @@ -1085,7 +1091,7 @@ class NioBuffer extends ResourceSupport implements Buffer, Re } catch (IndexOutOfBoundsException e) { throw checkWriteState(e, woff); } catch (ReadOnlyBufferException e) { - throw bufferIsReadOnly(); + throw bufferIsReadOnly(this); } } // @@ -1160,10 +1166,10 @@ class NioBuffer extends ResourceSupport implements Buffer, Re private RuntimeException checkWriteState(IndexOutOfBoundsException ioobe, int offset) { if (rmem == CLOSED_BUFFER) { - return bufferIsClosed(); + return bufferIsClosed(this); } if (wmem != rmem) { - return bufferIsReadOnly(); + return bufferIsReadOnly(this); } IndexOutOfBoundsException exception = outOfBounds(offset); @@ -1173,17 +1179,17 @@ class NioBuffer extends ResourceSupport implements Buffer, Re private RuntimeException readAccessCheckException(int index) { if (rmem == CLOSED_BUFFER) { - throw bufferIsClosed(); + throw bufferIsClosed(this); } return outOfBounds(index); } private RuntimeException writeAccessCheckException(int index) { if (rmem == CLOSED_BUFFER) { - throw bufferIsClosed(); + throw bufferIsClosed(this); } if (wmem != rmem) { - return bufferIsReadOnly(); + return bufferIsReadOnly(this); } return outOfBounds(index); } diff --git a/buffer-api/src/main/java/io/netty/buffer/api/internal/ResourceSupport.java b/buffer-api/src/main/java/io/netty/buffer/api/internal/ResourceSupport.java index 1af1bc4..139512f 100644 --- a/buffer-api/src/main/java/io/netty/buffer/api/internal/ResourceSupport.java +++ b/buffer-api/src/main/java/io/netty/buffer/api/internal/ResourceSupport.java @@ -63,7 +63,7 @@ public abstract class ResourceSupport, T extends ResourceS */ protected final I acquire() { if (acquires < 0) { - throw attachTrace(new IllegalStateException("This resource is closed: " + this + '.')); + throw attachTrace(createResourceClosedException()); } if (acquires == Integer.MAX_VALUE) { throw new IllegalStateException("Cannot acquire more references; counter would overflow."); @@ -73,6 +73,8 @@ public abstract class ResourceSupport, T extends ResourceS return self(); } + protected abstract RuntimeException createResourceClosedException(); + /** * Decrement the reference count, and despose of the resource if the last reference is closed. *

diff --git a/buffer-api/src/main/java/io/netty/buffer/api/internal/Statics.java b/buffer-api/src/main/java/io/netty/buffer/api/internal/Statics.java index f9e9a11..ab01fc2 100644 --- a/buffer-api/src/main/java/io/netty/buffer/api/internal/Statics.java +++ b/buffer-api/src/main/java/io/netty/buffer/api/internal/Statics.java @@ -16,6 +16,8 @@ package io.netty.buffer.api.internal; import io.netty.buffer.api.Buffer; +import io.netty.buffer.api.BufferClosedException; +import io.netty.buffer.api.BufferReadOnlyException; import io.netty.buffer.api.Drop; import java.lang.invoke.MethodHandle; @@ -166,12 +168,12 @@ public interface Statics { dest.position(destPos).put(bbslice(src, srcPos, length)); } - static IllegalStateException bufferIsClosed() { - return new IllegalStateException("This buffer is closed."); + static BufferClosedException bufferIsClosed(Buffer buffer) { + return new BufferClosedException("This buffer is closed: " + buffer); } - static IllegalStateException bufferIsReadOnly() { - return new IllegalStateException("This buffer is read-only."); + static BufferReadOnlyException bufferIsReadOnly(Buffer buffer) { + return new BufferReadOnlyException("This buffer is read-only: " + buffer); } static T acquire(ResourceSupport obj) { diff --git a/buffer-api/src/main/java/io/netty/buffer/api/unsafe/UnsafeBuffer.java b/buffer-api/src/main/java/io/netty/buffer/api/unsafe/UnsafeBuffer.java index 78353ab..d97d750 100644 --- a/buffer-api/src/main/java/io/netty/buffer/api/unsafe/UnsafeBuffer.java +++ b/buffer-api/src/main/java/io/netty/buffer/api/unsafe/UnsafeBuffer.java @@ -19,6 +19,7 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.api.BufferAllocator; import io.netty.buffer.api.AllocatorControl; import io.netty.buffer.api.Buffer; +import io.netty.buffer.api.BufferReadOnlyException; import io.netty.buffer.api.ByteCursor; import io.netty.buffer.api.Drop; import io.netty.buffer.api.Owned; @@ -98,6 +99,11 @@ class UnsafeBuffer extends ResourceSupport implements Buff return "Buffer[roff:" + roff + ", woff:" + woff + ", cap:" + rsize + ']'; } + @Override + protected RuntimeException createResourceClosedException() { + return bufferIsClosed(this); + } + @Override public Buffer order(ByteOrder order) { this.order = order; @@ -143,7 +149,7 @@ class UnsafeBuffer extends ResourceSupport implements Buff public Buffer fill(byte value) { checkSet(0, capacity()); if (rsize == CLOSED_SIZE) { - throw bufferIsClosed(); + throw bufferIsClosed(this); } try { PlatformDependent.setMemory(base, address, rsize, value); @@ -220,7 +226,7 @@ class UnsafeBuffer extends ResourceSupport implements Buff private void checkCopyIntoArgs(int srcPos, int length, int destPos, int destLength) { if (rsize == CLOSED_SIZE) { - throw bufferIsClosed(); + throw bufferIsClosed(this); } if (srcPos < 0) { throw new IllegalArgumentException("The srcPos cannot be negative: " + srcPos + '.'); @@ -245,7 +251,7 @@ class UnsafeBuffer extends ResourceSupport implements Buff public void copyInto(int srcPos, Buffer dest, int destPos, int length) { checkCopyIntoArgs(srcPos, length, destPos, dest.capacity()); if (dest.readOnly()) { - throw bufferIsReadOnly(); + throw bufferIsReadOnly(this); } long nativeAddress = dest.nativeAddress(); try { @@ -272,7 +278,7 @@ class UnsafeBuffer extends ResourceSupport implements Buff @Override public ByteCursor openCursor(int fromOffset, int length) { if (rsize == CLOSED_SIZE) { - throw bufferIsClosed(); + throw bufferIsClosed(this); } if (fromOffset < 0) { throw new IllegalArgumentException("The fromOffset cannot be negative: " + fromOffset + '.'); @@ -347,7 +353,7 @@ class UnsafeBuffer extends ResourceSupport implements Buff @Override public ByteCursor openReverseCursor(int fromOffset, int length) { if (rsize == CLOSED_SIZE) { - throw bufferIsClosed(); + throw bufferIsClosed(this); } if (fromOffset < 0) { throw new IllegalArgumentException("The fromOffset cannot be negative: " + fromOffset + '.'); @@ -436,7 +442,7 @@ class UnsafeBuffer extends ResourceSupport implements Buff throw new IllegalArgumentException("The minimum growth cannot be negative: " + minimumGrowth + '.'); } if (rsize != wsize) { - throw bufferIsReadOnly(); + throw bufferIsReadOnly(this); } if (writableBytes() >= size) { // We already have enough space. @@ -533,7 +539,7 @@ class UnsafeBuffer extends ResourceSupport implements Buff throw attachTrace(new IllegalStateException("Buffer must be owned in order to compact.")); } if (readOnly()) { - throw new IllegalStateException("Buffer must be writable in order to compact, but was read-only."); + throw new BufferReadOnlyException("Buffer must be writable in order to compact, but was read-only."); } if (roff == 0) { return; @@ -1340,17 +1346,17 @@ class UnsafeBuffer extends ResourceSupport implements Buff private RuntimeException readAccessCheckException(int index) { if (rsize == CLOSED_SIZE) { - throw bufferIsClosed(); + throw bufferIsClosed(this); } return outOfBounds(index); } private RuntimeException writeAccessCheckException(int index) { if (rsize == CLOSED_SIZE) { - throw bufferIsClosed(); + throw bufferIsClosed(this); } if (wsize != rsize) { - return bufferIsReadOnly(); + return bufferIsReadOnly(this); } return outOfBounds(index); } diff --git a/buffer-memseg/src/main/java/io/netty/buffer/api/memseg/MemSegBuffer.java b/buffer-memseg/src/main/java/io/netty/buffer/api/memseg/MemSegBuffer.java index 234fe26..840ee94 100644 --- a/buffer-memseg/src/main/java/io/netty/buffer/api/memseg/MemSegBuffer.java +++ b/buffer-memseg/src/main/java/io/netty/buffer/api/memseg/MemSegBuffer.java @@ -39,6 +39,8 @@ import jdk.incubator.foreign.ResourceScope; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import static io.netty.buffer.api.internal.Statics.bufferIsClosed; +import static io.netty.buffer.api.internal.Statics.bufferIsReadOnly; import static jdk.incubator.foreign.MemoryAccess.getByteAtOffset; import static jdk.incubator.foreign.MemoryAccess.getCharAtOffset; import static jdk.incubator.foreign.MemoryAccess.getDoubleAtOffset; @@ -164,6 +166,11 @@ class MemSegBuffer extends ResourceSupport implements Buff return "Buffer[roff:" + roff + ", woff:" + woff + ", cap:" + seg.byteSize() + ']'; } + @Override + protected RuntimeException createResourceClosedException() { + return bufferIsClosed(this); + } + @Override public Buffer order(ByteOrder order) { this.order = order; @@ -338,7 +345,7 @@ class MemSegBuffer extends ResourceSupport implements Buff private void copyInto(int srcPos, MemorySegment dest, int destPos, int length) { if (seg == CLOSED_SEGMENT) { - throw Statics.bufferIsClosed(); + throw bufferIsClosed(this); } if (srcPos < 0) { throw new IllegalArgumentException("The srcPos cannot be negative: " + srcPos + '.'); @@ -373,7 +380,7 @@ class MemSegBuffer extends ResourceSupport implements Buff @Override public ByteCursor openCursor(int fromOffset, int length) { if (seg == CLOSED_SEGMENT) { - throw Statics.bufferIsClosed(); + throw bufferIsClosed(this); } if (fromOffset < 0) { throw new IllegalArgumentException("The fromOffset cannot be negative: " + fromOffset + '.'); @@ -443,7 +450,7 @@ class MemSegBuffer extends ResourceSupport implements Buff @Override public ByteCursor openReverseCursor(int fromOffset, int length) { if (seg == CLOSED_SEGMENT) { - throw Statics.bufferIsClosed(); + throw bufferIsClosed(this); } if (fromOffset < 0) { throw new IllegalArgumentException("The fromOffset cannot be negative: " + fromOffset + '.'); @@ -521,7 +528,7 @@ class MemSegBuffer extends ResourceSupport implements Buff throw new IllegalArgumentException("The minimum growth cannot be negative: " + minimumGrowth + '.'); } if (seg != wseg) { - throw Statics.bufferIsReadOnly(); + throw bufferIsReadOnly(this); } if (writableBytes() >= size) { // We already have enough space. @@ -1184,27 +1191,27 @@ class MemSegBuffer extends ResourceSupport implements Buff private RuntimeException checkWriteState(IndexOutOfBoundsException ioobe) { if (seg == CLOSED_SEGMENT) { - return Statics.bufferIsClosed(); + return bufferIsClosed(this); } if (wseg != seg) { - return Statics.bufferIsReadOnly(); + return bufferIsReadOnly(this); } return ioobe; } private RuntimeException readAccessCheckException(int index) { if (seg == CLOSED_SEGMENT) { - throw Statics.bufferIsClosed(); + throw bufferIsClosed(this); } return outOfBounds(index); } private RuntimeException writeAccessCheckException(int index) { if (seg == CLOSED_SEGMENT) { - throw Statics.bufferIsClosed(); + throw bufferIsClosed(this); } if (wseg != seg) { - return Statics.bufferIsReadOnly(); + return bufferIsReadOnly(this); } return outOfBounds(index); } diff --git a/buffer-tests/src/test/java/io/netty/buffer/api/tests/BufferComponentIterationTest.java b/buffer-tests/src/test/java/io/netty/buffer/api/tests/BufferComponentIterationTest.java index 0f3b541..ab4c78e 100644 --- a/buffer-tests/src/test/java/io/netty/buffer/api/tests/BufferComponentIterationTest.java +++ b/buffer-tests/src/test/java/io/netty/buffer/api/tests/BufferComponentIterationTest.java @@ -17,6 +17,8 @@ package io.netty.buffer.api.tests; import io.netty.buffer.api.Buffer; import io.netty.buffer.api.BufferAllocator; +import io.netty.buffer.api.BufferClosedException; +import io.netty.buffer.api.BufferReadOnlyException; import io.netty.buffer.api.ByteCursor; import io.netty.buffer.api.CompositeBuffer; import org.junit.jupiter.api.Test; @@ -189,7 +191,7 @@ public class BufferComponentIterationTest extends BufferTestSupport { var buf = allocator.allocate(8); buf.writeLong(0); buf.close(); - assertThrows(IllegalStateException.class, () -> buf.forEachReadable(0, (component, index) -> true)); + assertThrows(BufferClosedException.class, () -> buf.forEachReadable(0, (component, index) -> true)); } } @@ -357,7 +359,7 @@ public class BufferComponentIterationTest extends BufferTestSupport { try (BufferAllocator allocator = fixture.createAllocator()) { Buffer buf = allocator.allocate(8); buf.close(); - assertThrows(IllegalStateException.class, () -> buf.forEachWritable(0, (index, component) -> true)); + assertThrows(BufferClosedException.class, () -> buf.forEachWritable(0, (index, component) -> true)); } } @@ -366,7 +368,7 @@ public class BufferComponentIterationTest extends BufferTestSupport { public void forEachWritableOnReadOnlyBufferMustThrow(Fixture fixture) { try (BufferAllocator allocator = fixture.createAllocator(); Buffer buf = allocator.allocate(8).makeReadOnly()) { - assertThrows(IllegalStateException.class, () -> buf.forEachWritable(0, (index, component) -> true)); + assertThrows(BufferReadOnlyException.class, () -> buf.forEachWritable(0, (index, component) -> true)); } } diff --git a/buffer-tests/src/test/java/io/netty/buffer/api/tests/BufferCompositionTest.java b/buffer-tests/src/test/java/io/netty/buffer/api/tests/BufferCompositionTest.java index 5d396fe..bc37a46 100644 --- a/buffer-tests/src/test/java/io/netty/buffer/api/tests/BufferCompositionTest.java +++ b/buffer-tests/src/test/java/io/netty/buffer/api/tests/BufferCompositionTest.java @@ -17,6 +17,7 @@ package io.netty.buffer.api.tests; import io.netty.buffer.api.Buffer; import io.netty.buffer.api.BufferAllocator; +import io.netty.buffer.api.BufferReadOnlyException; import io.netty.buffer.api.CompositeBuffer; import io.netty.buffer.api.Send; import io.netty.buffer.api.internal.ResourceSupport; @@ -391,7 +392,7 @@ public class BufferCompositionTest extends BufferTestSupport { Buffer b = allocator.allocate(4).makeReadOnly(); Buffer composite = CompositeBuffer.compose(allocator, a.send(), b.send())) { assertTrue(composite.readOnly()); - verifyWriteInaccessible(composite); + verifyWriteInaccessible(composite, BufferReadOnlyException.class, BufferReadOnlyException.class); } } diff --git a/buffer-tests/src/test/java/io/netty/buffer/api/tests/BufferReadOnlyTest.java b/buffer-tests/src/test/java/io/netty/buffer/api/tests/BufferReadOnlyTest.java index 3d4bf01..78961fe 100644 --- a/buffer-tests/src/test/java/io/netty/buffer/api/tests/BufferReadOnlyTest.java +++ b/buffer-tests/src/test/java/io/netty/buffer/api/tests/BufferReadOnlyTest.java @@ -17,6 +17,7 @@ package io.netty.buffer.api.tests; import io.netty.buffer.api.Buffer; import io.netty.buffer.api.BufferAllocator; +import io.netty.buffer.api.BufferReadOnlyException; import io.netty.buffer.api.CompositeBuffer; import io.netty.buffer.api.Send; import io.netty.buffer.api.internal.ResourceSupport; @@ -43,7 +44,7 @@ public class BufferReadOnlyTest extends BufferTestSupport { Buffer buf = allocator.allocate(8)) { var b = buf.makeReadOnly(); assertThat(b).isSameAs(buf); - verifyWriteInaccessible(buf); + verifyWriteInaccessible(buf, BufferReadOnlyException.class, BufferReadOnlyException.class); } } @@ -66,12 +67,12 @@ public class BufferReadOnlyTest extends BufferTestSupport { assertFalse(buf.readOnly()); buf.makeReadOnly(); assertTrue(buf.readOnly()); - verifyWriteInaccessible(buf); + verifyWriteInaccessible(buf, BufferReadOnlyException.class, BufferReadOnlyException.class); buf.makeReadOnly(); assertTrue(buf.readOnly()); - verifyWriteInaccessible(buf); + verifyWriteInaccessible(buf, BufferReadOnlyException.class, BufferReadOnlyException.class); } } @@ -84,7 +85,7 @@ public class BufferReadOnlyTest extends BufferTestSupport { var send = buf.send(); try (Buffer receive = send.receive()) { assertTrue(receive.readOnly()); - verifyWriteInaccessible(receive); + verifyWriteInaccessible(receive, BufferReadOnlyException.class, BufferReadOnlyException.class); } } } @@ -121,7 +122,7 @@ public class BufferReadOnlyTest extends BufferTestSupport { try (BufferAllocator allocator = fixture.createAllocator(); Buffer buf = allocator.allocate(8)) { buf.makeReadOnly(); - assertThrows(IllegalStateException.class, () -> buf.compact()); + assertThrows(BufferReadOnlyException.class, () -> buf.compact()); } } @@ -131,7 +132,7 @@ public class BufferReadOnlyTest extends BufferTestSupport { try (BufferAllocator allocator = fixture.createAllocator(); Buffer buf = allocator.allocate(8)) { buf.makeReadOnly(); - assertThrows(IllegalStateException.class, () -> buf.ensureWritable(1)); + assertThrows(BufferReadOnlyException.class, () -> buf.ensureWritable(1)); } } @@ -142,7 +143,7 @@ public class BufferReadOnlyTest extends BufferTestSupport { Buffer dest = allocator.allocate(8)) { dest.makeReadOnly(); try (Buffer src = allocator.allocate(8)) { - assertThrows(IllegalStateException.class, () -> src.copyInto(0, dest, 0, 1)); + assertThrows(BufferReadOnlyException.class, () -> src.copyInto(0, dest, 0, 1)); } } } @@ -152,7 +153,7 @@ public class BufferReadOnlyTest extends BufferTestSupport { public void readOnlyBuffersCannotChangeWriteOffset(Fixture fixture) { try (BufferAllocator allocator = fixture.createAllocator(); Buffer buf = allocator.allocate(8).makeReadOnly()) { - assertThrows(IllegalStateException.class, () -> buf.writerOffset(4)); + assertThrows(BufferReadOnlyException.class, () -> buf.writerOffset(4)); } } @@ -215,19 +216,19 @@ public class BufferReadOnlyTest extends BufferTestSupport { Buffer c = a.copy()) { assertEquals(1, a.readByte()); assertEquals(2, a.readByte()); - assertThrows(IllegalStateException.class, () -> a.compact()); // Can't compact read-only buffer. + assertThrows(BufferReadOnlyException.class, () -> a.compact()); // Can't compact read-only buffer. assertEquals(3, a.readByte()); assertEquals(4, a.readByte()); assertEquals(1, b.readByte()); assertEquals(2, b.readByte()); - assertThrows(IllegalStateException.class, () -> b.compact()); // Can't compact read-only buffer. + assertThrows(BufferReadOnlyException.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. + assertThrows(BufferReadOnlyException.class, () -> c.compact()); // Can't compact read-only buffer. assertEquals(3, c.readByte()); assertEquals(4, c.readByte()); } diff --git a/buffer-tests/src/test/java/io/netty/buffer/api/tests/BufferRefTest.java b/buffer-tests/src/test/java/io/netty/buffer/api/tests/BufferRefTest.java index e71743a..d5d8a55 100644 --- a/buffer-tests/src/test/java/io/netty/buffer/api/tests/BufferRefTest.java +++ b/buffer-tests/src/test/java/io/netty/buffer/api/tests/BufferRefTest.java @@ -17,6 +17,7 @@ package io.netty.buffer.api.tests; import io.netty.buffer.api.Buffer; import io.netty.buffer.api.BufferAllocator; +import io.netty.buffer.api.BufferClosedException; import io.netty.buffer.api.BufferRef; import io.netty.buffer.api.Send; import org.junit.jupiter.api.Test; @@ -24,7 +25,7 @@ import org.junit.jupiter.api.Test; import java.util.concurrent.atomic.AtomicReference; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertThrows; class BufferRefTest { @Test @@ -37,7 +38,7 @@ class BufferRefTest { ref.contents().writeInt(42); assertThat(ref.contents().readInt()).isEqualTo(42); ref.close(); - assertThrows(IllegalStateException.class, () -> ref.contents().writeInt(32)); + assertThrows(BufferClosedException.class, () -> ref.contents().writeInt(32)); } } @@ -49,34 +50,7 @@ class BufferRefTest { ref.contents().writeInt(42); assertThat(ref.contents().readInt()).isEqualTo(42); ref.close(); - assertThrows(IllegalStateException.class, () -> ref.contents().writeInt(32)); - } - } - - @Test - public void mustCloseOwnedBufferWhenReplaced() { - try (BufferAllocator allocator = BufferAllocator.heap()) { - AtomicReference orig = new AtomicReference<>(); - BufferRef ref; - Send s = allocator.allocate(8).send(); - ref = new BufferRef(Send.sending(Buffer.class, () -> { - Buffer b = s.receive(); - orig.set(b); - return b; - })); - - orig.get().writeInt(42); - assertThat(ref.contents().readInt()).isEqualTo(42); - - try (Buffer buf = allocator.allocate(8)) { - ref.replace(buf.send()); // Pass replacement directly. - } - - assertThrows(IllegalStateException.class, () -> orig.get().writeInt(32)); - ref.contents().writeInt(42); - assertThat(ref.contents().readInt()).isEqualTo(42); - ref.close(); - assertThrows(IllegalStateException.class, () -> ref.contents().writeInt(32)); + assertThrows(BufferClosedException.class, () -> ref.contents().writeInt(32)); } } @@ -99,11 +73,11 @@ class BufferRefTest { ref.replace(buf.send()); // Pass replacement via send(). } - assertThrows(IllegalStateException.class, () -> orig.get().writeInt(32)); + assertThrows(BufferClosedException.class, () -> orig.get().writeInt(32)); ref.contents().writeInt(42); assertThat(ref.contents().readInt()).isEqualTo(42); ref.close(); - assertThrows(IllegalStateException.class, () -> ref.contents().writeInt(32)); + assertThrows(BufferClosedException.class, () -> ref.contents().writeInt(32)); } } @@ -113,7 +87,7 @@ class BufferRefTest { BufferRef refA = new BufferRef(allocator.allocate(8).send())) { refA.contents().writeInt(42); Send send = refA.send(); - assertThrows(IllegalStateException.class, () -> refA.contents().readInt()); + assertThrows(BufferClosedException.class, () -> refA.contents().readInt()); try (BufferRef refB = send.receive()) { assertThat(refB.contents().readInt()).isEqualTo(42); } diff --git a/buffer-tests/src/test/java/io/netty/buffer/api/tests/BufferReferenceCountingTest.java b/buffer-tests/src/test/java/io/netty/buffer/api/tests/BufferReferenceCountingTest.java index 956795b..b534719 100644 --- a/buffer-tests/src/test/java/io/netty/buffer/api/tests/BufferReferenceCountingTest.java +++ b/buffer-tests/src/test/java/io/netty/buffer/api/tests/BufferReferenceCountingTest.java @@ -17,6 +17,7 @@ package io.netty.buffer.api.tests; import io.netty.buffer.api.Buffer; import io.netty.buffer.api.BufferAllocator; +import io.netty.buffer.api.BufferClosedException; import io.netty.buffer.api.CompositeBuffer; import io.netty.buffer.api.internal.ResourceSupport; import org.junit.jupiter.api.Test; @@ -77,7 +78,7 @@ public class BufferReferenceCountingTest extends BufferTestSupport { try (BufferAllocator allocator = fixture.createAllocator()) { var buf = allocator.allocate(8); buf.close(); - assertThrows(IllegalStateException.class, () -> acquire((ResourceSupport) buf)); + assertThrows(BufferClosedException.class, () -> acquire((ResourceSupport) buf)); } } diff --git a/buffer-tests/src/test/java/io/netty/buffer/api/tests/BufferTestSupport.java b/buffer-tests/src/test/java/io/netty/buffer/api/tests/BufferTestSupport.java index b6acd29..7535481 100644 --- a/buffer-tests/src/test/java/io/netty/buffer/api/tests/BufferTestSupport.java +++ b/buffer-tests/src/test/java/io/netty/buffer/api/tests/BufferTestSupport.java @@ -17,6 +17,7 @@ package io.netty.buffer.api.tests; import io.netty.buffer.api.Buffer; import io.netty.buffer.api.BufferAllocator; +import io.netty.buffer.api.BufferClosedException; import io.netty.buffer.api.CompositeBuffer; import io.netty.buffer.api.MemoryManagers; import io.netty.buffer.api.internal.ResourceSupport; @@ -323,88 +324,91 @@ public abstract class BufferTestSupport { public static void verifyInaccessible(Buffer buf) { verifyReadInaccessible(buf); - verifyWriteInaccessible(buf); + verifyWriteInaccessible(buf, BufferClosedException.class, IllegalStateException.class); try (BufferAllocator allocator = BufferAllocator.heap(); Buffer target = allocator.allocate(24)) { - assertThrows(IllegalStateException.class, () -> buf.copyInto(0, target, 0, 1)); - assertThrows(IllegalStateException.class, () -> buf.copyInto(0, new byte[1], 0, 1)); - assertThrows(IllegalStateException.class, () -> buf.copyInto(0, ByteBuffer.allocate(1), 0, 1)); + assertThrows(BufferClosedException.class, () -> buf.copyInto(0, target, 0, 1)); + assertThrows(BufferClosedException.class, () -> buf.copyInto(0, new byte[1], 0, 1)); + assertThrows(BufferClosedException.class, () -> buf.copyInto(0, ByteBuffer.allocate(1), 0, 1)); if (CompositeBuffer.isComposite(buf)) { assertThrows(IllegalStateException.class, () -> ((CompositeBuffer) buf).extendWith(target.send())); } } - assertThrows(IllegalStateException.class, () -> buf.split()); - assertThrows(IllegalStateException.class, () -> buf.send()); - assertThrows(IllegalStateException.class, () -> acquire((ResourceSupport) buf)); - assertThrows(IllegalStateException.class, () -> buf.copy()); - assertThrows(IllegalStateException.class, () -> buf.openCursor()); - assertThrows(IllegalStateException.class, () -> buf.openCursor(0, 0)); - assertThrows(IllegalStateException.class, () -> buf.openReverseCursor()); - assertThrows(IllegalStateException.class, () -> buf.openReverseCursor(0, 0)); + assertThrows(BufferClosedException.class, () -> buf.split()); + assertThrows(BufferClosedException.class, () -> buf.send()); + assertThrows(UnsupportedOperationException.class, () -> acquire((ResourceSupport) buf)); + final RuntimeException c1e = assertThrows(RuntimeException.class, () -> buf.copy()); + assertTrue(c1e instanceof UnsupportedOperationException || c1e instanceof IllegalStateException); + assertThrows(BufferClosedException.class, () -> buf.openCursor()); + assertThrows(BufferClosedException.class, () -> buf.openCursor(0, 0)); + assertThrows(BufferClosedException.class, () -> buf.openReverseCursor()); + assertThrows(BufferClosedException.class, () -> buf.openReverseCursor(0, 0)); } public static void verifyReadInaccessible(Buffer buf) { - assertThrows(IllegalStateException.class, () -> buf.readByte()); - assertThrows(IllegalStateException.class, () -> buf.readUnsignedByte()); - assertThrows(IllegalStateException.class, () -> buf.readChar()); - assertThrows(IllegalStateException.class, () -> buf.readShort()); - assertThrows(IllegalStateException.class, () -> buf.readUnsignedShort()); - assertThrows(IllegalStateException.class, () -> buf.readMedium()); - assertThrows(IllegalStateException.class, () -> buf.readUnsignedMedium()); - assertThrows(IllegalStateException.class, () -> buf.readInt()); - assertThrows(IllegalStateException.class, () -> buf.readUnsignedInt()); - assertThrows(IllegalStateException.class, () -> buf.readFloat()); - assertThrows(IllegalStateException.class, () -> buf.readLong()); - assertThrows(IllegalStateException.class, () -> buf.readDouble()); + assertThrows(UnsupportedOperationException.class, () -> buf.readByte()); + assertThrows(UnsupportedOperationException.class, () -> buf.readUnsignedByte()); + assertThrows(UnsupportedOperationException.class, () -> buf.readChar()); + assertThrows(UnsupportedOperationException.class, () -> buf.readShort()); + assertThrows(UnsupportedOperationException.class, () -> buf.readUnsignedShort()); + assertThrows(UnsupportedOperationException.class, () -> buf.readMedium()); + assertThrows(UnsupportedOperationException.class, () -> buf.readUnsignedMedium()); + assertThrows(UnsupportedOperationException.class, () -> buf.readInt()); + assertThrows(UnsupportedOperationException.class, () -> buf.readUnsignedInt()); + assertThrows(UnsupportedOperationException.class, () -> buf.readFloat()); + assertThrows(UnsupportedOperationException.class, () -> buf.readLong()); + assertThrows(UnsupportedOperationException.class, () -> buf.readDouble()); - assertThrows(IllegalStateException.class, () -> buf.getByte(0)); - assertThrows(IllegalStateException.class, () -> buf.getUnsignedByte(0)); - assertThrows(IllegalStateException.class, () -> buf.getChar(0)); - assertThrows(IllegalStateException.class, () -> buf.getShort(0)); - assertThrows(IllegalStateException.class, () -> buf.getUnsignedShort(0)); - assertThrows(IllegalStateException.class, () -> buf.getMedium(0)); - assertThrows(IllegalStateException.class, () -> buf.getUnsignedMedium(0)); - assertThrows(IllegalStateException.class, () -> buf.getInt(0)); - assertThrows(IllegalStateException.class, () -> buf.getUnsignedInt(0)); - assertThrows(IllegalStateException.class, () -> buf.getFloat(0)); - assertThrows(IllegalStateException.class, () -> buf.getLong(0)); - assertThrows(IllegalStateException.class, () -> buf.getDouble(0)); + assertThrows(UnsupportedOperationException.class, () -> buf.getByte(0)); + assertThrows(UnsupportedOperationException.class, () -> buf.getUnsignedByte(0)); + assertThrows(UnsupportedOperationException.class, () -> buf.getChar(0)); + assertThrows(UnsupportedOperationException.class, () -> buf.getShort(0)); + assertThrows(UnsupportedOperationException.class, () -> buf.getUnsignedShort(0)); + assertThrows(UnsupportedOperationException.class, () -> buf.getMedium(0)); + assertThrows(UnsupportedOperationException.class, () -> buf.getUnsignedMedium(0)); + assertThrows(UnsupportedOperationException.class, () -> buf.getInt(0)); + assertThrows(UnsupportedOperationException.class, () -> buf.getUnsignedInt(0)); + assertThrows(UnsupportedOperationException.class, () -> buf.getFloat(0)); + assertThrows(UnsupportedOperationException.class, () -> buf.getLong(0)); + assertThrows(UnsupportedOperationException.class, () -> buf.getDouble(0)); } - public static void verifyWriteInaccessible(Buffer buf) { - assertThrows(IllegalStateException.class, () -> buf.writeByte((byte) 32)); - assertThrows(IllegalStateException.class, () -> buf.writeUnsignedByte(32)); - assertThrows(IllegalStateException.class, () -> buf.writeChar('3')); - assertThrows(IllegalStateException.class, () -> buf.writeShort((short) 32)); - assertThrows(IllegalStateException.class, () -> buf.writeUnsignedShort(32)); - assertThrows(IllegalStateException.class, () -> buf.writeMedium(32)); - assertThrows(IllegalStateException.class, () -> buf.writeUnsignedMedium(32)); - assertThrows(IllegalStateException.class, () -> buf.writeInt(32)); - assertThrows(IllegalStateException.class, () -> buf.writeUnsignedInt(32)); - assertThrows(IllegalStateException.class, () -> buf.writeFloat(3.2f)); - assertThrows(IllegalStateException.class, () -> buf.writeLong(32)); - assertThrows(IllegalStateException.class, () -> buf.writeDouble(32)); + public static void verifyWriteInaccessible(Buffer buf, Class expected, + Class expectedEnsureWritable) { + assertThrows(expected, () -> buf.writeByte((byte) 32)); + assertThrows(expected, () -> buf.writeUnsignedByte(32)); + assertThrows(expected, () -> buf.writeChar('3')); + assertThrows(expected, () -> buf.writeShort((short) 32)); + assertThrows(expected, () -> buf.writeUnsignedShort(32)); + assertThrows(expected, () -> buf.writeMedium(32)); + assertThrows(expected, () -> buf.writeUnsignedMedium(32)); + assertThrows(expected, () -> buf.writeInt(32)); + assertThrows(expected, () -> buf.writeUnsignedInt(32)); + assertThrows(expected, () -> buf.writeFloat(3.2f)); + assertThrows(expected, () -> buf.writeLong(32)); + assertThrows(expected, () -> buf.writeDouble(32)); - assertThrows(IllegalStateException.class, () -> buf.setByte(0, (byte) 32)); - assertThrows(IllegalStateException.class, () -> buf.setUnsignedByte(0, 32)); - assertThrows(IllegalStateException.class, () -> buf.setChar(0, '3')); - assertThrows(IllegalStateException.class, () -> buf.setShort(0, (short) 32)); - assertThrows(IllegalStateException.class, () -> buf.setUnsignedShort(0, 32)); - assertThrows(IllegalStateException.class, () -> buf.setMedium(0, 32)); - assertThrows(IllegalStateException.class, () -> buf.setUnsignedMedium(0, 32)); - assertThrows(IllegalStateException.class, () -> buf.setInt(0, 32)); - assertThrows(IllegalStateException.class, () -> buf.setUnsignedInt(0, 32)); - assertThrows(IllegalStateException.class, () -> buf.setFloat(0, 3.2f)); - assertThrows(IllegalStateException.class, () -> buf.setLong(0, 32)); - assertThrows(IllegalStateException.class, () -> buf.setDouble(0, 32)); + assertThrows(expected, () -> buf.setByte(0, (byte) 32)); + assertThrows(expected, () -> buf.setUnsignedByte(0, 32)); + assertThrows(expected, () -> buf.setChar(0, '3')); + assertThrows(expected, () -> buf.setShort(0, (short) 32)); + assertThrows(expected, () -> buf.setUnsignedShort(0, 32)); + assertThrows(expected, () -> buf.setMedium(0, 32)); + assertThrows(expected, () -> buf.setUnsignedMedium(0, 32)); + assertThrows(expected, () -> buf.setInt(0, 32)); + assertThrows(expected, () -> buf.setUnsignedInt(0, 32)); + assertThrows(expected, () -> buf.setFloat(0, 3.2f)); + assertThrows(expected, () -> buf.setLong(0, 32)); + assertThrows(expected, () -> buf.setDouble(0, 32)); - assertThrows(IllegalStateException.class, () -> buf.ensureWritable(1)); - assertThrows(IllegalStateException.class, () -> buf.fill((byte) 0)); + assertThrows(expectedEnsureWritable, () -> buf.ensureWritable(1)); + final RuntimeException f1e = assertThrows(RuntimeException.class, () -> buf.fill((byte) 0)); + assertTrue(f1e instanceof IllegalStateException || f1e instanceof UnsupportedOperationException); try (BufferAllocator allocator = BufferAllocator.heap(); Buffer source = allocator.allocate(8)) { - assertThrows(IllegalStateException.class, () -> source.copyInto(0, buf, 0, 1)); + assertThrows(expected, () -> source.copyInto(0, buf, 0, 1)); } } diff --git a/buffer-tests/src/test/java/io/netty/buffer/api/tests/ScopeTest.java b/buffer-tests/src/test/java/io/netty/buffer/api/tests/ScopeTest.java index 7ec2e41..99030e0 100644 --- a/buffer-tests/src/test/java/io/netty/buffer/api/tests/ScopeTest.java +++ b/buffer-tests/src/test/java/io/netty/buffer/api/tests/ScopeTest.java @@ -51,6 +51,11 @@ public class ScopeTest { super(drop); } + @Override + protected RuntimeException createResourceClosedException() { + return new IllegalStateException("This resource is closed: " + this); + } + @Override protected Owned prepareSend() { return null;