public interface Buffer extends ClusterSerializable
A Buffer represents a sequence of zero or more bytes that can be written to or read from, and which expands as
necessary to accommodate any bytes written to it.<p>
There are two ways to write data to a Buffer: The first method involves methods that take the form setXXX
.
These methods write data into the buffer starting at the specified position. The position does not have to be inside data that
has already been written to the buffer; the buffer will automatically expand to encompass the position plus any data that needs
to be written. All positions are measured in bytes and start with zero.<p>
The second method involves methods that take the form appendXXX
; these methods append data
at the end of the buffer.<p>
Methods exist to both set
and append
all primitive types, String
, ByteBuffer
and
other instances of Buffer.<p>
Data can be read from a buffer by invoking methods which take the form getXXX
. These methods take a parameter
representing the position in the Buffer from where to read data.<p>
Once a buffer has been written to a socket or other write stream, the same buffer instance can’t be written again to another WriteStream.<p>
Instances of this class are not thread-safe.<p>
Modifier and Type | Field and Description |
---|---|
static BufferFactory |
factory |
Modifier and Type | Method and Description |
---|---|
Buffer |
appendBuffer(Buffer buff)
Appends the specified
Buffer to the end of this Buffer. |
Buffer |
appendBuffer(Buffer buff,
int offset,
int len)
Appends the specified
Buffer starting at the offset using len to the end of this Buffer. |
Buffer |
appendByte(byte b)
Appends the specified
byte to the end of the Buffer. |
Buffer |
appendBytes(byte[] bytes)
Appends the specified
byte[] to the end of the Buffer. |
Buffer |
appendBytes(byte[] bytes,
int offset,
int len)
Appends the specified number of bytes from
byte[] to the end of the Buffer, starting at the given offset. |
Buffer |
appendDouble(double d)
Appends the specified
double to the end of the Buffer. |
Buffer |
appendFloat(float f)
Appends the specified
float to the end of the Buffer. |
Buffer |
appendInt(int i)
Appends the specified
int to the end of the Buffer. |
Buffer |
appendLong(long l)
Appends the specified
long to the end of the Buffer. |
Buffer |
appendShort(short s)
Appends the specified
short to the end of the Buffer.The buffer will expand as necessary to accommodate any bytes written.<p>
Returns a reference to this so multiple operations can be appended together. |
Buffer |
appendString(String str)
Appends the specified
String str to the end of the Buffer with UTF-8 encoding.<p>
The buffer will expand as necessary to accommodate any bytes written.<p>
Returns a reference to this so multiple operations can be appended together<p> |
Buffer |
appendString(String str,
String enc)
Appends the specified
String to the end of the Buffer with the encoding as specified by enc .<p>
The buffer will expand as necessary to accommodate any bytes written.<p>
Returns a reference to this so multiple operations can be appended together.<p> |
static Buffer |
buffer() |
static Buffer |
buffer(byte[] bytes) |
static Buffer |
buffer(io.netty.buffer.ByteBuf byteBuf) |
static Buffer |
buffer(int initialSizeHint) |
static Buffer |
buffer(String string) |
static Buffer |
buffer(String string,
String enc) |
Buffer |
copy()
Returns a copy of the entire Buffer.
|
Buffer |
getBuffer(int start,
int end)
Returns a copy of a sub-sequence the Buffer as a
Buffer starting at position start
and ending at position end - 1 |
byte |
getByte(int pos)
Returns the
byte at position pos in the Buffer. |
io.netty.buffer.ByteBuf |
getByteBuf()
Returns the Buffer as a Netty
ByteBuf .<p>
This method is meant for internal use only. |
byte[] |
getBytes()
Returns a copy of the entire Buffer as a
byte[] |
byte[] |
getBytes(int start,
int end)
Returns a copy of a sub-sequence the Buffer as a
byte[] starting at position start
and ending at position end - 1 |
double |
getDouble(int pos)
Returns the
double at position pos in the Buffer. |
float |
getFloat(int pos)
Returns the
float at position pos in the Buffer. |
int |
getInt(int pos)
Returns the
int at position pos in the Buffer. |
long |
getLong(int pos)
Returns the
long at position pos in the Buffer. |
short |
getShort(int pos)
Returns the
short at position pos in the Buffer. |
String |
getString(int start,
int end)
Returns a copy of a sub-sequence the Buffer as a
String starting at position start
and ending at position end - 1 interpreted as a String in UTF-8 encoding |
String |
getString(int start,
int end,
String enc)
Returns a copy of a sub-sequence the Buffer as a
String starting at position start
and ending at position end - 1 interpreted as a String in the specified encoding |
int |
length()
Returns the length of the buffer, measured in bytes.
|
Buffer |
setBuffer(int pos,
Buffer b)
Sets the bytes at position
pos in the Buffer to the bytes represented by the Buffer b .<p>
The buffer will expand as necessary to accommodate any value written. |
Buffer |
setBuffer(int pos,
Buffer b,
int offset,
int len)
Sets the bytes at position
pos in the Buffer to the bytes represented by the Buffer b on the given offset and len .<p>
The buffer will expand as necessary to accommodate any value written. |
Buffer |
setByte(int pos,
byte b)
Sets the
byte at position pos in the Buffer to the value b .<p>
The buffer will expand as necessary to accommodate any value written. |
Buffer |
setBytes(int pos,
byte[] b)
Sets the bytes at position
pos in the Buffer to the bytes represented by the byte[] b .<p>
The buffer will expand as necessary to accommodate any value written. |
Buffer |
setBytes(int pos,
byte[] b,
int offset,
int len)
Sets the given number of bytes at position
pos in the Buffer to the bytes represented by the byte[] b .<p></p>
The buffer will expand as necessary to accommodate any value written. |
Buffer |
setBytes(int pos,
ByteBuffer b)
Sets the bytes at position
pos in the Buffer to the bytes represented by the ByteBuffer b .<p>
The buffer will expand as necessary to accommodate any value written. |
Buffer |
setDouble(int pos,
double d)
Sets the
double at position pos in the Buffer to the value d .<p>
The buffer will expand as necessary to accommodate any value written. |
Buffer |
setFloat(int pos,
float f)
Sets the
float at position pos in the Buffer to the value f .<p>
The buffer will expand as necessary to accommodate any value written. |
Buffer |
setInt(int pos,
int i)
Sets the
int at position pos in the Buffer to the value i .<p>
The buffer will expand as necessary to accommodate any value written. |
Buffer |
setLong(int pos,
long l)
Sets the
long at position pos in the Buffer to the value l .<p>
The buffer will expand as necessary to accommodate any value written. |
Buffer |
setShort(int pos,
short s)
Sets the
short at position pos in the Buffer to the value s .<p>
The buffer will expand as necessary to accommodate any value written. |
Buffer |
setString(int pos,
String str)
Sets the bytes at position
pos in the Buffer to the value of str encoded in UTF-8.<p>
The buffer will expand as necessary to accommodate any value written. |
Buffer |
setString(int pos,
String str,
String enc)
Sets the bytes at position
pos in the Buffer to the value of str encoded in encoding enc .<p>
The buffer will expand as necessary to accommodate any value written. |
Buffer |
slice()
Returns a slice of this buffer.
|
Buffer |
slice(int start,
int end)
Returns a slice of this buffer.
|
String |
toString(String enc)
Returns a
String representation of the Buffer with the encoding specified by enc |
readFromBuffer, writeToBuffer
static final BufferFactory factory
static Buffer buffer()
static Buffer buffer(int initialSizeHint)
static Buffer buffer(byte[] bytes)
static Buffer buffer(io.netty.buffer.ByteBuf byteBuf)
String toString(String enc)
Returns a String
representation of the Buffer with the encoding specified by enc
byte getByte(int pos)
Returns the byte
at position pos
in the Buffer.
IndexOutOfBoundsException
- if the specified pos
is less than 0
or pos + 1
is greater than the length of the Buffer.int getInt(int pos)
Returns the int
at position pos
in the Buffer.
IndexOutOfBoundsException
- if the specified pos
is less than 0
or pos + 4
is greater than the length of the Buffer.long getLong(int pos)
Returns the long
at position pos
in the Buffer.
IndexOutOfBoundsException
- if the specified pos
is less than 0
or pos + 8
is greater than the length of the Buffer.double getDouble(int pos)
Returns the double
at position pos
in the Buffer.
IndexOutOfBoundsException
- if the specified pos
is less than 0
or pos + 8
is greater than the length of the Buffer.float getFloat(int pos)
Returns the float
at position pos
in the Buffer.
IndexOutOfBoundsException
- if the specified pos
is less than 0
or pos + 4
is greater than the length of the Buffer.short getShort(int pos)
Returns the short
at position pos
in the Buffer.
IndexOutOfBoundsException
- if the specified pos
is less than 0
or pos + 2
is greater than the length of the Buffer.byte[] getBytes()
Returns a copy of the entire Buffer as a byte[]
byte[] getBytes(int start, int end)
Returns a copy of a sub-sequence the Buffer as a byte[]
starting at position start
and ending at position end - 1
Buffer getBuffer(int start, int end)
Returns a copy of a sub-sequence the Buffer as a Buffer
starting at position start
and ending at position end - 1
String getString(int start, int end, String enc)
Returns a copy of a sub-sequence the Buffer as a String
starting at position start
and ending at position end - 1
interpreted as a String in the specified encoding
String getString(int start, int end)
Returns a copy of a sub-sequence the Buffer as a String
starting at position start
and ending at position end - 1
interpreted as a String in UTF-8 encoding
Buffer appendBuffer(Buffer buff)
Appends the specified Buffer
to the end of this Buffer. The buffer will expand as necessary to accommodate
any bytes written.<p>
Returns a reference to this
so multiple operations can be appended together.
Buffer appendBuffer(Buffer buff, int offset, int len)
Appends the specified Buffer
starting at the offset
using len
to the end of this Buffer. The buffer will expand as necessary to accommodate
any bytes written.<p>
Returns a reference to this
so multiple operations can be appended together.
Buffer appendBytes(byte[] bytes)
Appends the specified byte[]
to the end of the Buffer. The buffer will expand as necessary to accommodate any bytes written.<p>
Returns a reference to this
so multiple operations can be appended together.
Buffer appendBytes(byte[] bytes, int offset, int len)
Appends the specified number of bytes from byte[]
to the end of the Buffer, starting at the given offset.
The buffer will expand as necessary to accommodate any bytes written.<p>
Returns a reference to this
so multiple operations can be appended together.
Buffer appendByte(byte b)
Appends the specified byte
to the end of the Buffer. The buffer will expand as necessary to accommodate any bytes written.<p>
Returns a reference to this
so multiple operations can be appended together.
Buffer appendInt(int i)
Appends the specified int
to the end of the Buffer. The buffer will expand as necessary to accommodate any bytes written.<p>
Returns a reference to this
so multiple operations can be appended together.
Buffer appendLong(long l)
Appends the specified long
to the end of the Buffer. The buffer will expand as necessary to accommodate any bytes written.<p>
Returns a reference to this
so multiple operations can be appended together.
Buffer appendShort(short s)
Appends the specified short
to the end of the Buffer.The buffer will expand as necessary to accommodate any bytes written.<p>
Returns a reference to this
so multiple operations can be appended together.
Buffer appendFloat(float f)
Appends the specified float
to the end of the Buffer. The buffer will expand as necessary to accommodate any bytes written.<p>
Returns a reference to this
so multiple operations can be appended together.
Buffer appendDouble(double d)
Appends the specified double
to the end of the Buffer. The buffer will expand as necessary to accommodate any bytes written.<p>
Returns a reference to this
so multiple operations can be appended together.
Buffer appendString(String str, String enc)
Appends the specified String
to the end of the Buffer with the encoding as specified by enc
.<p>
The buffer will expand as necessary to accommodate any bytes written.<p>
Returns a reference to this
so multiple operations can be appended together.<p>
Buffer appendString(String str)
Appends the specified String str
to the end of the Buffer with UTF-8 encoding.<p>
The buffer will expand as necessary to accommodate any bytes written.<p>
Returns a reference to this
so multiple operations can be appended together<p>
Buffer setByte(int pos, byte b)
Sets the byte
at position pos
in the Buffer to the value b
.<p>
The buffer will expand as necessary to accommodate any value written.
Buffer setInt(int pos, int i)
Sets the int
at position pos
in the Buffer to the value i
.<p>
The buffer will expand as necessary to accommodate any value written.
Buffer setLong(int pos, long l)
Sets the long
at position pos
in the Buffer to the value l
.<p>
The buffer will expand as necessary to accommodate any value written.
Buffer setDouble(int pos, double d)
Sets the double
at position pos
in the Buffer to the value d
.<p>
The buffer will expand as necessary to accommodate any value written.
Buffer setFloat(int pos, float f)
Sets the float
at position pos
in the Buffer to the value f
.<p>
The buffer will expand as necessary to accommodate any value written.
Buffer setShort(int pos, short s)
Sets the short
at position pos
in the Buffer to the value s
.<p>
The buffer will expand as necessary to accommodate any value written.
Buffer setBuffer(int pos, Buffer b)
Sets the bytes at position pos
in the Buffer to the bytes represented by the Buffer b
.<p>
The buffer will expand as necessary to accommodate any value written.
Buffer setBuffer(int pos, Buffer b, int offset, int len)
Sets the bytes at position pos
in the Buffer to the bytes represented by the Buffer b
on the given offset
and len
.<p>
The buffer will expand as necessary to accommodate any value written.
Buffer setBytes(int pos, ByteBuffer b)
Sets the bytes at position pos
in the Buffer to the bytes represented by the ByteBuffer b
.<p>
The buffer will expand as necessary to accommodate any value written.
Buffer setBytes(int pos, byte[] b)
Sets the bytes at position pos
in the Buffer to the bytes represented by the byte[] b
.<p>
The buffer will expand as necessary to accommodate any value written.
Buffer setBytes(int pos, byte[] b, int offset, int len)
Sets the given number of bytes at position pos
in the Buffer to the bytes represented by the byte[] b
.<p></p>
The buffer will expand as necessary to accommodate any value written.
Buffer setString(int pos, String str)
Sets the bytes at position pos
in the Buffer to the value of str
encoded in UTF-8.<p>
The buffer will expand as necessary to accommodate any value written.
Buffer setString(int pos, String str, String enc)
Sets the bytes at position pos
in the Buffer to the value of str
encoded in encoding enc
.<p>
The buffer will expand as necessary to accommodate any value written.
int length()
Returns the length of the buffer, measured in bytes. All positions are indexed from zero.
Buffer copy()
Returns a copy of the entire Buffer.
Buffer slice()
Returns a slice of this buffer. Modifying the content of the returned buffer or this buffer affects each other’s content while they maintain separate indexes and marks.
Buffer slice(int start, int end)
Returns a slice of this buffer. Modifying the content of the returned buffer or this buffer affects each other’s content while they maintain separate indexes and marks.
io.netty.buffer.ByteBuf getByteBuf()
Returns the Buffer as a Netty ByteBuf
.<p>
This method is meant for internal use only.
Copyright © 2014. All Rights Reserved.