Related issue: NETTY-253 (Add several useful getters and setters to ChannelBuffer class)

* Added getters and setters for char, float, and double
This commit is contained in:
Trustin Lee 2009-11-25 06:51:43 +00:00
parent 2b425a78c7
commit 2c3ab480a2
6 changed files with 242 additions and 16 deletions

View File

@ -136,6 +136,18 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
return getInt(index) & 0xFFFFFFFFL;
}
public char getChar(int index) {
return (char) getShort(index);
}
public float getFloat(int index) {
return Float.intBitsToFloat(getInt(index));
}
public double getDouble(int index) {
return Double.longBitsToDouble(getLong(index));
}
public void getBytes(int index, byte[] dst) {
getBytes(index, dst, 0, dst.length);
}
@ -152,6 +164,18 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
dst.writerIndex(dst.writerIndex() + length);
}
public void setChar(int index, char value) {
setShort(index, (short) value);
}
public void setFloat(int index, float value) {
setInt(index, Float.floatToRawIntBits(value));
}
public void setDouble(int index, double value) {
setLong(index, Double.doubleToRawLongBits(value));
}
public void setBytes(int index, byte[] src) {
setBytes(index, src, 0, src.length);
}
@ -255,6 +279,18 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
return v;
}
public char readChar() {
return (char) readShort();
}
public float readFloat() {
return Float.intBitsToFloat(readInt());
}
public double readDouble() {
return Double.longBitsToDouble(readLong());
}
public ChannelBuffer readBytes(int length) {
checkReadableBytes(length);
if (length == 0) {
@ -379,6 +415,18 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
writerIndex += 8;
}
public void writeChar(char value) {
writeShort((short) value);
}
public void writeFloat(float value) {
writeInt(Float.floatToRawIntBits(value));
}
public void writeDouble(double value) {
writeLong(Double.doubleToRawLongBits(value));
}
public void writeBytes(byte[] src, int srcIndex, int length) {
setBytes(writerIndex, src, srcIndex, length);
writerIndex += length;

View File

@ -512,6 +512,39 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
*/
long getLong(int index);
/**
* Gets a 2-byte UTF-16 character 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 + 2} is greater than {@code this.capacity}
*/
char getChar(int index);
/**
* Gets a 32-bit floating point number 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 + 4} is greater than {@code this.capacity}
*/
float getFloat(int index);
/**
* Gets a 64-bit floating point number 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 + 8} is greater than {@code this.capacity}
*/
double getDouble(int index);
/**
* Transfers this buffer's data to the specified destination starting at
* the specified absolute {@code index} until the destination becomes
@ -712,6 +745,42 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
*/
void setLong(int index, long value);
/**
* Sets the specified 2-byte UTF-16 character 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 + 2} is greater than {@code this.capacity}
*/
void setChar(int index, char value);
/**
* Sets the specified 32-bit floating-point number 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 + 4} is greater than {@code this.capacity}
*/
void setFloat(int index, float value);
/**
* Sets the specified 64-bit floating-point number 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 + 8} is greater than {@code this.capacity}
*/
void setDouble(int index, double value);
/**
* Transfers the specified source buffer's data to this buffer starting at
* the specified absolute {@code index} until the destination becomes
@ -944,6 +1013,33 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
*/
long readLong();
/**
* Gets a 2-byte UTF-16 character at the current {@code readerIndex}
* and increases the {@code readerIndex} by {@code 2} in this buffer.
*
* @throws IndexOutOfBoundsException
* if {@code this.readableBytes} is less than {@code 2}
*/
char readChar();
/**
* Gets a 32-bit floating point number at the current {@code readerIndex}
* and increases the {@code readerIndex} by {@code 4} in this buffer.
*
* @throws IndexOutOfBoundsException
* if {@code this.readableBytes} is less than {@code 4}
*/
float readFloat();
/**
* Gets a 64-bit floating point number at the current {@code readerIndex}
* and increases the {@code readerIndex} by {@code 8} in this buffer.
*
* @throws IndexOutOfBoundsException
* if {@code this.readableBytes} is less than {@code 8}
*/
double readDouble();
/**
* Transfers this buffer's data to a newly created buffer starting at
* the current {@code readerIndex} and increases the {@code readerIndex}
@ -1185,6 +1281,36 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
*/
void writeLong(long value);
/**
* Sets the specified 2-byte UTF-16 character at the current
* {@code writerIndex} and increases the {@code writerIndex} by {@code 2}
* in this buffer.
*
* @throws IndexOutOfBoundsException
* if {@code this.writableBytes} is less than {@code 2}
*/
void writeChar(char value);
/**
* Sets the specified 32-bit floating point number at the current
* {@code writerIndex} and increases the {@code writerIndex} by {@code 4}
* in this buffer.
*
* @throws IndexOutOfBoundsException
* if {@code this.writableBytes} is less than {@code 4}
*/
void writeFloat(float value);
/**
* Sets the specified 64-bit floating point number at the current
* {@code writerIndex} and increases the {@code writerIndex} by {@code 8}
* in this buffer.
*
* @throws IndexOutOfBoundsException
* if {@code this.writableBytes} is less than {@code 8}
*/
void writeDouble(double value);
/**
* Transfers the specified source buffer's data to this buffer starting at
* the current {@code writerIndex} until the source buffer becomes

View File

@ -66,8 +66,8 @@ public class DelimiterBasedFrameDecoder extends FrameDecoder {
private final ChannelBuffer[] delimiters;
private final int maxFrameLength;
private final boolean stripDelimiter;
private volatile boolean discardingTooLongFrame;
private volatile long tooLongFrameLength;
private boolean discardingTooLongFrame;
private long tooLongFrameLength;
/**
* Creates a new instance.

View File

@ -16,7 +16,6 @@
package org.jboss.netty.handler.codec.frame;
import java.net.SocketAddress;
import java.util.concurrent.atomic.AtomicReference;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
@ -181,8 +180,7 @@ import org.jboss.netty.handler.codec.replay.ReplayingDecoder;
public abstract class FrameDecoder extends SimpleChannelUpstreamHandler {
private final boolean unfold;
private final AtomicReference<ChannelBuffer> cumulation =
new AtomicReference<ChannelBuffer>();
private ChannelBuffer cumulation;
protected FrameDecoder() {
this(false);
@ -321,9 +319,11 @@ public abstract class FrameDecoder extends SimpleChannelUpstreamHandler {
private void cleanup(ChannelHandlerContext ctx, ChannelStateEvent e)
throws Exception {
try {
ChannelBuffer cumulation = this.cumulation.getAndSet(null);
ChannelBuffer cumulation = this.cumulation;
if (cumulation == null) {
return;
} else {
this.cumulation = null;
}
if (cumulation.readable()) {
@ -344,14 +344,12 @@ public abstract class FrameDecoder extends SimpleChannelUpstreamHandler {
}
private ChannelBuffer cumulation(ChannelHandlerContext ctx) {
ChannelBuffer buf = cumulation.get();
if (buf == null) {
buf = ChannelBuffers.dynamicBuffer(
ChannelBuffer c = cumulation;
if (c == null) {
c = ChannelBuffers.dynamicBuffer(
ctx.getChannel().getConfig().getBufferFactory());
if (!cumulation.compareAndSet(null, buf)) {
buf = cumulation.get();
}
cumulation = c;
}
return buf;
return c;
}
}

View File

@ -192,9 +192,9 @@ public class LengthFieldBasedFrameDecoder extends FrameDecoder {
private final int lengthFieldEndOffset;
private final int lengthAdjustment;
private final int initialBytesToStrip;
private volatile boolean discardingTooLongFrame;
private volatile long tooLongFrameLength;
private volatile long bytesToDiscard;
private boolean discardingTooLongFrame;
private long tooLongFrameLength;
private long bytesToDiscard;
/**
* Creates a new instance.

View File

@ -169,6 +169,21 @@ class ReplayingDecoderBuffer implements ChannelBuffer {
return buffer.getUnsignedShort(index);
}
public char getChar(int index) {
checkIndex(index, 2);
return buffer.getChar(index);
}
public float getFloat(int index) {
checkIndex(index, 4);
return buffer.getFloat(index);
}
public double getDouble(int index) {
checkIndex(index, 8);
return buffer.getDouble(index);
}
@Override
public int hashCode() {
throw new UnreplayableOperationException();
@ -335,6 +350,21 @@ class ReplayingDecoderBuffer implements ChannelBuffer {
return buffer.readUnsignedShort();
}
public char readChar() {
checkReadableBytes(2);
return buffer.readChar();
}
public float readFloat() {
checkReadableBytes(4);
return buffer.readFloat();
}
public double readDouble() {
checkReadableBytes(8);
return buffer.readDouble();
}
public void resetReaderIndex() {
buffer.resetReaderIndex();
}
@ -405,6 +435,18 @@ class ReplayingDecoderBuffer implements ChannelBuffer {
throw new UnreplayableOperationException();
}
public void setChar(int index, char value) {
throw new UnreplayableOperationException();
}
public void setFloat(int index, float value) {
throw new UnreplayableOperationException();
}
public void setDouble(int index, double value) {
throw new UnreplayableOperationException();
}
public int skipBytes(ChannelBufferIndexFinder firstIndexFinder) {
int oldReaderIndex = buffer.readerIndex();
int newReaderIndex = buffer.indexOf(oldReaderIndex, buffer.writerIndex(), firstIndexFinder);
@ -551,6 +593,18 @@ class ReplayingDecoderBuffer implements ChannelBuffer {
throw new UnreplayableOperationException();
}
public void writeChar(char value) {
throw new UnreplayableOperationException();
}
public void writeFloat(float value) {
throw new UnreplayableOperationException();
}
public void writeDouble(double value) {
throw new UnreplayableOperationException();
}
private void checkIndex(int index) {
if (index > buffer.writerIndex()) {
throw REPLAY;