public class NettyMessageBuffer extends Object implements MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
MessageBuffer
interface. It is a thin
wrapper over the the Netty ChannelBuffer
with some additional methods
for string and object read write. It does not expose all methods of the
ChannelBuffer, instead it has a method getNativeBuffer()
which can
be used to retrieve the buffer and then call the appropriate method. For
writing to the buffer, this class uses DynamicChannelBuffer
implementation.Modifier and Type | Field and Description |
---|---|
private org.jboss.netty.buffer.ChannelBuffer |
buffer |
Constructor and Description |
---|
NettyMessageBuffer() |
NettyMessageBuffer(org.jboss.netty.buffer.ChannelBuffer buffer)
This constructor can be used when trying to read information from a
ChannelBuffer . |
Modifier and Type | Method and Description |
---|---|
byte[] |
array()
Returns the backing byte array of this buffer.
|
void |
clear()
Clears the contents of this buffer.
|
org.jboss.netty.buffer.ChannelBuffer |
getNativeBuffer()
Returns the actual buffer implementation that is wrapped in this
MessageBuffer instance.
|
boolean |
isReadable() |
int |
readableBytes()
Gets the number of readable bytes left in the buffer.
|
int |
readByte()
Read a single signed byte from the current
readerIndex position
of the buffer. |
void |
readBytes(byte[] dst)
Transfers this buffer's data to the specified destination starting at the
current
readerIndex and increases the readerIndex by the
number of the transferred bytes (= dst.length ). |
void |
readBytes(byte[] dst,
int dstIndex,
int length)
Transfers this buffer's data to the specified destination starting at the
current
readerIndex and increases the readerIndex by the
number of the transferred bytes (= length ). |
byte[] |
readBytes(int length) |
char |
readChar()
Gets a 2-byte UTF-16 character at the current
readerIndex and
increases the readerIndex by 2 in this buffer. |
double |
readDouble()
Gets a 64-bit floating point number at the current
readerIndex
and increases the readerIndex by 8 in this buffer. |
float |
readFloat()
Gets a 32-bit floating point number at the current
readerIndex
and increases the readerIndex by 4 in this buffer. |
int |
readInt()
Gets a 32-bit integer at the current
readerIndex and increases
the readerIndex by 4 in this buffer. |
long |
readLong()
Gets a 64-bit integer at the current
readerIndex and increases
the readerIndex by 8 in this buffer. |
int |
readMedium()
Gets a 24-bit medium integer at the current
readerIndex and
increases the readerIndex by 3 in this buffer. |
<V> V |
readObject(Transform<org.jboss.netty.buffer.ChannelBuffer,V> converter)
Reads an object from the underlying buffer and transform the bytes using
the supplied transformer to any desired object.
|
int |
readShort()
Gets a 16-bit short integer at the current
readerIndex and
increases the readerIndex by 2 in this buffer. |
String |
readString() |
String[] |
readStrings(int numOfStrings) |
int |
readUnsignedByte()
Gets an unsigned byte at the current
readerIndex and increases
the readerIndex by 1 in this buffer. |
long |
readUnsignedInt()
Gets an unsigned 32-bit integer at the current
readerIndex and
increases the readerIndex by 4 in this buffer. |
int |
readUnsignedMedium()
Gets an unsigned 24-bit medium integer at the current
readerIndex
and increases the readerIndex by 3 in this buffer. |
int |
readUnsignedShort()
Gets an unsigned 16-bit short integer at the current
readerIndex
and increases the readerIndex by 2 in this buffer. |
MessageBuffer<org.jboss.netty.buffer.ChannelBuffer> |
writeByte(byte b) |
MessageBuffer<org.jboss.netty.buffer.ChannelBuffer> |
writeBytes(byte[] src)
Transfers the specified source array's data to this buffer starting at
the current
writerIndex and increases the writerIndex by
the number of the transferred bytes (= src.length ). |
MessageBuffer<org.jboss.netty.buffer.ChannelBuffer> |
writeChar(int value)
Sets the specified 2-byte UTF-16 character at the current
writerIndex and increases the writerIndex by 2 in
this buffer. |
MessageBuffer<org.jboss.netty.buffer.ChannelBuffer> |
writeDouble(double value)
Sets the specified 64-bit floating point number at the current
writerIndex and increases the writerIndex by 8 in
this buffer. |
MessageBuffer<org.jboss.netty.buffer.ChannelBuffer> |
writeFloat(float value)
Sets the specified 32-bit floating point number at the current
writerIndex and increases the writerIndex by 4 in
this buffer. |
MessageBuffer<org.jboss.netty.buffer.ChannelBuffer> |
writeInt(int value)
Sets the specified 32-bit integer at the current
writerIndex and
increases the writerIndex by 4 in this buffer. |
MessageBuffer<org.jboss.netty.buffer.ChannelBuffer> |
writeLong(long value)
Sets the specified 64-bit long integer at the current
writerIndex
and increases the writerIndex by 8 in this buffer. |
MessageBuffer<org.jboss.netty.buffer.ChannelBuffer> |
writeMedium(int value)
Sets the specified 24-bit medium integer at the current
writerIndex and increases the writerIndex by 3 in
this buffer. |
<V> MessageBuffer<org.jboss.netty.buffer.ChannelBuffer> |
writeObject(Transform<V,org.jboss.netty.buffer.ChannelBuffer> converter,
V object)
Most implementations will write an object to the underlying buffer after
converting the incoming object using the transformer into a byte array.
|
MessageBuffer<org.jboss.netty.buffer.ChannelBuffer> |
writeShort(int value)
Sets the specified 16-bit short integer at the current
writerIndex and increases the writerIndex by 2 in
this buffer. |
MessageBuffer<org.jboss.netty.buffer.ChannelBuffer> |
writeString(String message) |
MessageBuffer<org.jboss.netty.buffer.ChannelBuffer> |
writeStrings(String... messages) |
public NettyMessageBuffer()
public NettyMessageBuffer(org.jboss.netty.buffer.ChannelBuffer buffer)
ChannelBuffer
.buffer
- public boolean isReadable()
isReadable
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public int readableBytes()
MessageBuffer
readableBytes
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public byte[] array()
MessageBuffer
array
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public void clear()
MessageBuffer
clear
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public org.jboss.netty.buffer.ChannelBuffer getNativeBuffer()
MessageBuffer
getNativeBuffer
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
ChannelBuffer
, for a core java implementation
it could be ByteBuffer
public int readByte()
MessageBuffer
readerIndex
position
of the buffer. It will increment the readerIndex after doing this
operation.readByte
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public int readUnsignedByte()
MessageBuffer
readerIndex
and increases
the readerIndex
by 1
in this buffer.readUnsignedByte
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public byte[] readBytes(int length)
readBytes
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public void readBytes(byte[] dst)
MessageBuffer
readerIndex
and increases the readerIndex
by the
number of the transferred bytes (= dst.length
).readBytes
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public void readBytes(byte[] dst, int dstIndex, int length)
MessageBuffer
readerIndex
and increases the readerIndex
by the
number of the transferred bytes (= length
).readBytes
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
dstIndex
- the first index of the destinationlength
- the number of bytes to transferpublic char readChar()
MessageBuffer
readerIndex
and
increases the readerIndex
by 2
in this buffer.readChar
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public int readUnsignedShort()
MessageBuffer
readerIndex
and increases the readerIndex
by 2
in this buffer.readUnsignedShort
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public int readShort()
MessageBuffer
readerIndex
and
increases the readerIndex
by 2
in this buffer.readShort
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public int readUnsignedMedium()
MessageBuffer
readerIndex
and increases the readerIndex
by 3
in this buffer.readUnsignedMedium
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public int readMedium()
MessageBuffer
readerIndex
and
increases the readerIndex
by 3
in this buffer.readMedium
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public long readUnsignedInt()
MessageBuffer
readerIndex
and
increases the readerIndex
by 4
in this buffer.readUnsignedInt
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public int readInt()
MessageBuffer
readerIndex
and increases
the readerIndex
by 4
in this buffer.readInt
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public long readLong()
MessageBuffer
readerIndex
and increases
the readerIndex
by 8
in this buffer.readLong
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public float readFloat()
MessageBuffer
readerIndex
and increases the readerIndex
by 4
in this buffer.readFloat
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public double readDouble()
MessageBuffer
readerIndex
and increases the readerIndex
by 8
in this buffer.readDouble
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public String readString()
readString
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public String[] readStrings(int numOfStrings)
readStrings
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public <V> V readObject(Transform<org.jboss.netty.buffer.ChannelBuffer,V> converter)
MessageBuffer
readObject
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
converter
- The converter which will transform the bytes to relevant
object.public MessageBuffer<org.jboss.netty.buffer.ChannelBuffer> writeByte(byte b)
writeByte
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public MessageBuffer<org.jboss.netty.buffer.ChannelBuffer> writeBytes(byte[] src)
MessageBuffer
writerIndex
and increases the writerIndex
by
the number of the transferred bytes (= src.length
).writeBytes
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public MessageBuffer<org.jboss.netty.buffer.ChannelBuffer> writeChar(int value)
MessageBuffer
writerIndex
and increases the writerIndex
by 2
in
this buffer. The 16 high-order bits of the specified value are ignored.writeChar
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public MessageBuffer<org.jboss.netty.buffer.ChannelBuffer> writeShort(int value)
MessageBuffer
writerIndex
and increases the writerIndex
by 2
in
this buffer. The 16 high-order bits of the specified value are ignored.writeShort
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public MessageBuffer<org.jboss.netty.buffer.ChannelBuffer> writeMedium(int value)
MessageBuffer
writerIndex
and increases the writerIndex
by 3
in
this buffer.writeMedium
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public MessageBuffer<org.jboss.netty.buffer.ChannelBuffer> writeInt(int value)
MessageBuffer
writerIndex
and
increases the writerIndex
by 4
in this buffer.writeInt
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public MessageBuffer<org.jboss.netty.buffer.ChannelBuffer> writeLong(long value)
MessageBuffer
writerIndex
and increases the writerIndex
by 8
in this buffer.writeLong
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public MessageBuffer<org.jboss.netty.buffer.ChannelBuffer> writeFloat(float value)
MessageBuffer
writerIndex
and increases the writerIndex
by 4
in
this buffer.writeFloat
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public MessageBuffer<org.jboss.netty.buffer.ChannelBuffer> writeDouble(double value)
MessageBuffer
writerIndex
and increases the writerIndex
by 8
in
this buffer.writeDouble
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public MessageBuffer<org.jboss.netty.buffer.ChannelBuffer> writeString(String message)
writeString
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public MessageBuffer<org.jboss.netty.buffer.ChannelBuffer> writeStrings(String... messages)
writeStrings
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
public <V> MessageBuffer<org.jboss.netty.buffer.ChannelBuffer> writeObject(Transform<V,org.jboss.netty.buffer.ChannelBuffer> converter, V object)
MessageBuffer
writeObject
in interface MessageBuffer<org.jboss.netty.buffer.ChannelBuffer>
V
- The object to be converted, mostly to a byte array or relevant
buffer implementation.converter
- For most implementations, the converter which will transform
the object to byte array.Copyright © 2013. All Rights Reserved.