- java.lang.Object
-
- org.xnio.Buffers
-
public final class Buffers extends java.lang.Object
Buffer utility methods.
-
-
Field Summary
Fields Modifier and Type Field Description static java.nio.ByteBuffer
EMPTY_BYTE_BUFFER
The empty byte buffer.static Pooled<java.nio.ByteBuffer>
EMPTY_POOLED_BYTE_BUFFER
The empty pooled byte buffer.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addRandom(java.nio.ByteBuffer target)
Add a random amount of random data to the target buffer using the thread-local RNG.static void
addRandom(java.nio.ByteBuffer target, int count)
Addcount
bytes of random data to the target buffer using the thread-local RNG.static void
addRandom(java.nio.ByteBuffer target, java.util.Random random)
Add a random amount of random data to the target buffer.static void
addRandom(java.nio.ByteBuffer target, java.util.Random random, int count)
Addcount
bytes of random data to the target buffer.static <B extends java.nio.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.static void
assertWritable(java.nio.Buffer... buffers)
Assert the writability of the given buffers.static void
assertWritable(java.nio.Buffer[] buffers, int offs, int len)
Assert the writability of the given buffers.static <T extends java.nio.Buffer>
Tclear(T buffer)
Clear a buffer.static int
copy(int count, java.nio.ByteBuffer[] destinations, int offset, int length, java.nio.ByteBuffer source)
Copy at mostcount
bytes fromsources
intodestinations
in a "scatter" fashion.static int
copy(int count, java.nio.ByteBuffer destination, java.nio.ByteBuffer source)
Copy at mostcount
bytes fromsource
intodestination
.static int
copy(int count, java.nio.ByteBuffer destination, java.nio.ByteBuffer[] sources, int offset, int length)
Copy at mostcount
bytes fromsources
intodestination
in a "gather" fashion.static long
copy(long count, java.nio.ByteBuffer[] destinations, int destOffset, int destLength, java.nio.ByteBuffer[] sources, int srcOffset, int srcLength)
Copy at mostcount
bytes fromsources
intodestinations
by a combined "scatter"/"gather" operation.static int
copy(java.nio.ByteBuffer[] destinations, int offset, int length, java.nio.ByteBuffer source)
Copy as many bytes as possible fromsources
intodestinations
in a "scatter" fashion.static long
copy(java.nio.ByteBuffer[] destinations, int destOffset, int destLength, java.nio.ByteBuffer[] sources, int srcOffset, int srcLength)
Copy as many bytes as possible fromsources
intodestinations
by a combined "scatter"/"gather" operation.static java.nio.ByteBuffer
copy(java.nio.ByteBuffer buffer, int count, BufferAllocator<java.nio.ByteBuffer> allocator)
Copy a portion of the buffer into a newly allocated buffer.static int
copy(java.nio.ByteBuffer destination, java.nio.ByteBuffer source)
Copy as many bytes as possible fromsource
intodestination
.static int
copy(java.nio.ByteBuffer destination, java.nio.ByteBuffer[] sources, int offset, int length)
Copy as many bytes as possible fromsources
intodestination
in a "gather" fashion.static java.lang.Object
createDumper(java.nio.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 java.lang.Object
createDumper(java.nio.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 java.lang.String
debugString(java.nio.ByteBuffer buffer)
Get a debug-friendly description of the buffer.static java.lang.String
debugString(java.nio.ByteBuffer[] buffers, int offs, int len)
Get a debug-friendly description of the buffer.static void
dump(java.nio.ByteBuffer buffer, java.lang.Appendable dest, int indent, int columns)
Dump a byte buffer to the given target.static void
dump(java.nio.CharBuffer buffer, java.lang.Appendable dest, int indent, int columns)
Dump a character buffer to the given target.static Pooled<java.nio.ByteBuffer>
emptyPooledByteBuffer()
Create a "pooled" empty buffer.static void
emptyToStream(java.io.OutputStream target, java.nio.ByteBuffer source)
Empty a buffer to an output stream.static java.nio.ByteBuffer
fill(java.nio.ByteBuffer buffer, int value, int count)
Fill a buffer with a repeated value.static java.nio.CharBuffer
fill(java.nio.CharBuffer buffer, int value, int count)
Fill a buffer with a repeated value.static java.nio.IntBuffer
fill(java.nio.IntBuffer buffer, int value, int count)
Fill a buffer with a repeated value.static java.nio.LongBuffer
fill(java.nio.LongBuffer buffer, long value, int count)
Fill a buffer with a repeated value.static java.nio.ShortBuffer
fill(java.nio.ShortBuffer buffer, int value, int count)
Fill a buffer with a repeated value.static int
fillFromStream(java.nio.ByteBuffer target, java.io.InputStream source)
Fill a buffer from an input stream.static <T extends java.nio.Buffer>
Tflip(T buffer)
Flip a buffer.static java.lang.String
getModifiedUtf8(java.nio.ByteBuffer src)
Get a modified UTF-8 string from the remainder of the buffer.static java.lang.String
getModifiedUtf8Z(java.nio.ByteBuffer src)
Get a 0-terminated string from the byte buffer, decoding it using "modified UTF-8" encoding.static Pooled<java.nio.ByteBuffer>
globalPooledWrapper(java.nio.ByteBuffer buffer)
Create a pooled wrapper around a buffer that was allocated viaByteBufferPool
.static boolean
hasRemaining(java.nio.Buffer[] buffers)
Determine whether any of the buffers has remaining data.static boolean
hasRemaining(java.nio.Buffer[] buffers, int offs, int len)
Determine whether any of the buffers has remaining data.static boolean
isDirect(java.nio.Buffer... buffers)
Determine whether the given buffers list is comprised solely of direct buffers or solely of heap buffers.static boolean
isDirect(java.nio.Buffer[] buffers, int offset, int length)
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 java.nio.Buffer>
Tlimit(T buffer, int limit)
Set the buffer limit.static <T extends java.nio.Buffer>
Tmark(T buffer)
Set the buffer mark.static <B extends java.nio.Buffer>
Pooled<B>pooledWrapper(B buffer)
Create a pooled wrapper around a buffer.static <T extends java.nio.Buffer>
Tposition(T buffer, int position)
Set the buffer position.static java.nio.ByteBuffer
putModifiedUtf8(java.nio.ByteBuffer dest, java.lang.String orig)
Put the string into the byte buffer, encoding it using "modified UTF-8" encoding.static void
readAscii(java.nio.ByteBuffer src, java.lang.StringBuilder builder)
Read the remainder of a buffer as ASCII text, appending the results to the given string builder.static void
readAscii(java.nio.ByteBuffer src, java.lang.StringBuilder builder, char replacement)
Read the remainder of a buffer as ASCII text, appending the results to the given string builder.static void
readAscii(java.nio.ByteBuffer src, java.lang.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(java.nio.ByteBuffer src, java.lang.StringBuilder builder)
Read a single line of ASCII text from a byte buffer, appending the results to the given string builder.static boolean
readAsciiLine(java.nio.ByteBuffer src, java.lang.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(java.nio.ByteBuffer src, java.lang.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(java.nio.ByteBuffer src, java.lang.StringBuilder builder)
Read an ASCIIZ (NUL
-terminated) string from a byte buffer, appending the results to the given string builder.static boolean
readAsciiZ(java.nio.ByteBuffer src, java.lang.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(java.nio.ByteBuffer src, java.lang.StringBuilder builder)
Read the remainder of a buffer as Latin-1 text, appending the results to the given string builder.static boolean
readLatin1Line(java.nio.ByteBuffer src, java.lang.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(java.nio.ByteBuffer src, java.lang.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(java.nio.ByteBuffer src, java.lang.StringBuilder builder)
Read aNUL
-terminated Latin-1 string from a byte buffer, appending the results to the given string builder.static boolean
readLine(java.nio.ByteBuffer src, java.lang.StringBuilder builder, java.nio.charset.CharsetDecoder decoder)
Read a single line of text from a byte buffer, appending the results to the given string builder.static boolean
readLine(java.nio.ByteBuffer src, java.lang.StringBuilder builder, java.nio.charset.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(java.nio.ByteBuffer src, java.lang.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(java.nio.ByteBuffer src, java.lang.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(java.nio.ByteBuffer src, java.lang.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(java.nio.ByteBuffer src, java.lang.StringBuilder builder)
Read aNUL
-terminatedmodified UTF-8
string from a byte buffer, appending the results to the given string builder.static boolean
readModifiedUtf8Z(java.nio.ByteBuffer src, java.lang.StringBuilder builder, char replacement)
Read aNUL
-terminatedmodified UTF-8
string from a byte buffer, appending the results to the given string builder.static long
remaining(java.nio.Buffer[] buffers)
Get the total remaining size of all the given buffers.static long
remaining(java.nio.Buffer[] buffers, int offs, int len)
Get the total remaining size of all the given buffers.static <T extends java.nio.Buffer>
Treset(T buffer)
Reset the buffer.static <T extends java.nio.Buffer>
Trewind(T buffer)
Rewind the buffer.static Pool<java.nio.ByteBuffer>
secureBufferPool(Pool<java.nio.ByteBuffer> delegate)
A byte buffer pool which zeroes the content of the buffer before re-pooling it.static <T extends java.nio.Buffer>
Tskip(T buffer, int cnt)
Advance a buffer's position relative to its current position.static java.nio.ByteBuffer
slice(java.nio.ByteBuffer buffer, int sliceSize)
Slice the buffer.static java.nio.CharBuffer
slice(java.nio.CharBuffer buffer, int sliceSize)
Slice the buffer.static java.nio.IntBuffer
slice(java.nio.IntBuffer buffer, int sliceSize)
Slice the buffer.static java.nio.LongBuffer
slice(java.nio.LongBuffer buffer, int sliceSize)
Slice the buffer.static java.nio.ShortBuffer
slice(java.nio.ShortBuffer buffer, int sliceSize)
Slice the buffer.static BufferAllocator<java.nio.ByteBuffer>
sliceAllocator(java.nio.ByteBuffer buffer)
A buffer allocator which allocates slices off of the given buffer.static byte[]
take(java.nio.ByteBuffer buffer)
Take all of the remaining bytes from the buffer and return them in an array.static byte[]
take(java.nio.ByteBuffer[] buffers, int offs, int len)
Take all of the remaining bytes from the buffers and return them in an array.static byte[]
take(java.nio.ByteBuffer buffer, int cnt)
Take a certain number of bytes from the buffer and return them in an array.static char[]
take(java.nio.CharBuffer buffer)
Take all of the remaining chars from the buffer and return them in an array.static char[]
take(java.nio.CharBuffer buffer, int cnt)
Take a certain number of chars from the buffer and return them in an array.static int[]
take(java.nio.IntBuffer buffer)
Take all of the remaining ints from the buffer and return them in an array.static int[]
take(java.nio.IntBuffer buffer, int cnt)
Take a certain number of ints from the buffer and return them in an array.static long[]
take(java.nio.LongBuffer buffer)
Take all of the remaining longs from the buffer and return them in an array.static long[]
take(java.nio.LongBuffer buffer, int cnt)
Take a certain number of longs from the buffer and return them in an array.static short[]
take(java.nio.ShortBuffer buffer)
Take all of the remaining shorts from the buffer and return them in an array.static short[]
take(java.nio.ShortBuffer buffer, int cnt)
Take a certain number of shorts from the buffer and return them in an array.static long
trySkip(java.nio.Buffer[] buffers, int offs, int len, long cnt)
Attempt to advance a series of buffers' overall position relative to its current position.static int
trySkip(java.nio.Buffer buffer, int cnt)
Attempt to advance a buffer's position relative to its current position.static <T extends java.nio.Buffer>
Tunget(T buffer, int cnt)
Rewind a buffer's position relative to its current position.static void
zero(java.nio.ByteBuffer buffer)
Zero a buffer.static void
zero(java.nio.CharBuffer buffer)
Zero a buffer.
-
-
-
Field Detail
-
EMPTY_BYTE_BUFFER
public static final java.nio.ByteBuffer EMPTY_BYTE_BUFFER
The empty byte buffer.
-
EMPTY_POOLED_BYTE_BUFFER
public static final Pooled<java.nio.ByteBuffer> EMPTY_POOLED_BYTE_BUFFER
The empty pooled byte buffer. Freeing or discarding this buffer has no effect.
-
-
Method Detail
-
flip
public static <T extends java.nio.Buffer> T flip(T buffer)
Flip a buffer.- Type Parameters:
T
- the buffer type- Parameters:
buffer
- the buffer to flip- Returns:
- the buffer instance
- See Also:
Buffer.flip()
-
clear
public static <T extends java.nio.Buffer> T clear(T buffer)
Clear a buffer.- Type Parameters:
T
- the buffer type- Parameters:
buffer
- the buffer to clear- Returns:
- the buffer instance
- See Also:
Buffer.clear()
-
limit
public static <T extends java.nio.Buffer> T limit(T buffer, int 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:
Buffer.limit(int)
-
mark
public static <T extends java.nio.Buffer> T mark(T buffer)
Set the buffer mark.- Type Parameters:
T
- the buffer type- Parameters:
buffer
- the buffer to mark- Returns:
- the buffer instance
- See Also:
Buffer.mark()
-
position
public static <T extends java.nio.Buffer> T position(T buffer, int 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:
Buffer.position(int)
-
reset
public static <T extends java.nio.Buffer> T reset(T buffer)
Reset the buffer.- Type Parameters:
T
- the buffer type- Parameters:
buffer
- the buffer to reset- Returns:
- the buffer instance
- See Also:
Buffer.reset()
-
rewind
public static <T extends java.nio.Buffer> T rewind(T buffer)
Rewind the buffer.- Type Parameters:
T
- the buffer type- Parameters:
buffer
- the buffer to rewind- Returns:
- the buffer instance
- See Also:
Buffer.rewind()
-
slice
public static java.nio.ByteBuffer slice(java.nio.ByteBuffer buffer, int sliceSize)
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:
ByteBuffer.slice()
-
copy
public static java.nio.ByteBuffer copy(java.nio.ByteBuffer buffer, int count, BufferAllocator<java.nio.ByteBuffer> allocator)
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
public static int copy(java.nio.ByteBuffer destination, java.nio.ByteBuffer source)
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
public static int copy(java.nio.ByteBuffer[] destinations, int offset, int length, java.nio.ByteBuffer source)
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
public static int copy(java.nio.ByteBuffer destination, java.nio.ByteBuffer[] sources, int offset, int length)
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(java.nio.ByteBuffer[] destinations, int destOffset, int destLength, java.nio.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
public static int copy(int count, java.nio.ByteBuffer destination, java.nio.ByteBuffer source)
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, java.nio.ByteBuffer[] destinations, int offset, int length, java.nio.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, java.nio.ByteBuffer destination, java.nio.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, java.nio.ByteBuffer[] destinations, int destOffset, int destLength, java.nio.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
public static java.nio.ByteBuffer fill(java.nio.ByteBuffer buffer, int value, int count)
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
public static java.nio.CharBuffer slice(java.nio.CharBuffer buffer, int sliceSize)
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:
CharBuffer.slice()
-
fill
public static java.nio.CharBuffer fill(java.nio.CharBuffer buffer, int value, int count)
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
public static java.nio.ShortBuffer slice(java.nio.ShortBuffer buffer, int sliceSize)
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:
ShortBuffer.slice()
-
fill
public static java.nio.ShortBuffer fill(java.nio.ShortBuffer buffer, int value, int count)
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
public static java.nio.IntBuffer slice(java.nio.IntBuffer buffer, int sliceSize)
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:
IntBuffer.slice()
-
fill
public static java.nio.IntBuffer fill(java.nio.IntBuffer buffer, int value, int count)
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
public static java.nio.LongBuffer slice(java.nio.LongBuffer buffer, int sliceSize)
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:
LongBuffer.slice()
-
fill
public static java.nio.LongBuffer fill(java.nio.LongBuffer buffer, long value, int count)
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
public static <T extends java.nio.Buffer> T skip(T buffer, int cnt) throws java.nio.BufferUnderflowException
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:
java.nio.BufferUnderflowException
- if there are fewer thancnt
bytes remaining- See Also:
Buffer.position(int)
-
trySkip
public static int trySkip(java.nio.Buffer buffer, int cnt)
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:
Buffer.position(int)
-
trySkip
public static long trySkip(java.nio.Buffer[] buffers, int offs, int len, long cnt)
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:
Buffer.position(int)
-
unget
public static <T extends java.nio.Buffer> T unget(T buffer, int cnt)
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:
Buffer.position(int)
-
take
public static byte[] take(java.nio.ByteBuffer buffer, int cnt)
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
public static char[] take(java.nio.CharBuffer buffer, int cnt)
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
public static short[] take(java.nio.ShortBuffer buffer, int cnt)
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
public static int[] take(java.nio.IntBuffer buffer, int cnt)
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
public static long[] take(java.nio.LongBuffer buffer, int cnt)
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
public static byte[] take(java.nio.ByteBuffer buffer)
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
public static byte[] take(java.nio.ByteBuffer[] buffers, int offs, int len)
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
public static char[] take(java.nio.CharBuffer buffer)
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
public static short[] take(java.nio.ShortBuffer buffer)
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
public static int[] take(java.nio.IntBuffer buffer)
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
public static long[] take(java.nio.LongBuffer buffer)
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
public static java.lang.Object createDumper(java.nio.ByteBuffer buffer, int indent, int columns)
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(java.nio.ByteBuffer buffer, java.lang.Appendable dest, int indent, int columns) throws java.io.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:
java.io.IOException
- if an error occurs during append
-
createDumper
public static java.lang.Object createDumper(java.nio.CharBuffer buffer, int indent, int columns)
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(java.nio.CharBuffer buffer, java.lang.Appendable dest, int indent, int columns) throws java.io.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:
java.io.IOException
- if an error occurs during append
-
hasRemaining
public static boolean hasRemaining(java.nio.Buffer[] buffers, int offs, int len)
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
public static boolean hasRemaining(java.nio.Buffer[] buffers)
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
public static long remaining(java.nio.Buffer[] buffers, int offs, int len)
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
public static long remaining(java.nio.Buffer[] buffers)
Get the total remaining size of all the given buffers.- Parameters:
buffers
- the buffers- Returns:
- the number of remaining elements
-
putModifiedUtf8
public static java.nio.ByteBuffer putModifiedUtf8(java.nio.ByteBuffer dest, java.lang.String orig) throws java.nio.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:
java.nio.BufferOverflowException
- if there is not enough space in the buffer for the complete string- See Also:
DataOutput.writeUTF(String)
-
getModifiedUtf8Z
public static java.lang.String getModifiedUtf8Z(java.nio.ByteBuffer src) throws java.nio.BufferUnderflowException
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:
java.nio.BufferUnderflowException
- if the end of the buffer was reached before encountering a0
-
getModifiedUtf8
public static java.lang.String getModifiedUtf8(java.nio.ByteBuffer src) throws java.nio.BufferUnderflowException
Get a modified UTF-8 string from the remainder of the buffer.- Parameters:
src
- the buffer- Returns:
- the modified UTF-8 string
- Throws:
java.nio.BufferUnderflowException
- if the buffer ends abruptly in the midst of a single character
-
readAsciiZ
public static boolean readAsciiZ(java.nio.ByteBuffer src, java.lang.StringBuilder builder)
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
public static boolean readAsciiZ(java.nio.ByteBuffer src, java.lang.StringBuilder builder, char replacement)
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
public static boolean readAsciiLine(java.nio.ByteBuffer src, java.lang.StringBuilder builder)
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
public static boolean readAsciiLine(java.nio.ByteBuffer src, java.lang.StringBuilder builder, char replacement)
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(java.nio.ByteBuffer src, java.lang.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
public static void readAscii(java.nio.ByteBuffer src, java.lang.StringBuilder builder)
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
public static void readAscii(java.nio.ByteBuffer src, java.lang.StringBuilder builder, char replacement)
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
public static void readAscii(java.nio.ByteBuffer src, java.lang.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. 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
public static boolean readLatin1Z(java.nio.ByteBuffer src, java.lang.StringBuilder builder)
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
public static boolean readLatin1Line(java.nio.ByteBuffer src, java.lang.StringBuilder builder)
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
public static boolean readLatin1Line(java.nio.ByteBuffer src, java.lang.StringBuilder builder, char delimiter)
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
public static void readLatin1(java.nio.ByteBuffer src, java.lang.StringBuilder builder)
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
public static boolean readModifiedUtf8Z(java.nio.ByteBuffer src, java.lang.StringBuilder builder)
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
public static boolean readModifiedUtf8Z(java.nio.ByteBuffer src, java.lang.StringBuilder builder, char replacement)
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
public static boolean readModifiedUtf8Line(java.nio.ByteBuffer src, java.lang.StringBuilder builder)
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
public static boolean readModifiedUtf8Line(java.nio.ByteBuffer src, java.lang.StringBuilder builder, char replacement)
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(java.nio.ByteBuffer src, java.lang.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
public static boolean readLine(java.nio.ByteBuffer src, java.lang.StringBuilder builder, java.nio.charset.CharsetDecoder decoder)
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(java.nio.ByteBuffer src, java.lang.StringBuilder builder, java.nio.charset.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
public static <B extends java.nio.Buffer> Pooled<B> pooledWrapper(B buffer)
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
public static Pooled<java.nio.ByteBuffer> globalPooledWrapper(java.nio.ByteBuffer buffer)
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
public static Pooled<java.nio.ByteBuffer> 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
public static BufferAllocator<java.nio.ByteBuffer> sliceAllocator(java.nio.ByteBuffer buffer)
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 java.nio.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
public static Pool<java.nio.ByteBuffer> secureBufferPool(Pool<java.nio.ByteBuffer> delegate)
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
public static boolean isSecureBufferPool(Pool<?> pool)
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
public static void zero(java.nio.ByteBuffer buffer)
Zero a buffer. Ensures that any potentially sensitive information in the buffer is overwritten.- Parameters:
buffer
- the buffer
-
zero
public static void zero(java.nio.CharBuffer buffer)
Zero a buffer. Ensures that any potentially sensitive information in the buffer is overwritten.- Parameters:
buffer
- the buffer
-
isDirect
public static boolean isDirect(java.nio.Buffer... buffers) throws java.lang.IllegalArgumentException
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:
java.lang.IllegalArgumentException
- if both direct and heap buffers were found, or if a buffer isnull
-
isDirect
public static boolean isDirect(java.nio.Buffer[] buffers, int offset, int length)
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:
java.lang.IllegalArgumentException
- if both direct and heap buffers were found, or if a buffer isnull
-
assertWritable
public static void assertWritable(java.nio.Buffer[] buffers, int offs, int len) throws java.nio.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:
java.nio.ReadOnlyBufferException
- if any of the buffers are read-only
-
assertWritable
public static void assertWritable(java.nio.Buffer... buffers) throws java.nio.ReadOnlyBufferException
Assert the writability of the given buffers.- Parameters:
buffers
- the buffers array- Throws:
java.nio.ReadOnlyBufferException
- if any of the buffers are read-only
-
addRandom
public static void addRandom(java.nio.ByteBuffer target, java.util.Random random, int count)
Addcount
bytes of random data to the target buffer.- Parameters:
target
- the target bufferrandom
- the RNGcount
- the number of bytes to add
-
addRandom
public static void addRandom(java.nio.ByteBuffer target, int count)
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
public static void addRandom(java.nio.ByteBuffer target, java.util.Random random)
Add a random amount of random data to the target buffer.- Parameters:
target
- the target bufferrandom
- the RNG
-
addRandom
public static void addRandom(java.nio.ByteBuffer target)
Add a random amount of random data to the target buffer using the thread-local RNG.- Parameters:
target
- the target buffer
-
fillFromStream
public static int fillFromStream(java.nio.ByteBuffer target, java.io.InputStream source) throws java.io.IOException
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:
java.io.IOException
- if the stream read fails
-
debugString
public static java.lang.String debugString(java.nio.ByteBuffer buffer)
Get a debug-friendly description of the buffer.- Parameters:
buffer
- the buffer to describe- Returns:
- the string
-
debugString
public static java.lang.String debugString(java.nio.ByteBuffer[] buffers, int offs, int len)
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
public static void emptyToStream(java.io.OutputStream target, java.nio.ByteBuffer source) throws java.io.IOException
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:
java.io.IOException
- if the stream write fails
-
-