@NotThreadSafe public class Buffer extends java.lang.Object implements Recyclable, java.lang.Comparable<Buffer>, java.lang.Iterable<java.lang.Byte>, java.nio.channels.ReadableByteChannel, java.nio.channels.WritableByteChannel
ByteBuffer
. It provides optional
dynamic expansion of the buffer to accommodate additional content. It also provides convenience methods for
operating
on buffers.Modifier and Type | Class and Description |
---|---|
class |
Buffer.View
A View represents a segment of a buffer.
|
Modifier and Type | Field and Description |
---|---|
static Buffer |
DELIMITING_BUFFER
A delimiting buffer is sent to
BufferCodec and other components to signal the end of a sequence of Buffer. |
static int |
MAX_BUFFER_SIZE
The maximum allowed buffer size in bytes.
|
static int |
SMALL_BUFFER_SIZE
The size, in bytes, of a small buffer.
|
Constructor and Description |
---|
Buffer()
Create an empty Buffer that is dynamic.
|
Buffer(Buffer bufferToCopy)
Copy constructor that creates a shallow copy of the given Buffer by calling
ByteBuffer.duplicate() on the underlying ByteBuffer . |
Buffer(java.nio.ByteBuffer bufferToStartWith)
Create a Buffer using the given
ByteBuffer as the inital source. |
Buffer(int atLeast,
boolean fixed)
Create an Buffer that has an internal
ByteBuffer allocated to the given size and optional make
this buffer fixed-length. |
Modifier and Type | Method and Description |
---|---|
Buffer |
append(Buffer... buffers)
Append the given
Buffer to this Buffer. |
Buffer |
append(byte b)
Append the given
byte to this Buffer. |
Buffer |
append(byte[] b)
Append the given
byte[] to this Buffer. |
Buffer |
append(byte[] b,
int start,
int len)
Append the given
byte[] to this Buffer, starting at the given index and continuing for the
given
length. |
Buffer |
append(java.nio.ByteBuffer... buffers)
Append the given
ByteBuffer to this Buffer. |
Buffer |
append(char c)
Append the given
char to this Buffer. |
Buffer |
append(int i)
Append the given
int to this Buffer. |
Buffer |
append(long l)
Append the given
long to this Buffer. |
Buffer |
append(short s)
Append the given
short to this Buffer. |
Buffer |
append(java.lang.String s)
Append the given String to this Buffer.
|
byte[] |
asBytes()
Return the contents of this buffer copied into a
byte[] . |
java.lang.String |
asString()
Convert the contents of this buffer into a String using a UTF-8
CharsetDecoder . |
java.nio.ByteBuffer |
byteBuffer()
Return the underlying
ByteBuffer . |
int |
capacity()
Provides the current capacity of the internal
ByteBuffer . |
Buffer |
clear()
Clear the internal
ByteBuffer by setting the Buffer.position(int) to 0 and the limit to the
current capacity. |
void |
close() |
Buffer |
compact()
Compact the underlying
ByteBuffer . |
int |
compareTo(Buffer buffer) |
Buffer |
copy()
Create a new
Buffer by copying the underlying ByteBuffer into a newly-allocated Buffer . |
Buffer.View |
createView()
Create a
Buffer.View of the current range of this Buffer . |
Buffer.View |
createView(int start,
int end)
Create a
Buffer.View of the given range of this Buffer. |
Buffer |
duplicate()
Create a new
Buffer by calling ByteBuffer.duplicate() on the underlying ByteBuffer . |
byte |
first()
Get the first
byte from this Buffer. |
Buffer |
flip()
Flip this Buffer.
|
int |
indexOf(byte b)
Search the buffer and find the position of the first occurrence of the given
byte . |
int |
indexOf(byte b,
int start,
int end)
Search the buffer and find the position of the first occurrence of the given
byte staring at the start
position and searching until (and including) the end position. |
java.io.InputStream |
inputStream()
Create an
InputStream capable of reading the bytes from the internal ByteBuffer . |
boolean |
isDelimitingBuffer()
Is this instance a delimiting Buffer
|
boolean |
isDynamic()
Whether this Buffer is fixed-length or not.
|
boolean |
isOpen() |
java.util.Iterator<java.lang.Byte> |
iterator() |
byte |
last()
Get the last
byte from this Buffer. |
int |
limit()
Provides the current limit of the internal
ByteBuffer . |
Buffer |
limit(int limit)
Sets this buffer's limit.
|
static java.lang.Integer |
parseInt(Buffer b)
Very efficient method for parsing an
Integer from the given Buffer. |
static java.lang.Integer |
parseInt(Buffer b,
int start,
int end)
Very efficient method for parsing an
Integer from the given Buffer range. |
static java.lang.Long |
parseLong(Buffer b)
Very efficient method for parsing a
Long from the given Buffer. |
static java.lang.Long |
parseLong(Buffer b,
int start,
int end)
Very efficient method for parsing a
Long from the given Buffer range. |
int |
position()
Provides the current position in the internal
ByteBuffer . |
Buffer |
position(int pos)
Sets this buffer's position.
|
Buffer |
prepend(Buffer b)
Prepend the given
Buffer to this Buffer. |
Buffer |
prepend(byte b)
Prepend the given
byte to this Buffer. |
Buffer |
prepend(byte[] bytes)
Prepend the given
byte[] array to this Buffer. |
Buffer |
prepend(java.nio.ByteBuffer b)
Prepend the given
ByteBuffer to this Buffer. |
Buffer |
prepend(char c)
Prepend the given
char to this existing Buffer. |
Buffer |
prepend(int i)
Prepend the given
int to this Buffer. |
Buffer |
prepend(long l)
Prepend the given
long to this Buffer. |
Buffer |
prepend(short s)
Prepend the given
short to this Buffer. |
Buffer |
prepend(java.lang.String s)
Prepend the given
String to this Buffer. |
byte |
read()
Read a single
byte from the underlying ByteBuffer . |
Buffer |
read(byte[] b)
Read at least
b.length bytes from the underlying ByteBuffer . |
int |
read(java.nio.ByteBuffer dst) |
char |
readChar()
Read the next
char from the underlying ByteBuffer . |
double |
readDouble()
Read the next
double from the underlying ByteBuffer . |
float |
readFloat()
Read the next
float from the underlying ByteBuffer . |
int |
readInt()
Read the next
int from the underlying ByteBuffer . |
long |
readLong()
Read the next
long from the underlying ByteBuffer . |
short |
readShort()
Read the next
short from the underlying ByteBuffer . |
void |
recycle()
Free any internal resources and reset the state of the object to enable reuse.
|
int |
remaining()
How many bytes available in this Buffer.
|
Buffer |
reset()
Reset the buffer to the previously-saved position and limit.
|
Buffer |
rewind()
Rewind this Buffer to the beginning.
|
Buffer |
rewind(int len)
Rewinds this buffer by
len bytes. |
Buffer |
skip(int len)
Skips
len bytes. |
java.util.List<Buffer.View> |
slice(int... positions)
Slice this buffer at the given positions.
|
Buffer |
slice(int start,
int len)
Create a copy of the given range.
|
void |
snapshot()
Save the current buffer position and limit.
|
java.lang.Iterable<Buffer.View> |
split(Buffer delimiter)
Split this buffer on the given delimiter and leave the delimiter on the end of each segment.
|
java.lang.Iterable<Buffer.View> |
split(Buffer delimiter,
boolean stripDelimiter)
Split this buffer on the given delimiter.
|
java.util.List<Buffer.View> |
split(int delimiter)
Split this buffer on the given delimiter.
|
java.util.List<Buffer.View> |
split(int delimiter,
boolean stripDelimiter)
Split this buffer on the given delimiter and optionally leave the delimiter intact rather than stripping it.
|
java.lang.Iterable<Buffer.View> |
split(java.util.List<Buffer.View> views,
Buffer delimiter,
boolean stripDelimiter)
Split this buffer on the given delimiter.
|
java.util.List<Buffer.View> |
split(java.util.List<Buffer.View> views,
int delimiter)
Split this buffer on the given delimiter but save memory by reusing the given
List . |
java.util.List<Buffer.View> |
split(java.util.List<Buffer.View> views,
int delimiter,
boolean stripDelimiter)
Split this buffer on the given delimiter, save memory by reusing the given
List , and optionally leave the
delimiter intact rather than stripping it. |
java.lang.String |
substring(int start,
int end)
Slice a portion of this buffer and convert it to a String.
|
java.lang.String |
toString() |
static Buffer |
wrap(byte[] bytes)
Convenience method to create a new, fixed-length Buffer and putting the given byte array into the
buffer.
|
static Buffer |
wrap(java.lang.String str)
Convenience method to create a new, fixed-length Buffer from the given String.
|
static Buffer |
wrap(java.lang.String str,
boolean fixed)
Convenience method to create a new Buffer from the given String and optionally specify whether the new
Buffer should be a fixed length or not.
|
int |
write(java.nio.ByteBuffer src) |
public static int SMALL_BUFFER_SIZE
reactor.io.defaultBufferSize
system
property. Default to 16384 bytes.public static int MAX_BUFFER_SIZE
reactor.io.maxBufferSize
system
property. Defaults to 16384000 bytes.public static final Buffer DELIMITING_BUFFER
BufferCodec
and other components to signal the end of a sequence of Buffer.public Buffer()
public Buffer(int atLeast, boolean fixed)
ByteBuffer
allocated to the given size and optional make
this buffer fixed-length.atLeast
- Allocate this many bytes immediately.fixed
- true to make this buffer fixed-length, false otherwise.public Buffer(Buffer bufferToCopy)
ByteBuffer.duplicate()
on the underlying ByteBuffer
.bufferToCopy
- The Buffer to copy.public Buffer(java.nio.ByteBuffer bufferToStartWith)
ByteBuffer
as the inital source.bufferToStartWith
- The ByteBuffer
to start with.public static Buffer wrap(byte[] bytes)
bytes
- The bytes to create a buffer from.public static Buffer wrap(java.lang.String str, boolean fixed)
str
- The String to create a buffer from.fixed
- true to create a fixed-length Buffer, false otherwise.public static Buffer wrap(java.lang.String str)
str
- The String to create a buffer from.public static java.lang.Integer parseInt(Buffer b, int start, int end)
Integer
from the given Buffer range. Much faster than
Integer.parseInt(String)
.b
- The Buffer to slice.start
- start of the range.end
- end of the range.public static java.lang.Integer parseInt(Buffer b)
Integer
from the given Buffer. Much faster than Integer.parseInt(String)
.b
- The Buffer to slice.public static java.lang.Long parseLong(Buffer b, int start, int end)
Long
from the given Buffer range. Much faster than Long.parseLong(String)
.b
- The Buffer to slice.start
- start of the range.end
- end of the range.public static java.lang.Long parseLong(Buffer b)
Long
from the given Buffer. Much faster than Long.parseLong(String)
.b
- The Buffer to slice.public void recycle()
Recyclable
recycle
in interface Recyclable
public final boolean isDynamic()
public final int position()
ByteBuffer
.public Buffer position(int pos)
pos
- the new positionpublic Buffer limit(int limit)
limit
- the new limitpublic Buffer skip(int len)
len
bytes.len
- the number of bytes to skipjava.nio.BufferUnderflowException
- if the skip exceeds the available bytesjava.lang.IllegalArgumentException
- if len is negativepublic int limit()
ByteBuffer
.public int capacity()
ByteBuffer
.public int remaining()
public Buffer clear()
ByteBuffer
by setting the Buffer.position(int)
to 0 and the limit to the
current capacity.public Buffer compact()
ByteBuffer
.public Buffer flip()
public Buffer rewind()
public Buffer rewind(int len)
len
bytes.len
- The number of bytes the rewind byjava.nio.BufferUnderflowException
- if the rewind would move past the start of the bufferjava.lang.IllegalArgumentException
- if len is negativepublic Buffer duplicate()
Buffer
by calling ByteBuffer.duplicate()
on the underlying ByteBuffer
.Buffer
public Buffer copy()
Buffer
by copying the underlying ByteBuffer
into a newly-allocated Buffer
.Buffer
public Buffer prepend(Buffer b)
Buffer
to this Buffer.b
- The Buffer
to prepend.public Buffer prepend(java.lang.String s)
String
to this Buffer.s
- The String
to prepend.public Buffer prepend(byte[] bytes)
byte[]
array to this Buffer.bytes
- The byte[]
to prepend.public Buffer prepend(java.nio.ByteBuffer b)
ByteBuffer
to this Buffer.b
- The ByteBuffer
to prepend.public Buffer prepend(byte b)
byte
to this Buffer.b
- The byte
to prepend.public Buffer prepend(char c)
char
to this existing Buffer.c
- The char
to prepend.public Buffer prepend(short s)
short
to this Buffer.s
- The short
to prepend.public Buffer prepend(int i)
int
to this Buffer.i
- The int
to prepend.public Buffer prepend(long l)
long
to this Buffer.l
- The long
to prepend.public Buffer append(java.lang.String s)
s
- The String to append.public Buffer append(short s)
short
to this Buffer.s
- The short
to append.public Buffer append(int i)
int
to this Buffer.i
- The int
to append.public Buffer append(long l)
long
to this Buffer.l
- The long
to append.public Buffer append(char c)
char
to this Buffer.c
- The char
to append.public Buffer append(java.nio.ByteBuffer... buffers)
ByteBuffer
to this Buffer.buffers
- The ByteBuffers
to append.public Buffer append(Buffer... buffers)
Buffer
to this Buffer.buffers
- The Buffers
to append.public Buffer append(byte b)
byte
to this Buffer.b
- The byte
to append.public Buffer append(byte[] b)
byte[]
to this Buffer.b
- The byte[]
to append.public Buffer append(byte[] b, int start, int len)
byte[]
to this Buffer, starting at the given index and continuing for the
given
length.b
- the bytes to appendstart
- the index of where to start copying byteslen
- the len of the bytes to copypublic byte first()
byte
from this Buffer.byte
.public byte last()
byte
from this Buffer.byte
.public byte read()
byte
from the underlying ByteBuffer
.byte
.public Buffer read(byte[] b)
b.length
bytes from the underlying ByteBuffer
.b
- The buffer to fill.public short readShort()
short
from the underlying ByteBuffer
.short
.public int readInt()
int
from the underlying ByteBuffer
.int
.public float readFloat()
float
from the underlying ByteBuffer
.float
.public double readDouble()
double
from the underlying ByteBuffer
.double
.public long readLong()
long
from the underlying ByteBuffer
.long
.public char readChar()
char
from the underlying ByteBuffer
.char
.public void snapshot()
public Buffer reset()
public java.util.Iterator<java.lang.Byte> iterator()
iterator
in interface java.lang.Iterable<java.lang.Byte>
public int read(java.nio.ByteBuffer dst) throws java.io.IOException
read
in interface java.nio.channels.ReadableByteChannel
java.io.IOException
public int write(java.nio.ByteBuffer src) throws java.io.IOException
write
in interface java.nio.channels.WritableByteChannel
java.io.IOException
public boolean isOpen()
isOpen
in interface java.nio.channels.Channel
public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in interface java.nio.channels.Channel
java.io.IOException
public java.lang.String asString()
CharsetDecoder
.public java.lang.String substring(int start, int end)
start
- start of the range.end
- end of the range.public byte[] asBytes()
byte[]
.byte[]
.public java.io.InputStream inputStream()
InputStream
capable of reading the bytes from the internal ByteBuffer
.InputStream
.public Buffer slice(int start, int len)
start
- start of the range.len
- end of the range.Buffer
, constructed from the contents of the given range.public java.util.List<Buffer.View> split(int delimiter)
delimiter
- The delimiter on which to split this buffer.List
of Views
that point to the segments of this buffer.public java.util.List<Buffer.View> split(java.util.List<Buffer.View> views, int delimiter)
List
.public java.util.List<Buffer.View> split(int delimiter, boolean stripDelimiter)
delimiter
- The delimiter on which to split this buffer.stripDelimiter
- true to ignore the delimiter, false to leave it in the returned data.List
of Views
that point to the segments of this buffer.public java.util.List<Buffer.View> split(java.util.List<Buffer.View> views, int delimiter, boolean stripDelimiter)
List
, and optionally leave the
delimiter intact rather than stripping it.public java.lang.Iterable<Buffer.View> split(Buffer delimiter)
delimiter
- the multi-byte delimiterIterable
of Views
that point to the segments of this buffer.public java.lang.Iterable<Buffer.View> split(Buffer delimiter, boolean stripDelimiter)
stripDelimiter
is true
.delimiter
- The multi-byte delimiter.stripDelimiter
- true to ignore the delimiter, false to leave it in the returned data.Iterable
of Views
that point to the segments of this buffer.public java.lang.Iterable<Buffer.View> split(java.util.List<Buffer.View> views, Buffer delimiter, boolean stripDelimiter)
List
. The delimiter is
stripped from the end of the segment if stripDelimiter
is true
.views
- The already-allocated List to reuse.delimiter
- The multi-byte delimiter.stripDelimiter
- true to ignore the delimiter, false to leave it in the returned data.Iterable
of Views
that point to the segments of this buffer.public int indexOf(byte b)
byte
.b
- the byte
to search for-1
if not foundpublic int indexOf(byte b, int start, int end)
byte
staring at the start
position and searching until (and including) the end position.b
- the byte
to search forstart
- the position to start searchingend
- the position at which to stop searching-1
if not foundpublic Buffer.View createView()
Buffer.View
of the current range of this Buffer
.position()
,
limit()
public Buffer.View createView(int start, int end)
Buffer.View
of the given range of this Buffer.start
- start of the range.end
- end of the range.Buffer.View
object that represents the given range.public java.util.List<Buffer.View> slice(int... positions)
positions
- The start and end positions of the slices.Views
pointing to the slices.public java.nio.ByteBuffer byteBuffer()
ByteBuffer
.ByteBuffer
in use.public boolean isDelimitingBuffer()
public java.lang.String toString()
toString
in class java.lang.Object