From 4a5d7a5a170f22721e3029270023f779f600b87b Mon Sep 17 00:00:00 2001 From: Scott Mitchell Date: Thu, 12 Dec 2019 21:20:30 -0800 Subject: [PATCH] ReadOnlyByteBuf writable bytes Motivation: ReadOnlyByteBuf and ReadOnlyByteBuffer are not writable, but their writableBytes related methods return non-zero values. This is inconsistent with the behavior of these buffer types. Modifications: - ReadOnlyByteBuf and ReadOnlyByteBuffer writableBytes related methods should return 0 Result: More correct ReadOnlyByteBuf and ReadOnlyByteBuffer behavior with respect to writability. --- .../main/java/io/netty/buffer/ByteBuf.java | 23 +++++++++---------- .../java/io/netty/buffer/ReadOnlyByteBuf.java | 10 ++++++++ .../netty/buffer/ReadOnlyByteBufferBuf.java | 10 ++++++++ 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/buffer/src/main/java/io/netty/buffer/ByteBuf.java b/buffer/src/main/java/io/netty/buffer/ByteBuf.java index b1e7a9d65c..df77ac1728 100644 --- a/buffer/src/main/java/io/netty/buffer/ByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/ByteBuf.java @@ -395,20 +395,23 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { public abstract ByteBuf setIndex(int readerIndex, int writerIndex); /** - * Returns the number of readable bytes which is equal to - * {@code (this.writerIndex - this.readerIndex)}. + * Returns the number of readable bytes which is logically equivalent to + * {@code (this.writerIndex - this.readerIndex)}, but maybe overridden to accommodate + * specialized behavior (e.g. write only). */ public abstract int readableBytes(); /** - * Returns the number of writable bytes which is equal to - * {@code (this.capacity - this.writerIndex)}. + * Returns the number of writable bytes which is logically equivalent to + * {@code (this.capacity - this.writerIndex)}, but maybe overridden to accommodate + * specialized behavior (e.g. read only). */ public abstract int writableBytes(); /** - * Returns the maximum possible number of writable bytes, which is equal to - * {@code (this.maxCapacity - this.writerIndex)}. + * Returns the maximum possible number of writable bytes, which is logically equivalent to + * {@code (this.maxCapacity - this.writerIndex)}, but maybe overridden to accommodate + * specialized behavior (e.g. read only). */ public abstract int maxWritableBytes(); @@ -422,9 +425,7 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { } /** - * Returns {@code true} - * if and only if {@code (this.writerIndex - this.readerIndex)} is greater - * than {@code 0}. + * Returns {@code true} if and only if {@link #readableBytes()} is greater than {@code 0}. */ public abstract boolean isReadable(); @@ -434,9 +435,7 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { public abstract boolean isReadable(int size); /** - * Returns {@code true} - * if and only if {@code (this.capacity - this.writerIndex)} is greater - * than {@code 0}. + * Returns {@code true} if and only if {@link #writableBytes()} is greater than {@code 0}. */ public abstract boolean isWritable(); diff --git a/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBuf.java b/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBuf.java index 7d5b651943..979916c985 100644 --- a/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBuf.java @@ -75,6 +75,16 @@ public class ReadOnlyByteBuf extends AbstractDerivedByteBuf { throw new ReadOnlyBufferException(); } + @Override + public int writableBytes() { + return 0; + } + + @Override + public int maxWritableBytes() { + return 0; + } + @Override public ByteBuf unwrap() { return buffer; diff --git a/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBufferBuf.java b/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBufferBuf.java index 6a9f82c85b..1e0bf73958 100644 --- a/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBufferBuf.java +++ b/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBufferBuf.java @@ -70,6 +70,16 @@ class ReadOnlyByteBufferBuf extends AbstractReferenceCountedByteBuf { return 1; } + @Override + public int writableBytes() { + return 0; + } + + @Override + public int maxWritableBytes() { + return 0; + } + @Override public byte getByte(int index) { ensureAccessible();