diff --git a/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBufferBuf.java b/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBufferBuf.java index 037068b03c..406514f384 100644 --- a/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBufferBuf.java +++ b/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBufferBuf.java @@ -51,6 +51,26 @@ class ReadOnlyByteBufferBuf extends AbstractReferenceCountedByteBuf { @Override protected void deallocate() { } + @Override + public boolean isWritable() { + return false; + } + + @Override + public boolean isWritable(int numBytes) { + return false; + } + + @Override + public ByteBuf ensureWritable(int minWritableBytes) { + throw new ReadOnlyBufferException(); + } + + @Override + public int ensureWritable(int minWritableBytes, boolean force) { + return 1; + } + @Override public byte getByte(int index) { ensureAccessible(); diff --git a/buffer/src/test/java/io/netty/buffer/ReadOnlyDirectByteBufferBufTest.java b/buffer/src/test/java/io/netty/buffer/ReadOnlyDirectByteBufferBufTest.java index cbee31c448..d51ce1150b 100644 --- a/buffer/src/test/java/io/netty/buffer/ReadOnlyDirectByteBufferBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/ReadOnlyDirectByteBufferBufTest.java @@ -43,6 +43,58 @@ public class ReadOnlyDirectByteBufferBufTest { buffer(allocate(1)); } + @Test + public void shouldIndicateNotWritable() { + ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer()).clear(); + try { + Assert.assertFalse(buf.isWritable()); + } finally { + buf.release(); + } + } + + @Test + public void shouldIndicateNotWritableAnyNumber() { + ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer()).clear(); + try { + Assert.assertFalse(buf.isWritable(1)); + } finally { + buf.release(); + } + } + + @Test + public void ensureWritableIntStatusShouldFailButNotThrow() { + ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer()).clear(); + try { + int result = buf.ensureWritable(1, false); + Assert.assertEquals(1, result); + } finally { + buf.release(); + } + } + + @Test + public void ensureWritableForceIntStatusShouldFailButNotThrow() { + ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer()).clear(); + try { + int result = buf.ensureWritable(1, true); + Assert.assertEquals(1, result); + } finally { + buf.release(); + } + } + + @Test(expected = ReadOnlyBufferException.class) + public void ensureWritableShouldThrow() { + ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer()).clear(); + try { + buf.ensureWritable(1); + } finally { + buf.release(); + } + } + @Test(expected = ReadOnlyBufferException.class) public void testSetByte() { ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());