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.
This commit is contained in:
Scott Mitchell 2019-12-12 21:20:30 -08:00 committed by Norman Maurer
parent 517da28740
commit 4a5d7a5a17
3 changed files with 31 additions and 12 deletions

View File

@ -395,20 +395,23 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable<ByteBuf> {
public abstract ByteBuf setIndex(int readerIndex, int writerIndex); public abstract ByteBuf setIndex(int readerIndex, int writerIndex);
/** /**
* Returns the number of readable bytes which is equal to * Returns the number of readable bytes which is logically equivalent to
* {@code (this.writerIndex - this.readerIndex)}. * {@code (this.writerIndex - this.readerIndex)}, but maybe overridden to accommodate
* specialized behavior (e.g. write only).
*/ */
public abstract int readableBytes(); public abstract int readableBytes();
/** /**
* Returns the number of writable bytes which is equal to * Returns the number of writable bytes which is logically equivalent to
* {@code (this.capacity - this.writerIndex)}. * {@code (this.capacity - this.writerIndex)}, but maybe overridden to accommodate
* specialized behavior (e.g. read only).
*/ */
public abstract int writableBytes(); public abstract int writableBytes();
/** /**
* Returns the maximum possible number of writable bytes, which is equal to * Returns the maximum possible number of writable bytes, which is logically equivalent to
* {@code (this.maxCapacity - this.writerIndex)}. * {@code (this.maxCapacity - this.writerIndex)}, but maybe overridden to accommodate
* specialized behavior (e.g. read only).
*/ */
public abstract int maxWritableBytes(); public abstract int maxWritableBytes();
@ -422,9 +425,7 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable<ByteBuf> {
} }
/** /**
* Returns {@code true} * Returns {@code true} if and only if {@link #readableBytes()} is greater than {@code 0}.
* if and only if {@code (this.writerIndex - this.readerIndex)} is greater
* than {@code 0}.
*/ */
public abstract boolean isReadable(); public abstract boolean isReadable();
@ -434,9 +435,7 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable<ByteBuf> {
public abstract boolean isReadable(int size); public abstract boolean isReadable(int size);
/** /**
* Returns {@code true} * Returns {@code true} if and only if {@link #writableBytes()} is greater than {@code 0}.
* if and only if {@code (this.capacity - this.writerIndex)} is greater
* than {@code 0}.
*/ */
public abstract boolean isWritable(); public abstract boolean isWritable();

View File

@ -75,6 +75,16 @@ public class ReadOnlyByteBuf extends AbstractDerivedByteBuf {
throw new ReadOnlyBufferException(); throw new ReadOnlyBufferException();
} }
@Override
public int writableBytes() {
return 0;
}
@Override
public int maxWritableBytes() {
return 0;
}
@Override @Override
public ByteBuf unwrap() { public ByteBuf unwrap() {
return buffer; return buffer;

View File

@ -70,6 +70,16 @@ class ReadOnlyByteBufferBuf extends AbstractReferenceCountedByteBuf {
return 1; return 1;
} }
@Override
public int writableBytes() {
return 0;
}
@Override
public int maxWritableBytes() {
return 0;
}
@Override @Override
public byte getByte(int index) { public byte getByte(int index) {
ensureAccessible(); ensureAccessible();