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);
/**
* 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<ByteBuf> {
}
/**
* 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<ByteBuf> {
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();

View File

@ -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;

View File

@ -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();