Add ByteBuf.isReadOnly()

Motivation:

It is sometimes useful to determins if a buffer is read-only.

Modifications:

Add ByteBuf.isReadOnly()

Result:

One more feature
This commit is contained in:
Trustin Lee 2016-04-13 18:21:17 +09:00
parent 57063b6db0
commit 0b078314b2
10 changed files with 46 additions and 2 deletions

View File

@ -69,6 +69,11 @@ public abstract class AbstractByteBuf extends ByteBuf {
this.maxCapacity = maxCapacity;
}
@Override
public boolean isReadOnly() {
return false;
}
@Override
public int maxCapacity() {
return maxCapacity;

View File

@ -70,6 +70,11 @@ public abstract class AbstractDerivedByteBuf extends AbstractByteBuf {
return unwrap().release(decrement);
}
@Override
public boolean isReadOnly() {
return unwrap().isReadOnly();
}
@Override
public ByteBuffer internalNioBuffer(int index, int length) {
return nioBuffer(index, length);

View File

@ -295,6 +295,11 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable<ByteBuf> {
*/
public abstract boolean isDirect();
/**
* Returns {@code true} if and only if this buffer is read-only.
*/
public abstract boolean isReadOnly();
/**
* Returns the {@code readerIndex} of this buffer.
*/

View File

@ -95,6 +95,11 @@ public final class EmptyByteBuf extends ByteBuf {
return null;
}
@Override
public boolean isReadOnly() {
return false;
}
@Override
public boolean isDirect() {
return true;

View File

@ -50,6 +50,11 @@ public class ReadOnlyByteBuf extends AbstractDerivedByteBuf {
setIndex(buffer.readerIndex(), buffer.writerIndex());
}
@Override
public boolean isReadOnly() {
return true;
}
@Override
public boolean isWritable() {
return false;

View File

@ -315,6 +315,11 @@ class ReadOnlyByteBufferBuf extends AbstractReferenceCountedByteBuf {
return null;
}
@Override
public boolean isReadOnly() {
return buffer.isReadOnly();
}
@Override
public boolean isDirect() {
return buffer.isDirect();

View File

@ -92,6 +92,11 @@ public class SwappedByteBuf extends ByteBuf {
return buf.maxCapacity();
}
@Override
public boolean isReadOnly() {
return buf.isReadOnly();
}
@Override
public boolean isDirect() {
return buf.isDirect();

View File

@ -93,6 +93,11 @@ class WrappedByteBuf extends ByteBuf {
return buf;
}
@Override
public boolean isReadOnly() {
return buf.isReadOnly();
}
@Override
public final boolean isDirect() {
return buf.isDirect();

View File

@ -39,7 +39,6 @@ import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.CompositeByteBuf;
import io.netty.buffer.EmptyByteBuf;
import io.netty.buffer.ReadOnlyByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
@ -127,7 +126,7 @@ public class Http2FrameRoundtripTest {
// Now verify that all of the reference counts are zero.
for (ByteBuf buf : needReleasing) {
int expectedFinalRefCount = 0;
if (buf instanceof ReadOnlyByteBuf || buf instanceof EmptyByteBuf) {
if (buf.isReadOnly() || buf instanceof EmptyByteBuf) {
// Special case for when we're writing slices of the padding buffer.
expectedFinalRefCount = 1;
}

View File

@ -88,6 +88,11 @@ final class ReplayingDecoderByteBuf extends ByteBuf {
return buffer.alloc();
}
@Override
public boolean isReadOnly() {
return false;
}
@Override
public boolean isDirect() {
return buffer.isDirect();