From 1eab02881ee8864e1e289bf07f83c1701ced51b0 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Fri, 8 Aug 2008 08:14:32 +0000 Subject: [PATCH] * Renamed ChannelBuffer.writeNul() to writeZero(); * Added ChannelBuffer.setZero() --- .../netty/buffer/AbstractChannelBuffer.java | 34 +++- .../org/jboss/netty/buffer/ChannelBuffer.java | 169 +++++++++++++++++- .../netty/buffer/DynamicChannelBuffer.java | 4 +- .../codec/replay/ReplayingDecoderBuffer.java | 6 +- 4 files changed, 208 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/jboss/netty/buffer/AbstractChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/AbstractChannelBuffer.java index 69c483c506..2cb5ba3f92 100644 --- a/src/main/java/org/jboss/netty/buffer/AbstractChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/AbstractChannelBuffer.java @@ -139,6 +139,38 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer { setBytes(index, src, src.readerIndex(), src.readableBytes()); } + public void setZero(int index, int length) { + if (length == 0) { + return; + } + if (length < 0) { + throw new IllegalArgumentException( + "length must be 0 or greater than 0."); + } + + int nLong = length >>> 3; + int nBytes = length & 7; + for (int i = nLong; i > 0; i --) { + setLong(index, 0); + index += 8; + } + if (nBytes == 4) { + setInt(index, 0); + } else if (nBytes < 4) { + for (int i = nBytes; i > 0; i --) { + setByte(index, (byte) 0); + index ++; + } + } else { + setInt(index, 0); + index += 4; + for (int i = nBytes - 4; i > 0; i --) { + setByte(index, (byte) 0); + index ++; + } + } + } + public byte readByte() { if (readerIndex == writerIndex) { throw new IndexOutOfBoundsException(); @@ -327,7 +359,7 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer { return writtenBytes; } - public void writeNul(int length) { + public void writeZero(int length) { if (length == 0) { return; } diff --git a/src/main/java/org/jboss/netty/buffer/ChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/ChannelBuffer.java index 14fa9e752c..41677dabea 100644 --- a/src/main/java/org/jboss/netty/buffer/ChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/ChannelBuffer.java @@ -401,24 +401,189 @@ public interface ChannelBuffer extends Comparable { */ void discardReadBytes(); + /** + * Gets a byte at the specified absolute {@code index} in this buffer. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0} or + * {@code index + 1} is greater than {@code capacity} + */ byte getByte(int index); + + /** + * Gets a 16-bit short integer at the specified absolute {@code index} in + * this buffer. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0} or + * {@code index + 2} is greater than {@code capacity} + */ short getShort(int index); + + /** + * Gets a 24-bit medium integer at the specified absolute {@code index} in + * this buffer. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0} or + * {@code index + 3} is greater than {@code capacity} + */ int getMedium(int index); + + /** + * Gets a 32-bit integer at the specified absolute {@code index} in + * this buffer. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0} or + * {@code index + 4} is greater than {@code capacity} + */ int getInt(int index); + + /** + * Gets a 64-bit long integer at the specified absolute {@code index} in + * this buffer. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0} or + * {@code index + 8} is greater than {@code capacity} + */ long getLong(int index); + /** + * Transfers this buffer's data to the specified destination starting at + * the specified absolute {@code index} until the destination becomes + * unwritable. This method is basically same with {@link #getBytes(int, ChannelBuffer, int, int)}, + * except that this method advances the {@code writerIndex} of the + * destination while {@link #getBytes(int, ChannelBuffer, int, int)} + * doesn't. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0} or + * if {@code index + dst.writableBytes} is greater than {@code capacity} + */ void getBytes(int index, ChannelBuffer dst); + + /** + * Transfers this buffer's data to the specified destination starting at + * the specified absolute {@code index}. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0}, + * if the specified {@code dstIndex} is less than {@code 0}, + * if {@code index + length} is greater than {@code capacity}, or + * if {@code dstIndex + length} is greater than {@code dst.capacity} + */ void getBytes(int index, ChannelBuffer dst, int dstIndex, int length); + + /** + * Transfers this buffer's data to the specified destination starting at + * the specified absolute {@code index}. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0} or + * if {@code index + dst.length} is greater than {@code capacity} + */ void getBytes(int index, byte[] dst); + + /** + * Transfers this buffer's data to the specified destination starting at + * the specified absolute {@code index}. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0}, + * if the specified {@code dstIndex} is less than {@code 0}, + * if {@code index + length} is greater than {@code capacity}, or + * if {@code dstIndex + length} is greater than {@code dst.lenggth} + */ void getBytes(int index, byte[] dst, int dstIndex, int length); + + /** + * Transfers this buffer's data to the specified destination starting at + * the specified absolute {@code index} until the destination's position + * reaches to its limit. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0} or + * if {@code index + dst.remaining()} is greater than {@code capacity} + */ void getBytes(int index, ByteBuffer dst); + + /** + * Transfers this buffer's data to the specified stream starting at the + * specified absolute {@code index}. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0} or + * if {@code index + length} is greater than {@code capacity} + * @throws IOException + * if the specified stream threw an exception during I/O + */ void getBytes(int index, OutputStream out, int length) throws IOException; + + /** + * Transfers this buffer's data to the specified channel starting at the + * specified absolute {@code index}. + * + * @return the number of bytes written out to the specified channel + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0} or + * if {@code index + length} is greater than {@code capacity} + * @throws IOException + * if the specified channel threw an exception during I/O + */ int getBytes(int index, GatheringByteChannel out, int length) throws IOException; + /** + * Sets the specified byte at the specified absolute {@code index} in this + * buffer. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0} or + * {@code index + 1} is greater than {@code capacity} + */ void setByte(int index, byte value); + + /** + * Sets the specified 16-bit short integer at the specified absolute + * {@code index} in this buffer. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0} or + * {@code index + 2} is greater than {@code capacity} + */ void setShort(int index, short value); + + /** + * Sets the specified 24-bit medium integer at the specified absolute + * {@code index} in this buffer. Please note that the most significant + * byte is ignored in the specified value. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0} or + * {@code index + 3} is greater than {@code capacity} + */ void setMedium(int index, int value); + + /** + * Sets the specified 32-bit integer at the specified absolute + * {@code index} in this buffer. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0} or + * {@code index + 4} is greater than {@code capacity} + */ void setInt(int index, int value); + + /** + * Sets the specified 64-bit long integer at the specified absolute + * {@code index} in this buffer. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0} or + * {@code index + 8} is greater than {@code capacity} + */ void setLong(int index, long value); void setBytes(int index, ChannelBuffer src); @@ -429,6 +594,8 @@ public interface ChannelBuffer extends Comparable { void setBytes(int index, InputStream in, int length) throws IOException; int setBytes(int index, ScatteringByteChannel in, int length) throws IOException; + void setZero(int index, int length); + byte readByte(); short readShort(); int readMedium(); @@ -465,7 +632,7 @@ public interface ChannelBuffer extends Comparable { void writeBytes(InputStream in, int length) throws IOException; int writeBytes(ScatteringByteChannel in, int length) throws IOException; - void writeNul(int length); + void writeZero(int length); int indexOf(int fromIndex, int toIndex, byte value); int indexOf(int fromIndex, int toIndex, ChannelBufferIndexFinder indexFinder); diff --git a/src/main/java/org/jboss/netty/buffer/DynamicChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/DynamicChannelBuffer.java index a34cca70e5..78191753a5 100644 --- a/src/main/java/org/jboss/netty/buffer/DynamicChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/DynamicChannelBuffer.java @@ -201,9 +201,9 @@ public class DynamicChannelBuffer extends AbstractChannelBuffer { } @Override - public void writeNul(int length) { + public void writeZero(int length) { ensureWritableBytes(length); - super.writeNul(length); + super.writeZero(length); } public ChannelBuffer duplicate() { 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 c4155f50c0..3e99085266 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 @@ -320,6 +320,10 @@ class ReplayingDecoderBuffer implements ChannelBuffer { reject(); } + public void setZero(int index, int length) { + reject(); + } + public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException { reject(); @@ -453,7 +457,7 @@ class ReplayingDecoderBuffer implements ChannelBuffer { reject(); } - public void writeNul(int length) { + public void writeZero(int length) { reject(); }