Package org.xnio
Class Buffers
java.lang.Object
org.xnio.Buffers
Buffer utility methods.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ByteBuffer
The empty byte buffer.static final Pooled<ByteBuffer>
The empty pooled byte buffer. -
Method Summary
Modifier and TypeMethodDescriptionstatic void
addRandom
(ByteBuffer target) Add a random amount of random data to the target buffer using the thread-local RNG.static void
addRandom
(ByteBuffer target, int count) Addcount
bytes of random data to the target buffer using the thread-local RNG.static void
addRandom
(ByteBuffer target, Random random) Add a random amount of random data to the target buffer.static void
addRandom
(ByteBuffer target, Random random, int count) Addcount
bytes of random data to the target buffer.allocatedBufferPool
(BufferAllocator<B> allocator, int size) A buffer pool which allocates a new buffer on every allocate request, and discards buffers on free.static void
assertWritable
(Buffer... buffers) Assert the writability of the given buffers.static void
assertWritable
(Buffer[] buffers, int offs, int len) Assert the writability of the given buffers.static <T extends Buffer>
Tclear
(T buffer) Clear a buffer.static int
copy
(int count, ByteBuffer[] destinations, int offset, int length, ByteBuffer source) Copy at mostcount
bytes fromsources
intodestinations
in a "scatter" fashion.static int
copy
(int count, ByteBuffer destination, ByteBuffer source) Copy at mostcount
bytes fromsource
intodestination
.static int
copy
(int count, ByteBuffer destination, ByteBuffer[] sources, int offset, int length) Copy at mostcount
bytes fromsources
intodestination
in a "gather" fashion.static long
copy
(long count, ByteBuffer[] destinations, int destOffset, int destLength, ByteBuffer[] sources, int srcOffset, int srcLength) Copy at mostcount
bytes fromsources
intodestinations
by a combined "scatter"/"gather" operation.static int
copy
(ByteBuffer[] destinations, int offset, int length, ByteBuffer source) Copy as many bytes as possible fromsources
intodestinations
in a "scatter" fashion.static long
copy
(ByteBuffer[] destinations, int destOffset, int destLength, ByteBuffer[] sources, int srcOffset, int srcLength) Copy as many bytes as possible fromsources
intodestinations
by a combined "scatter"/"gather" operation.static ByteBuffer
copy
(ByteBuffer buffer, int count, BufferAllocator<ByteBuffer> allocator) Copy a portion of the buffer into a newly allocated buffer.static int
copy
(ByteBuffer destination, ByteBuffer source) Copy as many bytes as possible fromsource
intodestination
.static int
copy
(ByteBuffer destination, ByteBuffer[] sources, int offset, int length) Copy as many bytes as possible fromsources
intodestination
in a "gather" fashion.static Object
createDumper
(ByteBuffer buffer, int indent, int columns) Create an object that returns the dumped form of the given byte buffer when itstoString()
method is called.static Object
createDumper
(CharBuffer buffer, int indent, int columns) Create an object that returns the dumped form of the given character buffer when itstoString()
method is called.static String
debugString
(ByteBuffer buffer) Get a debug-friendly description of the buffer.static String
debugString
(ByteBuffer[] buffers, int offs, int len) Get a debug-friendly description of the buffer.static void
dump
(ByteBuffer buffer, Appendable dest, int indent, int columns) Dump a byte buffer to the given target.static void
dump
(CharBuffer buffer, Appendable dest, int indent, int columns) Dump a character buffer to the given target.static Pooled<ByteBuffer>
Create a "pooled" empty buffer.static void
emptyToStream
(OutputStream target, ByteBuffer source) Empty a buffer to an output stream.static ByteBuffer
fill
(ByteBuffer buffer, int value, int count) Fill a buffer with a repeated value.static CharBuffer
fill
(CharBuffer buffer, int value, int count) Fill a buffer with a repeated value.static IntBuffer
Fill a buffer with a repeated value.static LongBuffer
fill
(LongBuffer buffer, long value, int count) Fill a buffer with a repeated value.static ShortBuffer
fill
(ShortBuffer buffer, int value, int count) Fill a buffer with a repeated value.static int
fillFromStream
(ByteBuffer target, InputStream source) Fill a buffer from an input stream.static <T extends Buffer>
Tflip
(T buffer) Flip a buffer.static String
Get a modified UTF-8 string from the remainder of the buffer.static String
Get a 0-terminated string from the byte buffer, decoding it using "modified UTF-8" encoding.static Pooled<ByteBuffer>
globalPooledWrapper
(ByteBuffer buffer) Create a pooled wrapper around a buffer that was allocated viaByteBufferPool
.static boolean
hasRemaining
(Buffer[] buffers) Determine whether any of the buffers has remaining data.static boolean
hasRemaining
(Buffer[] buffers, int offs, int len) Determine whether any of the buffers has remaining data.static boolean
Determine whether the given buffers list is comprised solely of direct buffers or solely of heap buffers.static boolean
Determine whether the given buffers list is comprised solely of direct buffers or solely of heap buffers.static boolean
isSecureBufferPool
(Pool<?> pool) Determine whether the given pool is a secure pool.static <T extends Buffer>
Tlimit
(T buffer, int limit) Set the buffer limit.static <T extends Buffer>
Tmark
(T buffer) Set the buffer mark.pooledWrapper
(B buffer) Create a pooled wrapper around a buffer.static <T extends Buffer>
Tposition
(T buffer, int position) Set the buffer position.static ByteBuffer
putModifiedUtf8
(ByteBuffer dest, String orig) Put the string into the byte buffer, encoding it using "modified UTF-8" encoding.static void
readAscii
(ByteBuffer src, StringBuilder builder) Read the remainder of a buffer as ASCII text, appending the results to the given string builder.static void
readAscii
(ByteBuffer src, StringBuilder builder, char replacement) Read the remainder of a buffer as ASCII text, appending the results to the given string builder.static void
readAscii
(ByteBuffer src, StringBuilder builder, int limit, char replacement) Read the remainder of a buffer as ASCII text, up to a certain limit, appending the results to the given string builder.static boolean
readAsciiLine
(ByteBuffer src, StringBuilder builder) Read a single line of ASCII text from a byte buffer, appending the results to the given string builder.static boolean
readAsciiLine
(ByteBuffer src, StringBuilder builder, char replacement) Read a single line of ASCII text from a byte buffer, appending the results to the given string builder.static boolean
readAsciiLine
(ByteBuffer src, StringBuilder builder, char replacement, char delimiter) Read a single line of ASCII text from a byte buffer, appending the results to the given string builder, using the given delimiter character instead ofEOL
.static boolean
readAsciiZ
(ByteBuffer src, StringBuilder builder) Read an ASCIIZ (NUL
-terminated) string from a byte buffer, appending the results to the given string builder.static boolean
readAsciiZ
(ByteBuffer src, StringBuilder builder, char replacement) Read an ASCIIZ (NUL
-terminated) string from a byte buffer, appending the results to the given string builder.static void
readLatin1
(ByteBuffer src, StringBuilder builder) Read the remainder of a buffer as Latin-1 text, appending the results to the given string builder.static boolean
readLatin1Line
(ByteBuffer src, StringBuilder builder) Read a single line of Latin-1 text from a byte buffer, appending the results to the given string builder.static boolean
readLatin1Line
(ByteBuffer src, StringBuilder builder, char delimiter) Read a single line of Latin-1 text from a byte buffer, appending the results to the given string builder.static boolean
readLatin1Z
(ByteBuffer src, StringBuilder builder) Read aNUL
-terminated Latin-1 string from a byte buffer, appending the results to the given string builder.static boolean
readLine
(ByteBuffer src, StringBuilder builder, CharsetDecoder decoder) Read a single line of text from a byte buffer, appending the results to the given string builder.static boolean
readLine
(ByteBuffer src, StringBuilder builder, CharsetDecoder decoder, char delimiter) Read a single line of text from a byte buffer, appending the results to the given string builder.static boolean
readModifiedUtf8Line
(ByteBuffer src, StringBuilder builder) Read a single line ofmodified UTF-8
text from a byte buffer, appending the results to the given string builder.static boolean
readModifiedUtf8Line
(ByteBuffer src, StringBuilder builder, char replacement) Read a single line ofmodified UTF-8
text from a byte buffer, appending the results to the given string builder.static boolean
readModifiedUtf8Line
(ByteBuffer src, StringBuilder builder, char replacement, char delimiter) Read a single line ofmodified UTF-8
text from a byte buffer, appending the results to the given string builder.static boolean
readModifiedUtf8Z
(ByteBuffer src, StringBuilder builder) Read aNUL
-terminatedmodified UTF-8
string from a byte buffer, appending the results to the given string builder.static boolean
readModifiedUtf8Z
(ByteBuffer src, StringBuilder builder, char replacement) Read aNUL
-terminatedmodified UTF-8
string from a byte buffer, appending the results to the given string builder.static long
Get the total remaining size of all the given buffers.static long
Get the total remaining size of all the given buffers.static <T extends Buffer>
Treset
(T buffer) Reset the buffer.static <T extends Buffer>
Trewind
(T buffer) Rewind the buffer.static Pool<ByteBuffer>
secureBufferPool
(Pool<ByteBuffer> delegate) A byte buffer pool which zeroes the content of the buffer before re-pooling it.static <T extends Buffer>
Tskip
(T buffer, int cnt) Advance a buffer's position relative to its current position.static ByteBuffer
slice
(ByteBuffer buffer, int sliceSize) Slice the buffer.static CharBuffer
slice
(CharBuffer buffer, int sliceSize) Slice the buffer.static IntBuffer
Slice the buffer.static LongBuffer
slice
(LongBuffer buffer, int sliceSize) Slice the buffer.static ShortBuffer
slice
(ShortBuffer buffer, int sliceSize) Slice the buffer.static BufferAllocator<ByteBuffer>
sliceAllocator
(ByteBuffer buffer) A buffer allocator which allocates slices off of the given buffer.static byte[]
take
(ByteBuffer buffer) Take all of the remaining bytes from the buffer and return them in an array.static byte[]
take
(ByteBuffer[] buffers, int offs, int len) Take all of the remaining bytes from the buffers and return them in an array.static byte[]
take
(ByteBuffer buffer, int cnt) Take a certain number of bytes from the buffer and return them in an array.static char[]
take
(CharBuffer buffer) Take all of the remaining chars from the buffer and return them in an array.static char[]
take
(CharBuffer buffer, int cnt) Take a certain number of chars from the buffer and return them in an array.static int[]
Take all of the remaining ints from the buffer and return them in an array.static int[]
Take a certain number of ints from the buffer and return them in an array.static long[]
take
(LongBuffer buffer) Take all of the remaining longs from the buffer and return them in an array.static long[]
take
(LongBuffer buffer, int cnt) Take a certain number of longs from the buffer and return them in an array.static short[]
take
(ShortBuffer buffer) Take all of the remaining shorts from the buffer and return them in an array.static short[]
take
(ShortBuffer buffer, int cnt) Take a certain number of shorts from the buffer and return them in an array.static long
Attempt to advance a series of buffers' overall position relative to its current position.static int
Attempt to advance a buffer's position relative to its current position.static <T extends Buffer>
Tunget
(T buffer, int cnt) Rewind a buffer's position relative to its current position.static void
zero
(ByteBuffer buffer) Zero a buffer.static void
zero
(CharBuffer buffer) Zero a buffer.
-
Field Details
-
EMPTY_BYTE_BUFFER
The empty byte buffer. -
EMPTY_POOLED_BYTE_BUFFER
The empty pooled byte buffer. Freeing or discarding this buffer has no effect.
-
-
Method Details
-
flip
Flip a buffer.- Type Parameters:
T
- the buffer type- Parameters:
buffer
- the buffer to flip- Returns:
- the buffer instance
- See Also:
-
clear
Clear a buffer.- Type Parameters:
T
- the buffer type- Parameters:
buffer
- the buffer to clear- Returns:
- the buffer instance
- See Also:
-
limit
Set the buffer limit.- Type Parameters:
T
- the buffer type- Parameters:
buffer
- the buffer to setlimit
- the new limit- Returns:
- the buffer instance
- See Also:
-
mark
Set the buffer mark.- Type Parameters:
T
- the buffer type- Parameters:
buffer
- the buffer to mark- Returns:
- the buffer instance
- See Also:
-
position
Set the buffer position.- Type Parameters:
T
- the buffer type- Parameters:
buffer
- the buffer to setposition
- the new position- Returns:
- the buffer instance
- See Also:
-
reset
Reset the buffer.- Type Parameters:
T
- the buffer type- Parameters:
buffer
- the buffer to reset- Returns:
- the buffer instance
- See Also:
-
rewind
Rewind the buffer.- Type Parameters:
T
- the buffer type- Parameters:
buffer
- the buffer to rewind- Returns:
- the buffer instance
- See Also:
-
slice
Slice the buffer. The original buffer's position will be moved up past the slice that was taken.- Parameters:
buffer
- the buffer to slicesliceSize
- the size of the slice- Returns:
- the buffer slice
- See Also:
-
copy
Copy a portion of the buffer into a newly allocated buffer. The original buffer's position will be moved up past the copy that was taken.- Parameters:
buffer
- the buffer to slicecount
- the size of the copyallocator
- the buffer allocator to use- Returns:
- the buffer slice
-
copy
Copy as many bytes as possible fromsource
intodestination
.- Parameters:
destination
- the destination buffersource
- the source buffer- Returns:
- the number of bytes put into the destination buffer
-
copy
Copy as many bytes as possible fromsources
intodestinations
in a "scatter" fashion.- Parameters:
destinations
- the destination buffersoffset
- the offset into the destination buffers arraylength
- the number of buffers to updatesource
- the source buffer- Returns:
- the number of bytes put into the destination buffers
-
copy
Copy as many bytes as possible fromsources
intodestination
in a "gather" fashion.- Parameters:
destination
- the destination buffersources
- the source buffersoffset
- the offset into the source buffers arraylength
- the number of buffers to read from- Returns:
- the number of bytes put into the destination buffers
-
copy
public static long copy(ByteBuffer[] destinations, int destOffset, int destLength, ByteBuffer[] sources, int srcOffset, int srcLength) Copy as many bytes as possible fromsources
intodestinations
by a combined "scatter"/"gather" operation.- Parameters:
destinations
- the destination buffersdestOffset
- the offset into the destination buffers arraydestLength
- the number of buffers to write tosources
- the source bufferssrcOffset
- the offset into the source buffers arraysrcLength
- the number of buffers to read from- Returns:
- the number of bytes put into the destination buffers
-
copy
Copy at mostcount
bytes fromsource
intodestination
.- Parameters:
count
- the maximum number of bytes to copydestination
- the destination buffersource
- the source buffer- Returns:
- the number of bytes put into the destination buffer
-
copy
public static int copy(int count, ByteBuffer[] destinations, int offset, int length, ByteBuffer source) Copy at mostcount
bytes fromsources
intodestinations
in a "scatter" fashion.- Parameters:
count
- the maximum number of bytes to copydestinations
- the destination buffersoffset
- the offset into the destination buffers arraylength
- the number of buffers to updatesource
- the source buffer- Returns:
- the number of bytes put into the destination buffers
-
copy
public static int copy(int count, ByteBuffer destination, ByteBuffer[] sources, int offset, int length) Copy at mostcount
bytes fromsources
intodestination
in a "gather" fashion.- Parameters:
count
- the maximum number of bytes to copydestination
- the destination buffersources
- the source buffersoffset
- the offset into the source buffers arraylength
- the number of buffers to read from- Returns:
- the number of bytes put into the destination buffers
-
copy
public static long copy(long count, ByteBuffer[] destinations, int destOffset, int destLength, ByteBuffer[] sources, int srcOffset, int srcLength) Copy at mostcount
bytes fromsources
intodestinations
by a combined "scatter"/"gather" operation.- Parameters:
count
- the maximum number of bytes to copydestinations
- the destination buffersdestOffset
- the offset into the destination buffers arraydestLength
- the number of buffers to write tosources
- the source bufferssrcOffset
- the offset into the source buffers arraysrcLength
- the number of buffers to read from- Returns:
- the number of bytes put into the destination buffers
-
fill
Fill a buffer with a repeated value.- Parameters:
buffer
- the buffer to fillvalue
- the value to fillcount
- the number of bytes to fill- Returns:
- the buffer instance
-
slice
Slice the buffer. The original buffer's position will be moved up past the slice that was taken.- Parameters:
buffer
- the buffer to slicesliceSize
- the size of the slice- Returns:
- the buffer slice
- See Also:
-
fill
Fill a buffer with a repeated value.- Parameters:
buffer
- the buffer to fillvalue
- the value to fillcount
- the number of chars to fill- Returns:
- the buffer instance
-
slice
Slice the buffer. The original buffer's position will be moved up past the slice that was taken.- Parameters:
buffer
- the buffer to slicesliceSize
- the size of the slice- Returns:
- the buffer slice
- See Also:
-
fill
Fill a buffer with a repeated value.- Parameters:
buffer
- the buffer to fillvalue
- the value to fillcount
- the number of shorts to fill- Returns:
- the buffer instance
-
slice
Slice the buffer. The original buffer's position will be moved up past the slice that was taken.- Parameters:
buffer
- the buffer to slicesliceSize
- the size of the slice- Returns:
- the buffer slice
- See Also:
-
fill
Fill a buffer with a repeated value.- Parameters:
buffer
- the buffer to fillvalue
- the value to fillcount
- the number of ints to fill- Returns:
- the buffer instance
-
slice
Slice the buffer. The original buffer's position will be moved up past the slice that was taken.- Parameters:
buffer
- the buffer to slicesliceSize
- the size of the slice- Returns:
- the buffer slice
- See Also:
-
fill
Fill a buffer with a repeated value.- Parameters:
buffer
- the buffer to fillvalue
- the value to fillcount
- the number of longs to fill- Returns:
- the buffer instance
-
skip
Advance a buffer's position relative to its current position.- Type Parameters:
T
- the buffer type- Parameters:
buffer
- the buffer to setcnt
- the distance to skip- Returns:
- the buffer instance
- Throws:
BufferUnderflowException
- if there are fewer thancnt
bytes remaining- See Also:
-
trySkip
Attempt to advance a buffer's position relative to its current position.- Parameters:
buffer
- the buffer to setcnt
- the distance to skip- Returns:
- the actual number of bytes skipped
- See Also:
-
trySkip
Attempt to advance a series of buffers' overall position relative to its current position.- Parameters:
buffers
- the buffers to setoffs
- the offset into the buffers arraylen
- the number of buffers to considercnt
- the distance to skip- Returns:
- the actual number of bytes skipped
- See Also:
-
unget
Rewind a buffer's position relative to its current position.- Type Parameters:
T
- the buffer type- Parameters:
buffer
- the buffer to setcnt
- the distance to skip backwards- Returns:
- the buffer instance
- See Also:
-
take
Take a certain number of bytes from the buffer and return them in an array.- Parameters:
buffer
- the buffer to readcnt
- the number of bytes to take- Returns:
- the bytes
-
take
Take a certain number of chars from the buffer and return them in an array.- Parameters:
buffer
- the buffer to readcnt
- the number of chars to take- Returns:
- the chars
-
take
Take a certain number of shorts from the buffer and return them in an array.- Parameters:
buffer
- the buffer to readcnt
- the number of shorts to take- Returns:
- the shorts
-
take
Take a certain number of ints from the buffer and return them in an array.- Parameters:
buffer
- the buffer to readcnt
- the number of ints to take- Returns:
- the ints
-
take
Take a certain number of longs from the buffer and return them in an array.- Parameters:
buffer
- the buffer to readcnt
- the number of longs to take- Returns:
- the longs
-
take
Take all of the remaining bytes from the buffer and return them in an array.- Parameters:
buffer
- the buffer to read- Returns:
- the bytes
-
take
Take all of the remaining bytes from the buffers and return them in an array.- Parameters:
buffers
- the buffer to readoffs
- the offset into the arraylen
- the number of buffers- Returns:
- the bytes
-
take
Take all of the remaining chars from the buffer and return them in an array.- Parameters:
buffer
- the buffer to read- Returns:
- the chars
-
take
Take all of the remaining shorts from the buffer and return them in an array.- Parameters:
buffer
- the buffer to read- Returns:
- the shorts
-
take
Take all of the remaining ints from the buffer and return them in an array.- Parameters:
buffer
- the buffer to read- Returns:
- the ints
-
take
Take all of the remaining longs from the buffer and return them in an array.- Parameters:
buffer
- the buffer to read- Returns:
- the longs
-
createDumper
Create an object that returns the dumped form of the given byte buffer when itstoString()
method is called. Useful for logging byte buffers; if thetoString()
method is never called, the process of dumping the buffer is never performed.- Parameters:
buffer
- the bufferindent
- the indentation to usecolumns
- the number of 8-byte columns- Returns:
- a stringable object
-
dump
public static void dump(ByteBuffer buffer, Appendable dest, int indent, int columns) throws IOException Dump a byte buffer to the given target.- Parameters:
buffer
- the bufferdest
- the targetindent
- the indentation to usecolumns
- the number of 8-byte columns- Throws:
IOException
- if an error occurs during append
-
createDumper
Create an object that returns the dumped form of the given character buffer when itstoString()
method is called. Useful for logging character buffers; if thetoString()
method is never called, the process of dumping the buffer is never performed.- Parameters:
buffer
- the bufferindent
- the indentation to usecolumns
- the number of 8-byte columns- Returns:
- a stringable object
-
dump
public static void dump(CharBuffer buffer, Appendable dest, int indent, int columns) throws IOException Dump a character buffer to the given target.- Parameters:
buffer
- the bufferdest
- the targetindent
- the indentation to usecolumns
- the number of 8-byte columns- Throws:
IOException
- if an error occurs during append
-
hasRemaining
Determine whether any of the buffers has remaining data.- Parameters:
buffers
- the buffersoffs
- the offset into the buffers arraylen
- the number of buffers to check- Returns:
true
if any of the selected buffers has remaining data
-
hasRemaining
Determine whether any of the buffers has remaining data.- Parameters:
buffers
- the buffers- Returns:
true
if any of the selected buffers has remaining data
-
remaining
Get the total remaining size of all the given buffers.- Parameters:
buffers
- the buffersoffs
- the offset into the buffers arraylen
- the number of buffers to check- Returns:
- the number of remaining elements
-
remaining
Get the total remaining size of all the given buffers.- Parameters:
buffers
- the buffers- Returns:
- the number of remaining elements
-
putModifiedUtf8
public static ByteBuffer putModifiedUtf8(ByteBuffer dest, String orig) throws BufferOverflowException Put the string into the byte buffer, encoding it using "modified UTF-8" encoding.- Parameters:
dest
- the byte bufferorig
- the source bytes- Returns:
- the byte buffer
- Throws:
BufferOverflowException
- if there is not enough space in the buffer for the complete string- See Also:
-
getModifiedUtf8Z
Get a 0-terminated string from the byte buffer, decoding it using "modified UTF-8" encoding.- Parameters:
src
- the source buffer- Returns:
- the string
- Throws:
BufferUnderflowException
- if the end of the buffer was reached before encountering a0
-
getModifiedUtf8
Get a modified UTF-8 string from the remainder of the buffer.- Parameters:
src
- the buffer- Returns:
- the modified UTF-8 string
- Throws:
BufferUnderflowException
- if the buffer ends abruptly in the midst of a single character
-
readAsciiZ
Read an ASCIIZ (NUL
-terminated) string from a byte buffer, appending the results to the given string builder. If noNUL
character is encountered,false
is returned, indicating that more data needs to be acquired before the operation can be complete. On return, there may be data remaining in the source buffer. If an invalid byte is read, the character'?'
is written to the string builder in its place.- Parameters:
src
- the source bufferbuilder
- the destination builder- Returns:
true
if the entire string was read,false
if more data is needed
-
readAsciiZ
Read an ASCIIZ (NUL
-terminated) string from a byte buffer, appending the results to the given string builder. If noNUL
character is encountered,false
is returned, indicating that more data needs to be acquired before the operation can be complete. On return, there may be data remaining in the source buffer. If an invalid byte is read, the character designated byreplacement
is written to the string builder in its place.- Parameters:
src
- the source bufferbuilder
- the destination builderreplacement
- the replacement character for invalid bytes- Returns:
true
if the entire string was read,false
if more data is needed
-
readAsciiLine
Read a single line of ASCII text from a byte buffer, appending the results to the given string builder. If noEOL
character is encountered,false
is returned, indicating that more data needs to be acquired before the operation can be complete. On return, there may be data remaining in the source buffer. If an invalid byte is read, the character'?'
is written to the string builder in its place. TheEOL
character will be included in the resultant string.- Parameters:
src
- the source bufferbuilder
- the destination builder- Returns:
true
if the entire string was read,false
if more data is needed
-
readAsciiLine
Read a single line of ASCII text from a byte buffer, appending the results to the given string builder. If noEOL
character is encountered,false
is returned, indicating that more data needs to be acquired before the operation can be complete. On return, there may be data remaining in the source buffer. If an invalid byte is read, the character designated byreplacement
is written to the string builder in its place. TheEOL
character will be included in the resultant string.- Parameters:
src
- the source bufferbuilder
- the destination builderreplacement
- the replacement character for invalid bytes- Returns:
true
if the entire string was read,false
if more data is needed
-
readAsciiLine
public static boolean readAsciiLine(ByteBuffer src, StringBuilder builder, char replacement, char delimiter) Read a single line of ASCII text from a byte buffer, appending the results to the given string builder, using the given delimiter character instead ofEOL
. If no delimiter character is encountered,false
is returned, indicating that more data needs to be acquired before the operation can be complete. On return, there may be data remaining in the source buffer. If an invalid byte is read, the character designated byreplacement
is written to the string builder in its place. The delimiter character will be included in the resultant string.- Parameters:
src
- the source bufferbuilder
- the destination builderreplacement
- the replacement character for invalid bytesdelimiter
- the character which marks the end of the line- Returns:
true
if the entire string was read,false
if more data is needed
-
readAscii
Read the remainder of a buffer as ASCII text, appending the results to the given string builder. If an invalid byte is read, the character'?'
is written to the string builder in its place.- Parameters:
src
- the source bufferbuilder
- the destination builder
-
readAscii
Read the remainder of a buffer as ASCII text, appending the results to the given string builder. If an invalid byte is read, the character designated byreplacement
is written to the string builder in its place.- Parameters:
src
- the source bufferbuilder
- the destination builderreplacement
- the replacement character for invalid bytes
-
readAscii
Read the remainder of a buffer as ASCII text, up to a certain limit, appending the results to the given string builder. If an invalid byte is read, the character designated byreplacement
is written to the string builder in its place.- Parameters:
src
- the source bufferbuilder
- the destination builderlimit
- the maximum number of characters to writereplacement
- the replacement character for invalid bytes
-
readLatin1Z
Read aNUL
-terminated Latin-1 string from a byte buffer, appending the results to the given string builder. If noNUL
character is encountered,false
is returned, indicating that more data needs to be acquired before the operation can be complete. On return, there may be data remaining in the source buffer.- Parameters:
src
- the source bufferbuilder
- the destination builder- Returns:
true
if the entire string was read,false
if more data is needed
-
readLatin1Line
Read a single line of Latin-1 text from a byte buffer, appending the results to the given string builder. If noEOL
character is encountered,false
is returned, indicating that more data needs to be acquired before the operation can be complete. On return, there may be data remaining in the source buffer. TheEOL
character will be included in the resultant string.- Parameters:
src
- the source bufferbuilder
- the destination builder- Returns:
true
if the entire string was read,false
if more data is needed
-
readLatin1Line
Read a single line of Latin-1 text from a byte buffer, appending the results to the given string builder. If no delimiter character is encountered,false
is returned, indicating that more data needs to be acquired before the operation can be complete. On return, there may be data remaining in the source buffer. The delimiter character will be included in the resultant string.- Parameters:
src
- the source bufferbuilder
- the destination builderdelimiter
- the character which marks the end of the line- Returns:
true
if the entire string was read,false
if more data is needed
-
readLatin1
Read the remainder of a buffer as Latin-1 text, appending the results to the given string builder.- Parameters:
src
- the source bufferbuilder
- the destination builder
-
readModifiedUtf8Z
Read aNUL
-terminatedmodified UTF-8
string from a byte buffer, appending the results to the given string builder. If noNUL
byte is encountered,false
is returned, indicating that more data needs to be acquired before the operation can be complete. On return, there may be data remaining in the source buffer. If an invalid byte sequence is read, the character'?'
is written to the string builder in its place.- Parameters:
src
- the source bufferbuilder
- the destination builder- Returns:
true
if the entire string was read,false
if more data is needed
-
readModifiedUtf8Z
Read aNUL
-terminatedmodified UTF-8
string from a byte buffer, appending the results to the given string builder. If noNUL
byte is encountered,false
is returned, indicating that more data needs to be acquired before the operation can be complete. On return, there may be data remaining in the source buffer. If an invalid byte sequence is read, the character designated byreplacement
is written to the string builder in its place.- Parameters:
src
- the source bufferbuilder
- the destination builderreplacement
- the replacement character to use- Returns:
true
if the entire string was read,false
if more data is needed
-
readModifiedUtf8Line
Read a single line ofmodified UTF-8
text from a byte buffer, appending the results to the given string builder. If noEOL
character is encountered,false
is returned, indicating that more data needs to be acquired before the operation can be complete. On return, there may be data remaining in the source buffer. If an invalid byte is read, the character'?'
is written to the string builder in its place. TheEOL
character will be included in the resultant string.- Parameters:
src
- the source bufferbuilder
- the destination builder- Returns:
true
if the entire string was read,false
if more data is needed
-
readModifiedUtf8Line
Read a single line ofmodified UTF-8
text from a byte buffer, appending the results to the given string builder. If noEOL
character is encountered,false
is returned, indicating that more data needs to be acquired before the operation can be complete. On return, there may be data remaining in the source buffer. If an invalid byte is read, the character designated byreplacement
is written to the string builder in its place. TheEOL
character will be included in the resultant string.- Parameters:
src
- the source bufferbuilder
- the destination builderreplacement
- the replacement character for invalid bytes- Returns:
true
if the entire string was read,false
if more data is needed
-
readModifiedUtf8Line
public static boolean readModifiedUtf8Line(ByteBuffer src, StringBuilder builder, char replacement, char delimiter) Read a single line ofmodified UTF-8
text from a byte buffer, appending the results to the given string builder. If noEOL
character is encountered,false
is returned, indicating that more data needs to be acquired before the operation can be complete. On return, there may be data remaining in the source buffer. If an invalid byte is read, the character designated byreplacement
is written to the string builder in its place. The delimiter character will be included in the resultant string.- Parameters:
src
- the source bufferbuilder
- the destination builderreplacement
- the replacement character for invalid bytesdelimiter
- the character which marks the end of the line- Returns:
true
if the entire string was read,false
if more data is needed
-
readLine
Read a single line of text from a byte buffer, appending the results to the given string builder. If noEOL
character is encountered,false
is returned, indicating that more data needs to be acquired before the operation can be complete. On return, there may be data remaining in the source buffer. Invalid bytes are handled according to the policy specified by thedecoder
instance. Since this method decodes only one character at a time, it should not be expected to have the same performance as the other optimized, character set-specific methods specified in this class. TheEOL
character will be included in the resultant string.- Parameters:
src
- the source bufferbuilder
- the destination builderdecoder
- the decoder to use- Returns:
true
if the entire string was read,false
if more data is needed
-
readLine
public static boolean readLine(ByteBuffer src, StringBuilder builder, CharsetDecoder decoder, char delimiter) Read a single line of text from a byte buffer, appending the results to the given string builder. If no delimiter character is encountered,false
is returned, indicating that more data needs to be acquired before the operation can be complete. On return, there may be data remaining in the source buffer. Invalid bytes are handled according to the policy specified by thedecoder
instance. Since this method decodes only one character at a time, it should not be expected to have the same performance as the other optimized, character set-specific methods specified in this class. The delimiter character will be included in the resultant string.- Parameters:
src
- the source bufferbuilder
- the destination builderdecoder
- the decoder to usedelimiter
- the character which marks the end of the line- Returns:
true
if the entire string was read,false
if more data is needed
-
pooledWrapper
Create a pooled wrapper around a buffer. The buffer is unreferenced for garbage collection when freed or discarded.- Type Parameters:
B
- the buffer type- Parameters:
buffer
- the buffer to wrap- Returns:
- the pooled wrapper
-
globalPooledWrapper
Create a pooled wrapper around a buffer that was allocated viaByteBufferPool
. The buffer is freed to the global pool when freed.- Parameters:
buffer
- the buffer to wrap- Returns:
- the pooled wrapper
-
emptyPooledByteBuffer
Create a "pooled" empty buffer. Discarding or freeing the buffer has no effect; the returned buffer is always empty.- Returns:
- a new pooled empty buffer
-
sliceAllocator
A buffer allocator which allocates slices off of the given buffer. Once the buffer is exhausted, further attempts to allocate buffers will result inBufferUnderflowException
.- Parameters:
buffer
- the source buffer- Returns:
- the slice allocator
-
allocatedBufferPool
public static <B extends Buffer> Pool<B> allocatedBufferPool(BufferAllocator<B> allocator, int size) A buffer pool which allocates a new buffer on every allocate request, and discards buffers on free.- Type Parameters:
B
- the buffer type- Parameters:
allocator
- the buffer allocatorsize
- the buffer size- Returns:
- the buffer pool
-
secureBufferPool
A byte buffer pool which zeroes the content of the buffer before re-pooling it.- Parameters:
delegate
- the delegate pool- Returns:
- the wrapper pool
-
isSecureBufferPool
Determine whether the given pool is a secure pool. Note that this test will fail if used on a pool which wraps a secure pool.- Parameters:
pool
- the pool to test- Returns:
true
if it is a secure pool instance
-
zero
Zero a buffer. Ensures that any potentially sensitive information in the buffer is overwritten.- Parameters:
buffer
- the buffer
-
zero
Zero a buffer. Ensures that any potentially sensitive information in the buffer is overwritten.- Parameters:
buffer
- the buffer
-
isDirect
Determine whether the given buffers list is comprised solely of direct buffers or solely of heap buffers.- Parameters:
buffers
- the buffers- Returns:
true
if all the buffers are direct,false
if they are all heap buffers- Throws:
IllegalArgumentException
- if both direct and heap buffers were found, or if a buffer isnull
-
isDirect
Determine whether the given buffers list is comprised solely of direct buffers or solely of heap buffers.- Parameters:
buffers
- the buffers- Returns:
true
if all the buffers are direct,false
if they are all heap buffers- Throws:
IllegalArgumentException
- if both direct and heap buffers were found, or if a buffer isnull
-
assertWritable
public static void assertWritable(Buffer[] buffers, int offs, int len) throws ReadOnlyBufferException Assert the writability of the given buffers.- Parameters:
buffers
- the buffers arrayoffs
- the offset in the array to start searchinglen
- the number of buffers to check- Throws:
ReadOnlyBufferException
- if any of the buffers are read-only
-
assertWritable
Assert the writability of the given buffers.- Parameters:
buffers
- the buffers array- Throws:
ReadOnlyBufferException
- if any of the buffers are read-only
-
addRandom
Addcount
bytes of random data to the target buffer.- Parameters:
target
- the target bufferrandom
- the RNGcount
- the number of bytes to add
-
addRandom
Addcount
bytes of random data to the target buffer using the thread-local RNG.- Parameters:
target
- the target buffercount
- the number of bytes to add
-
addRandom
Add a random amount of random data to the target buffer.- Parameters:
target
- the target bufferrandom
- the RNG
-
addRandom
Add a random amount of random data to the target buffer using the thread-local RNG.- Parameters:
target
- the target buffer
-
fillFromStream
Fill a buffer from an input stream. Specially optimized for heap buffers. If a partial transfer occurs due to interruption, the buffer's position is updated accordingly.- Parameters:
target
- the target buffersource
- the source stream- Returns:
- the number of bytes transferred, or
-1
if no bytes were moved due to end-of-stream - Throws:
IOException
- if the stream read fails
-
debugString
Get a debug-friendly description of the buffer.- Parameters:
buffer
- the buffer to describe- Returns:
- the string
-
debugString
Get a debug-friendly description of the buffer.- Parameters:
buffers
- the buffers to describeoffs
- the offset into the arraylen
- the number of buffers- Returns:
- the string
-
emptyToStream
Empty a buffer to an output stream. Specially optimized for heap buffers. If a partial transfer occurs due to interruption, the buffer's position is updated accordingly.- Parameters:
target
- the target streamsource
- the source buffer- Throws:
IOException
- if the stream write fails
-