From 507b9d0b701b6e3c4b6ff7a27c51e0f20a8670cf Mon Sep 17 00:00:00 2001 From: Cruz Bishop Date: Mon, 7 Nov 2011 21:07:54 +1000 Subject: [PATCH 1/2] Boolean operations in channel buffers Adds getBoolean(index), readBoolean(), writeBoolean(value), setBoolean(index, value) Fixes https://issues.jboss.org/browse/NETTY-344 --- .../netty/buffer/AbstractChannelBuffer.java | 22 +++++++- .../org/jboss/netty/buffer/ChannelBuffer.java | 53 ++++++++++++++++--- .../codec/replay/ReplayingDecoderBuffer.java | 22 ++++++++ 3 files changed, 90 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/jboss/netty/buffer/AbstractChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/AbstractChannelBuffer.java index f0d70fe919..7e213513be 100644 --- a/src/main/java/org/jboss/netty/buffer/AbstractChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/AbstractChannelBuffer.java @@ -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) { diff --git a/src/main/java/org/jboss/netty/buffer/ChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/ChannelBuffer.java index 07c3e68936..9482bad8ff 100644 --- a/src/main/java/org/jboss/netty/buffer/ChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/ChannelBuffer.java @@ -448,6 +448,17 @@ public interface ChannelBuffer extends Comparable { * 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 { * 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 { * 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 { * @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 { * @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 { * 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 { * 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 { */ 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 { * @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 diff --git a/src/main/java/org/jboss/netty/handler/codec/replay/ReplayingDecoderBuffer.java b/src/main/java/org/jboss/netty/handler/codec/replay/ReplayingDecoderBuffer.java index 8f801f9b04..dfe00bf5f2 100644 --- a/src/main/java/org/jboss/netty/handler/codec/replay/ReplayingDecoderBuffer.java +++ b/src/main/java/org/jboss/netty/handler/codec/replay/ReplayingDecoderBuffer.java @@ -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) { From dab76e266c935635b8ac22e017edd87b55b8ed59 Mon Sep 17 00:00:00 2001 From: Cruz Bishop Date: Mon, 7 Nov 2011 21:13:45 +1000 Subject: [PATCH 2/2] Added tests --- .../jboss/netty/buffer/AbstractChannelBufferTest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/test/java/org/jboss/netty/buffer/AbstractChannelBufferTest.java b/src/test/java/org/jboss/netty/buffer/AbstractChannelBufferTest.java index f9e39542da..55823907be 100644 --- a/src/test/java/org/jboss/netty/buffer/AbstractChannelBufferTest.java +++ b/src/test/java/org/jboss/netty/buffer/AbstractChannelBufferTest.java @@ -144,6 +144,16 @@ public abstract class AbstractChannelBufferTest { buffer.readerIndex(0); buffer.writerIndex(CAPACITY); } + + @Test(expected=IndexOutOfBoundsException.class) + public void getBooleanBoundaryCheck1() { + buffer.getBoolean(-1); + } + + @Test(expected=IndexOutOfBoundsException.class) + public void getBooleanBoundaryCheck2() { + buffer.getBoolean(buffer.capacity()); + } @Test(expected=IndexOutOfBoundsException.class) public void getByteBoundaryCheck1() {