Boolean operations in channel buffers

Adds getBoolean(index), readBoolean(), writeBoolean(value), setBoolean(index, value)
Fixes https://issues.jboss.org/browse/NETTY-344
This commit is contained in:
Cruz Bishop 2011-11-07 21:07:54 +10:00
parent 53b055fc10
commit 507b9d0b70
3 changed files with 90 additions and 7 deletions

View File

@ -137,6 +137,11 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
throw new IndexOutOfBoundsException();
}
}
@Override
public boolean getBoolean(int index) {
return (getByte(index) == 1);
}
@Override
public short getUnsignedByte(int index) {
@ -195,6 +200,11 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
getBytes(index, dst, dst.writerIndex(), length);
dst.writerIndex(dst.writerIndex() + length);
}
@Override
public void setBoolean(int index, boolean value) {
setByte(index, value ? 1 : 0);
}
@Override
public void setChar(int index, int value) {
@ -262,7 +272,7 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
}
}
}
@Override
public byte readByte() {
if (readerIndex == writerIndex) {
@ -270,6 +280,11 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
}
return getByte(readerIndex ++);
}
@Override
public boolean readBoolean() {
return (readByte() == 1);
}
@Override
public short readUnsignedByte() {
@ -426,6 +441,11 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
}
readerIndex = newReaderIndex;
}
@Override
public void writeBoolean(boolean value) {
writeByte(value ? 1 : 0);
}
@Override
public void writeByte(int value) {

View File

@ -448,6 +448,17 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
* not a dynamic buffer
*/
void ensureWritableBytes(int writableBytes);
/**
* Gets a boolean at the specified absolute (@code index) in this buffer.
* This method does not modify the {@code readerIndex} or {@code writerIndex}
* of this buffer.
*
* @throws IndexOutOfBoundsException
* if the specified {@code index} is less than {@code 0} or
* {@code index + 1} is greater than {@code this.capacity}
*/
boolean getBoolean(int index);
/**
* Gets a byte at the specified absolute {@code index} in this buffer.
@ -668,7 +679,7 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
* if {@code dstIndex + length} is greater than
* {@code dst.length}
*/
void getBytes(int index, byte[] dst, int dstIndex, int length);
void getBytes(int index, byte[] dst, int dstIndex, int length);
/**
* Transfers this buffer's data to the specified destination starting at
@ -682,7 +693,7 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
* if {@code index + dst.remaining()} is greater than
* {@code this.capacity}
*/
void getBytes(int index, ByteBuffer dst);
void getBytes(int index, ByteBuffer dst);
/**
* Transfers this buffer's data to the specified stream starting at the
@ -699,7 +710,7 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
* @throws IOException
* if the specified stream threw an exception during I/O
*/
void getBytes(int index, OutputStream out, int length) throws IOException;
void getBytes(int index, OutputStream out, int length) throws IOException;
/**
* Transfers this buffer's data to the specified channel starting at the
@ -718,8 +729,20 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
* @throws IOException
* if the specified channel threw an exception during I/O
*/
int getBytes(int index, GatheringByteChannel out, int length) throws IOException;
int getBytes(int index, GatheringByteChannel out, int length) throws IOException;
/**
* Sets the specified boolean at the specified absolute {@code index} in this
* buffer.
* This method does not modify {@code readerIndex} or {@code writerIndex} of
* this buffer.
*
* @throws IndexOutOfBoundsException
* if the specified {@code index} is less than {@code 0} or
* {@code index + 1} is greater than {@code this.capacity}
*/
void setBoolean(int index, boolean value);
/**
* Sets the specified byte at the specified absolute {@code index} in this
* buffer. The 24 high-order bits of the specified value are ignored.
@ -730,7 +753,7 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
* if the specified {@code index} is less than {@code 0} or
* {@code index + 1} is greater than {@code this.capacity}
*/
void setByte(int index, int value);
void setByte(int index, int value);
/**
* Sets the specified 16-bit short integer at the specified absolute
@ -969,6 +992,15 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
* if {@code index + length} is greater than {@code this.capacity}
*/
void setZero(int index, int length);
/**
* Gets a boolean at the current {@code readerIndex} and increases
* the {@code readerIndex} by {@code 1} in this buffer.
*
* @throws IndexOutOfBoundsException
* if {@code this.readableBytes} is less than {@code 1}
*/
boolean readBoolean();
/**
* Gets a byte at the current {@code readerIndex} and increases
@ -1229,6 +1261,15 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
*/
void skipBytes(int length);
/**
* Sets the specified boolean at the current {@code writerIndex}
* and increases the {@code writerIndex} by {@code 1} in this buffer.
*
* @throws IndexOutOfBoundsException
* if {@code this.writableBytes} is less than {@code 1}
*/
void writeBoolean(boolean value);
/**
* Sets the specified byte at the current {@code writerIndex}
* and increases the {@code writerIndex} by {@code 1} in this buffer.
@ -1237,7 +1278,7 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
* @throws IndexOutOfBoundsException
* if {@code this.writableBytes} is less than {@code 1}
*/
void writeByte(int value);
void writeByte(int value);
/**
* Sets the specified 16-bit short integer at the current

View File

@ -126,6 +126,12 @@ class ReplayingDecoderBuffer implements ChannelBuffer {
public ChannelBuffer duplicate() {
throw new UnreplayableOperationException();
}
@Override
public boolean getBoolean(int index) {
checkIndex(index);
return buffer.getBoolean(index);
}
@Override
public byte getByte(int index) {
@ -358,6 +364,12 @@ class ReplayingDecoderBuffer implements ChannelBuffer {
return Integer.MAX_VALUE - buffer.readerIndex();
}
}
@Override
public boolean readBoolean() {
checkReadableBytes(1);
return buffer.readBoolean();
}
@Override
public byte readByte() {
@ -506,6 +518,11 @@ class ReplayingDecoderBuffer implements ChannelBuffer {
public void resetWriterIndex() {
throw new UnreplayableOperationException();
}
@Override
public void setBoolean(int index, boolean value) {
throw new UnreplayableOperationException();
}
@Override
public void setByte(int index, int value) {
@ -669,6 +686,11 @@ class ReplayingDecoderBuffer implements ChannelBuffer {
public int writableBytes() {
return 0;
}
@Override
public void writeBoolean(boolean value) {
throw new UnreplayableOperationException();
}
@Override
public void writeByte(int value) {