public class Buffer
extends java.lang.Object
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.
The second method involves methods that take the form appendXXX
; these methods append data
at the end of the buffer.
Methods exist to both set
and append
all primitive types, String
, ByteBuffer
and
other instances of Buffer.
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.
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.
Instances of this class are not thread-safe.
Constructor and Description |
---|
Buffer()
Create an empty buffer
|
Buffer(byte[] bytes)
Create a new Buffer that contains the contents of a
byte[] |
Buffer(io.netty.buffer.ByteBuf buffer)
Create a new Buffer from a Netty
ByteBuf instance. |
Buffer(int initialSizeHint)
Creates a new empty Buffer that is expected to have a size of
initialSizeHint after data has been
written to it. |
Buffer(java.lang.String str)
Create a new Buffer that contains the contents of
String str encoded with UTF-8 encoding |
Buffer(java.lang.String str,
java.lang.String enc)
Create a new Buffer that contains the contents of a
String str encoded according to the encoding enc |
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. |
Buffer |
appendString(java.lang.String str)
Appends the specified
String str to the end of the Buffer with UTF-8 encoding. |
Buffer |
appendString(java.lang.String str,
java.lang.String enc)
Appends the specified
String to the end of the Buffer with the encoding as specified by enc . |
Buffer |
copy()
Returns a copy of the entire Buffer.
|
boolean |
equals(java.lang.Object o) |
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 . |
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. |
java.lang.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 |
java.lang.String |
getString(int start,
int end,
java.lang.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 . |
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 . |
Buffer |
setByte(int pos,
byte b)
Sets the
byte at position pos in the Buffer to the value b . |
Buffer |
setBytes(int pos,
byte[] b)
Sets the bytes at position
pos in the Buffer to the bytes represented by the byte[] b . |
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 . |
Buffer |
setBytes(int pos,
java.nio.ByteBuffer b)
Sets the bytes at position
pos in the Buffer to the bytes represented by the ByteBuffer b . |
Buffer |
setDouble(int pos,
double d)
Sets the
double at position pos in the Buffer to the value d . |
Buffer |
setFloat(int pos,
float f)
Sets the
float at position pos in the Buffer to the value f . |
Buffer |
setInt(int pos,
int i)
Sets the
int at position pos in the Buffer to the value i . |
Buffer |
setLong(int pos,
long l)
Sets the
long at position pos in the Buffer to the value l . |
Buffer |
setShort(int pos,
short s)
Sets the
short at position pos in the Buffer to the value s . |
Buffer |
setString(int pos,
java.lang.String str)
Sets the bytes at position
pos in the Buffer to the value of str encoded in UTF-8. |
Buffer |
setString(int pos,
java.lang.String str,
java.lang.String enc)
Sets the bytes at position
pos in the Buffer to the value of str encoded in encoding enc . |
java.lang.String |
toString()
Returns a
String representation of the Buffer assuming it contains a String encoding in UTF-8 |
java.lang.String |
toString(java.lang.String enc)
Returns a
String representation of the Buffer with the encoding specified by enc |
public Buffer()
public Buffer(int initialSizeHint)
initialSizeHint
after data has been
written to it.
Please note that length
of the Buffer immediately after creation will be zero.
The initialSizeHint
is merely a hint to the system for how much memory to initially allocate to the buffer to prevent excessive
automatic re-allocations as data is written to it.
public Buffer(byte[] bytes)
byte[]
public Buffer(java.lang.String str, java.lang.String enc)
String str
encoded according to the encoding enc
public Buffer(java.lang.String str)
String str
encoded with UTF-8 encodingpublic Buffer(io.netty.buffer.ByteBuf buffer)
ByteBuf
instance.
This method is meant for internal use only.public java.lang.String toString()
String
representation of the Buffer assuming it contains a String
encoding in UTF-8toString
in class java.lang.Object
public java.lang.String toString(java.lang.String enc)
String
representation of the Buffer with the encoding specified by enc
public byte getByte(int pos)
byte
at position pos
in the Buffer.java.lang.IndexOutOfBoundsException
- if the specified pos
is less than 0
or pos + 1
is greater than the length of the Buffer.public int getInt(int pos)
int
at position pos
in the Buffer.java.lang.IndexOutOfBoundsException
- if the specified pos
is less than 0
or pos + 4
is greater than the length of the Buffer.public long getLong(int pos)
long
at position pos
in the Buffer.java.lang.IndexOutOfBoundsException
- if the specified pos
is less than 0
or pos + 8
is greater than the length of the Buffer.public double getDouble(int pos)
double
at position pos
in the Buffer.java.lang.IndexOutOfBoundsException
- if the specified pos
is less than 0
or pos + 8
is greater than the length of the Buffer.public float getFloat(int pos)
float
at position pos
in the Buffer.java.lang.IndexOutOfBoundsException
- if the specified pos
is less than 0
or pos + 4
is greater than the length of the Buffer.public short getShort(int pos)
short
at position pos
in the Buffer.java.lang.IndexOutOfBoundsException
- if the specified pos
is less than 0
or pos + 2
is greater than the length of the Buffer.public byte[] getBytes()
byte[]
public byte[] getBytes(int start, int end)
byte[]
starting at position start
and ending at position end - 1
public Buffer getBuffer(int start, int end)
Buffer
starting at position start
and ending at position end - 1
public java.lang.String getString(int start, int end, java.lang.String enc)
String
starting at position start
and ending at position end - 1
interpreted as a String in the specified encodingpublic java.lang.String getString(int start, int end)
String
starting at position start
and ending at position end - 1
interpreted as a String in UTF-8 encodingpublic Buffer appendBuffer(Buffer buff)
Buffer
to the end of this Buffer. The buffer will expand as necessary to accommodate
any bytes written.
Returns a reference to this
so multiple operations can be appended together.
public Buffer appendBuffer(Buffer buff, int offset, int len)
Buffer
starting at the offset
using len
to the end of this Buffer. The buffer will expand as necessary to accommodate
any bytes written.
Returns a reference to this
so multiple operations can be appended together.
public Buffer appendBytes(byte[] bytes)
byte[]
to the end of the Buffer. The buffer will expand as necessary to accommodate any bytes written.
Returns a reference to this
so multiple operations can be appended together.
public Buffer appendBytes(byte[] bytes, int offset, int len)
byte[]
to the end of the Buffer, starting at the given offset.
The buffer will expand as necessary to accommodate any bytes written.
Returns a reference to this
so multiple operations can be appended together.
public Buffer appendByte(byte b)
byte
to the end of the Buffer. The buffer will expand as necessary to accommodate any bytes written.
Returns a reference to this
so multiple operations can be appended together.
public Buffer appendInt(int i)
int
to the end of the Buffer. The buffer will expand as necessary to accommodate any bytes written.
Returns a reference to this
so multiple operations can be appended together.
public Buffer appendLong(long l)
long
to the end of the Buffer. The buffer will expand as necessary to accommodate any bytes written.
Returns a reference to this
so multiple operations can be appended together.
public Buffer appendShort(short s)
short
to the end of the Buffer.The buffer will expand as necessary to accommodate any bytes written.
Returns a reference to this
so multiple operations can be appended together.
public Buffer appendFloat(float f)
float
to the end of the Buffer. The buffer will expand as necessary to accommodate any bytes written.
Returns a reference to this
so multiple operations can be appended together.
public Buffer appendDouble(double d)
double
to the end of the Buffer. The buffer will expand as necessary to accommodate any bytes written.
Returns a reference to this
so multiple operations can be appended together.
public Buffer appendString(java.lang.String str, java.lang.String enc)
String
to the end of the Buffer with the encoding as specified by enc
.The buffer will expand as necessary to accommodate any bytes written.
Returns a reference to this
so multiple operations can be appended together.
public Buffer appendString(java.lang.String str)
String str
to the end of the Buffer with UTF-8 encoding.The buffer will expand as necessary to accommodate any bytes written.
Returns a reference to this
so multiple operations can be appended together
public Buffer setByte(int pos, byte b)
byte
at position pos
in the Buffer to the value b
.The buffer will expand as necessary to accommodate any value written.
public Buffer setInt(int pos, int i)
int
at position pos
in the Buffer to the value i
.The buffer will expand as necessary to accommodate any value written.
public Buffer setLong(int pos, long l)
long
at position pos
in the Buffer to the value l
.The buffer will expand as necessary to accommodate any value written.
public Buffer setDouble(int pos, double d)
double
at position pos
in the Buffer to the value d
.The buffer will expand as necessary to accommodate any value written.
public Buffer setFloat(int pos, float f)
float
at position pos
in the Buffer to the value f
.The buffer will expand as necessary to accommodate any value written.
public Buffer setShort(int pos, short s)
short
at position pos
in the Buffer to the value s
.The buffer will expand as necessary to accommodate any value written.
public Buffer setBuffer(int pos, Buffer b)
pos
in the Buffer to the bytes represented by the Buffer b
.The buffer will expand as necessary to accommodate any value written.
public Buffer setBuffer(int pos, Buffer b, int offset, int len)
pos
in the Buffer to the bytes represented by the Buffer b
on the given offset
and len
.The buffer will expand as necessary to accommodate any value written.
public Buffer setBytes(int pos, java.nio.ByteBuffer b)
pos
in the Buffer to the bytes represented by the ByteBuffer b
.The buffer will expand as necessary to accommodate any value written.
public Buffer setBytes(int pos, byte[] b)
pos
in the Buffer to the bytes represented by the byte[] b
.The buffer will expand as necessary to accommodate any value written.
public Buffer setBytes(int pos, byte[] b, int offset, int len)
pos
in the Buffer to the bytes represented by the byte[] b
.
The buffer will expand as necessary to accommodate any value written.public Buffer setString(int pos, java.lang.String str)
pos
in the Buffer to the value of str
encoded in UTF-8.The buffer will expand as necessary to accommodate any value written.
public Buffer setString(int pos, java.lang.String str, java.lang.String enc)
pos
in the Buffer to the value of str
encoded in encoding enc
.The buffer will expand as necessary to accommodate any value written.
public int length()
public Buffer copy()
public io.netty.buffer.ByteBuf getByteBuf()
ByteBuf
.This method is meant for internal use only.
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object