Top Description Inners Fields Constructors Methods
org.xnio

public final Class Buffers

extends Object
Class Inheritance
Imports
java.io.DataInput, .DataOutput, .InputStream, .InterruptedIOException, .OutputStream, .IOException, java.nio.Buffer, .BufferUnderflowException, .ByteBuffer, .CharBuffer, .IntBuffer, .LongBuffer, .ReadOnlyBufferException, .ShortBuffer, .BufferOverflowException, java.nio.charset.CharsetDecoder, .CoderResult, java.util.Arrays, .Random, java.util.concurrent.atomic.AtomicIntegerFieldUpdater, org.wildfly.common.ref.CleanerReference, .Reaper, .Reference

Buffer utility methods.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
Buffers.SecureByteBufferPool

References Deprecated Pool is deprecated or references (maybe indirectly) at least one deprecated element.
private static class
Buffers.SecurePooledByteBuffer

References Deprecated Pooled is deprecated or references (maybe indirectly) at least one deprecated element.

Field Summary

Modifier and TypeField and Description
public static final ByteBuffer
EMPTY_BYTE_BUFFER

The empty byte buffer.

public static final Pooled<ByteBuffer>
EMPTY_POOLED_BYTE_BUFFER

References Deprecated Pooled is deprecated or references (maybe indirectly) at least one deprecated element.
The empty pooled byte buffer.
private static final byte[]

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static void
addRandom(ByteBuffer
the target buffer
target
,
Random
the RNG
random
,
int
the number of bytes to add
count
)

Add count bytes of random data to the target buffer.

public static void
addRandom(ByteBuffer
the target buffer
target
,
int
the number of bytes to add
count
)

Add count bytes of random data to the target buffer using the thread-local RNG.

public static void
addRandom(ByteBuffer
the target buffer
target
,
Random
the RNG
random
)

Add a random amount of random data to the target buffer.

public static void
addRandom(ByteBuffer
the target buffer
target
)

Add a random amount of random data to the target buffer using the thread-local RNG.

public static <
the buffer type
B extends Buffer
>
Pool<B>

Returns:

the buffer pool
allocatedBufferPool
(final BufferAllocator<B>
the buffer allocator
allocator
,
final int
the buffer size
size
)
References Deprecated Pool is deprecated or references (maybe indirectly) at least one deprecated element.

A buffer pool which allocates a new buffer on every allocate request, and discards buffers on free.

private static void
appendHexRow(final ByteBuffer buffer, final Appendable dest, final int startPos, final int columns)

private static void
appendHexRow(final CharBuffer buffer, final Appendable dest, final int startPos, final int columns)

private static void
appendTextRow(final ByteBuffer buffer, final Appendable dest, final int startPos, final int columns)

private static void
appendTextRow(final CharBuffer buffer, final Appendable dest, final int startPos, final int columns)

public static void
assertWritable(Buffer[]
the buffers array
buffers
,
int
the offset in the array to start searching
offs
,
int
the number of buffers to check
len
)

Assert the writability of the given buffers.

public static void
assertWritable(Buffer...
the buffers array
buffers
)

Assert the writability of the given buffers.

public static <
the buffer type
T extends Buffer
>
T

Returns:

the buffer instance
clear
(T
the buffer to clear
buffer
)

Clear a buffer.

public static ByteBuffer

Returns:

the buffer slice
copy
(ByteBuffer
the buffer to slice
buffer
,
int
the size of the copy
count
,
BufferAllocator<ByteBuffer>
the buffer allocator to use
allocator
)

Copy a portion of the buffer into a newly allocated buffer.

public static int

Returns:

the number of bytes put into the destination buffer
copy
(final ByteBuffer
the destination buffer
destination
,
final ByteBuffer
the source buffer
source
)

Copy as many bytes as possible from source into destination.

public static int

Returns:

the number of bytes put into the destination buffers
copy
(final ByteBuffer[]
the destination buffers
destinations
,
final int
the offset into the destination buffers array
offset
,
final int
the number of buffers to update
length
,
final ByteBuffer
the source buffer
source
)

Copy as many bytes as possible from sources into destinations in a "scatter" fashion.

public static int

Returns:

the number of bytes put into the destination buffers
copy
(final ByteBuffer
the destination buffer
destination
,
final ByteBuffer[]
the source buffers
sources
,
final int
the offset into the source buffers array
offset
,
final int
the number of buffers to read from
length
)

Copy as many bytes as possible from sources into destination in a "gather" fashion.

public static long

Returns:

the number of bytes put into the destination buffers
copy
(final ByteBuffer[]
the destination buffers
destinations
,
final int
the offset into the destination buffers array
destOffset
,
final int
the number of buffers to write to
destLength
,
final ByteBuffer[]
the source buffers
sources
,
final int
the offset into the source buffers array
srcOffset
,
final int
the number of buffers to read from
srcLength
)

Copy as many bytes as possible from sources into destinations by a combined "scatter"/"gather" operation.

public static int

Returns:

the number of bytes put into the destination buffer
copy
(int
the maximum number of bytes to copy
count
,
final ByteBuffer
the destination buffer
destination
,
final ByteBuffer
the source buffer
source
)

Copy at most count bytes from source into destination.

public static int

Returns:

the number of bytes put into the destination buffers
copy
(int
the maximum number of bytes to copy
count
,
final ByteBuffer[]
the destination buffers
destinations
,
final int
the offset into the destination buffers array
offset
,
final int
the number of buffers to update
length
,
final ByteBuffer
the source buffer
source
)

Copy at most count bytes from sources into destinations in a "scatter" fashion.

public static int

Returns:

the number of bytes put into the destination buffers
copy
(int
the maximum number of bytes to copy
count
,
final ByteBuffer
the destination buffer
destination
,
final ByteBuffer[]
the source buffers
sources
,
final int
the offset into the source buffers array
offset
,
final int
the number of buffers to read from
length
)

Copy at most count bytes from sources into destination in a "gather" fashion.

public static long

Returns:

the number of bytes put into the destination buffers
copy
(long
the maximum number of bytes to copy
count
,
final ByteBuffer[]
the destination buffers
destinations
,
final int
the offset into the destination buffers array
destOffset
,
final int
the number of buffers to write to
destLength
,
final ByteBuffer[]
the source buffers
sources
,
final int
the offset into the source buffers array
srcOffset
,
final int
the number of buffers to read from
srcLength
)

Copy at most count bytes from sources into destinations by a combined "scatter"/"gather" operation.

public static Object

Returns:

a stringable object
createDumper
(final ByteBuffer
the buffer
buffer
,
final int
the indentation to use
indent
,
final int
the number of 8-byte columns
columns
)

Create an object that returns the dumped form of the given byte buffer when its toString() method is called.

public static Object

Returns:

a stringable object
createDumper
(final CharBuffer
the buffer
buffer
,
final int
the indentation to use
indent
,
final int
the number of 8-byte columns
columns
)

Create an object that returns the dumped form of the given character buffer when its toString() method is called.

public static String

Returns:

the string
debugString
(ByteBuffer
the buffer to describe
buffer
)

Get a debug-friendly description of the buffer.

public static String

Returns:

the string
debugString
(ByteBuffer[]
the buffers to describe
buffers
,
int
the offset into the array
offs
,
int
the number of buffers
len
)

Get a debug-friendly description of the buffer.

public static void
dump(final ByteBuffer
the buffer
buffer
,
final Appendable
the target
dest
,
final int
the indentation to use
indent
,
final int
the number of 8-byte columns
columns
)

Dump a byte buffer to the given target.

public static void
dump(final CharBuffer
the buffer
buffer
,
final Appendable
the target
dest
,
final int
the indentation to use
indent
,
final int
the number of 8-byte columns
columns
)

Dump a character buffer to the given target.

public static Pooled<ByteBuffer>

Returns:

a new pooled empty buffer
emptyPooledByteBuffer
()
References Deprecated Pooled is deprecated or references (maybe indirectly) at least one deprecated element.

Create a "pooled" empty buffer.

public static void
emptyToStream(OutputStream
the target stream
target
,
ByteBuffer
the source buffer
source
)

Empty a buffer to an output stream.

public static ByteBuffer

Returns:

the buffer instance
fill
(ByteBuffer
the buffer to fill
buffer
,
int
the value to fill
value
,
int
the number of bytes to fill
count
)

Fill a buffer with a repeated value.

public static CharBuffer

Returns:

the buffer instance
fill
(CharBuffer
the buffer to fill
buffer
,
int
the value to fill
value
,
int
the number of chars to fill
count
)

Fill a buffer with a repeated value.

public static ShortBuffer

Returns:

the buffer instance
fill
(ShortBuffer
the buffer to fill
buffer
,
int
the value to fill
value
,
int
the number of shorts to fill
count
)

Fill a buffer with a repeated value.

public static IntBuffer

Returns:

the buffer instance
fill
(IntBuffer
the buffer to fill
buffer
,
int
the value to fill
value
,
int
the number of ints to fill
count
)

Fill a buffer with a repeated value.

public static LongBuffer

Returns:

the buffer instance
fill
(LongBuffer
the buffer to fill
buffer
,
long
the value to fill
value
,
int
the number of longs to fill
count
)

Fill a buffer with a repeated value.

public static int

Returns:

the number of bytes transferred, or -1 if no bytes were moved due to end-of-stream
fillFromStream
(ByteBuffer
the target buffer
target
,
InputStream
the source stream
source
)

Fill a buffer from an input stream.

public static <
the buffer type
T extends Buffer
>
T

Returns:

the buffer instance
flip
(T
the buffer to flip
buffer
)

Flip a buffer.

public static String

Returns:

the modified UTF-8 string
getModifiedUtf8
(ByteBuffer
the buffer
src
)

Get a modified UTF-8 string from the remainder of the buffer.

public static String

Returns:

the string
getModifiedUtf8Z
(ByteBuffer
the source buffer
src
)

Get a 0-terminated string from the byte buffer, decoding it using "modified UTF-8" encoding.

public static Pooled<ByteBuffer>

Returns:

the pooled wrapper
globalPooledWrapper
(final ByteBuffer
the buffer to wrap
buffer
)
References Deprecated Pooled is deprecated or references (maybe indirectly) at least one deprecated element.

Create a pooled wrapper around a buffer that was allocated via ByteBufferPool.

public static boolean

Returns:

true if any of the selected buffers has remaining data
hasRemaining
(final Buffer[]
the buffers
buffers
,
final int
the offset into the buffers array
offs
,
final int
the number of buffers to check
len
)

Determine whether any of the buffers has remaining data.

public static boolean

Returns:

true if any of the selected buffers has remaining data
hasRemaining
(final Buffer[]
the buffers
buffers
)

Determine whether any of the buffers has remaining data.

public static boolean

Returns:

true if all the buffers are direct, false if they are all heap buffers
isDirect
(Buffer...
the buffers
buffers
)

Determine whether the given buffers list is comprised solely of direct buffers or solely of heap buffers.

public static boolean

Returns:

true if all the buffers are direct, false if they are all heap buffers
isDirect
(final Buffer[]
the buffers
buffers
,
final int offset, final int length)

Determine whether the given buffers list is comprised solely of direct buffers or solely of heap buffers.

public static boolean

Returns:

true if it is a secure pool instance
isSecureBufferPool
(Pool<?>
the pool to test
pool
)
References Deprecated Pool is deprecated or references (maybe indirectly) at least one deprecated element.

Determine whether the given pool is a secure pool.

public static <
the buffer type
T extends Buffer
>
T

Returns:

the buffer instance
limit
(T
the buffer to set
buffer
,
int
the new limit
limit
)

Set the buffer limit.

public static <
the buffer type
T extends Buffer
>
T

Returns:

the buffer instance
mark
(T
the buffer to mark
buffer
)

Set the buffer mark.

public static <
the buffer type
B extends Buffer
>
Pooled<B>

Returns:

the pooled wrapper
pooledWrapper
(final B
the buffer to wrap
buffer
)
References Deprecated Pooled is deprecated or references (maybe indirectly) at least one deprecated element.

Create a pooled wrapper around a buffer.

public static <
the buffer type
T extends Buffer
>
T

Returns:

the buffer instance
position
(T
the buffer to set
buffer
,
int
the new position
position
)

Set the buffer position.

public static ByteBuffer

Returns:

the byte buffer
putModifiedUtf8
(ByteBuffer
the byte buffer
dest
,
String
the source bytes
orig
)

Put the string into the byte buffer, encoding it using "modified UTF-8" encoding.

public static void
readAscii(final ByteBuffer
the source buffer
src
,
final StringBuilder
the destination builder
builder
)

Read the remainder of a buffer as ASCII text, appending the results to the given string builder.

public static void
readAscii(final ByteBuffer
the source buffer
src
,
final StringBuilder
the destination builder
builder
,
final char
the replacement character for invalid bytes
replacement
)

Read the remainder of a buffer as ASCII text, appending the results to the given string builder.

public static void
readAscii(final ByteBuffer
the source buffer
src
,
final StringBuilder
the destination builder
builder
,
int
the maximum number of characters to write
limit
,
final char
the replacement character for invalid bytes
replacement
)

Read the remainder of a buffer as ASCII text, up to a certain limit, appending the results to the given string builder.

public static boolean

Returns:

true if the entire string was read, false if more data is needed
readAsciiLine
(final ByteBuffer
the source buffer
src
,
final StringBuilder
the destination builder
builder
)

Read a single line of ASCII text from a byte buffer, appending the results to the given string builder.

public static boolean

Returns:

true if the entire string was read, false if more data is needed
readAsciiLine
(final ByteBuffer
the source buffer
src
,
final StringBuilder
the destination builder
builder
,
final char
the replacement character for invalid bytes
replacement
)

Read a single line of ASCII text from a byte buffer, appending the results to the given string builder.

public static boolean

Returns:

true if the entire string was read, false if more data is needed
readAsciiLine
(final ByteBuffer
the source buffer
src
,
final StringBuilder
the destination builder
builder
,
final char
the replacement character for invalid bytes
replacement
,
final char
the character which marks the end of the line
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 of EOL.

public static boolean

Returns:

true if the entire string was read, false if more data is needed
readAsciiZ
(final ByteBuffer
the source buffer
src
,
final StringBuilder
the destination builder
builder
)

Read an ASCIIZ (NUL-terminated) string from a byte buffer, appending the results to the given string builder.

public static boolean

Returns:

true if the entire string was read, false if more data is needed
readAsciiZ
(final ByteBuffer
the source buffer
src
,
final StringBuilder
the destination builder
builder
,
final char
the replacement character for invalid bytes
replacement
)

Read an ASCIIZ (NUL-terminated) string from a byte buffer, appending the results to the given string builder.

public static void
readLatin1(final ByteBuffer
the source buffer
src
,
final StringBuilder
the destination builder
builder
)

Read the remainder of a buffer as Latin-1 text, appending the results to the given string builder.

public static boolean

Returns:

true if the entire string was read, false if more data is needed
readLatin1Line
(final ByteBuffer
the source buffer
src
,
final StringBuilder
the destination builder
builder
)

Read a single line of Latin-1 text from a byte buffer, appending the results to the given string builder.

public static boolean

Returns:

true if the entire string was read, false if more data is needed
readLatin1Line
(final ByteBuffer
the source buffer
src
,
final StringBuilder
the destination builder
builder
,
final char
the character which marks the end of the line
delimiter
)

Read a single line of Latin-1 text from a byte buffer, appending the results to the given string builder.

public static boolean

Returns:

true if the entire string was read, false if more data is needed
readLatin1Z
(final ByteBuffer
the source buffer
src
,
final StringBuilder
the destination builder
builder
)

Read a NUL-terminated Latin-1 string from a byte buffer, appending the results to the given string builder.

public static boolean

Returns:

true if the entire string was read, false if more data is needed
readLine
(final ByteBuffer
the source buffer
src
,
final StringBuilder
the destination builder
builder
,
final CharsetDecoder
the decoder to use
decoder
)

Read a single line of text from a byte buffer, appending the results to the given string builder.

public static boolean

Returns:

true if the entire string was read, false if more data is needed
readLine
(final ByteBuffer
the source buffer
src
,
final StringBuilder
the destination builder
builder
,
final CharsetDecoder
the decoder to use
decoder
,
final char
the character which marks the end of the line
delimiter
)

Read a single line of text from a byte buffer, appending the results to the given string builder.

public static boolean

Returns:

true if the entire string was read, false if more data is needed
readModifiedUtf8Line
(final ByteBuffer
the source buffer
src
,
final StringBuilder
the destination builder
builder
)

Read a single line of modified UTF-8 text from a byte buffer, appending the results to the given string builder.

public static boolean

Returns:

true if the entire string was read, false if more data is needed
readModifiedUtf8Line
(final ByteBuffer
the source buffer
src
,
final StringBuilder
the destination builder
builder
,
final char
the replacement character for invalid bytes
replacement
)

Read a single line of modified UTF-8 text from a byte buffer, appending the results to the given string builder.

public static boolean

Returns:

true if the entire string was read, false if more data is needed
readModifiedUtf8Line
(final ByteBuffer
the source buffer
src
,
final StringBuilder
the destination builder
builder
,
final char
the replacement character for invalid bytes
replacement
,
final char
the character which marks the end of the line
delimiter
)

Read a single line of modified UTF-8 text from a byte buffer, appending the results to the given string builder.

public static boolean

Returns:

true if the entire string was read, false if more data is needed
readModifiedUtf8Z
(final ByteBuffer
the source buffer
src
,
final StringBuilder
the destination builder
builder
)

Read a NUL-terminated modified UTF-8 string from a byte buffer, appending the results to the given string builder.

public static boolean

Returns:

true if the entire string was read, false if more data is needed
readModifiedUtf8Z
(final ByteBuffer
the source buffer
src
,
final StringBuilder
the destination builder
builder
,
final char
the replacement character to use
replacement
)

Read a NUL-terminated modified UTF-8 string from a byte buffer, appending the results to the given string builder.

private static int
public static long

Returns:

the number of remaining elements
remaining
(final Buffer[]
the buffers
buffers
,
final int
the offset into the buffers array
offs
,
final int
the number of buffers to check
len
)

Get the total remaining size of all the given buffers.

public static long

Returns:

the number of remaining elements
remaining
(final Buffer[]
the buffers
buffers
)

Get the total remaining size of all the given buffers.

public static <
the buffer type
T extends Buffer
>
T

Returns:

the buffer instance
reset
(T
the buffer to reset
buffer
)

Reset the buffer.

public static <
the buffer type
T extends Buffer
>
T

Returns:

the buffer instance
rewind
(T
the buffer to rewind
buffer
)

Rewind the buffer.

public static Pool<ByteBuffer>

Returns:

the wrapper pool
secureBufferPool
(final Pool<ByteBuffer>
the delegate pool
delegate
)
References Deprecated Pool is deprecated or references (maybe indirectly) at least one deprecated element.

A byte buffer pool which zeroes the content of the buffer before re-pooling it.

public static <
the buffer type
T extends Buffer
>
T

Returns:

the buffer instance
skip
(T
the buffer to set
buffer
,
int
the distance to skip
cnt
)

Advance a buffer's position relative to its current position.

public static ByteBuffer

Returns:

the buffer slice
slice
(ByteBuffer
the buffer to slice
buffer
,
int
the size of the slice
sliceSize
)

Slice the buffer.

public static CharBuffer

Returns:

the buffer slice
slice
(CharBuffer
the buffer to slice
buffer
,
int
the size of the slice
sliceSize
)

Slice the buffer.

public static ShortBuffer

Returns:

the buffer slice
slice
(ShortBuffer
the buffer to slice
buffer
,
int
the size of the slice
sliceSize
)

Slice the buffer.

public static IntBuffer

Returns:

the buffer slice
slice
(IntBuffer
the buffer to slice
buffer
,
int
the size of the slice
sliceSize
)

Slice the buffer.

public static LongBuffer

Returns:

the buffer slice
slice
(LongBuffer
the buffer to slice
buffer
,
int
the size of the slice
sliceSize
)

Slice the buffer.

public static BufferAllocator<ByteBuffer>

Returns:

the slice allocator
sliceAllocator
(final ByteBuffer
the source buffer
buffer
)

A buffer allocator which allocates slices off of the given buffer.

public static byte[]

Returns:

the bytes
take
(ByteBuffer
the buffer to read
buffer
,
int
the number of bytes to take
cnt
)

Take a certain number of bytes from the buffer and return them in an array.

public static char[]

Returns:

the chars
take
(CharBuffer
the buffer to read
buffer
,
int
the number of chars to take
cnt
)

Take a certain number of chars from the buffer and return them in an array.

public static short[]

Returns:

the shorts
take
(ShortBuffer
the buffer to read
buffer
,
int
the number of shorts to take
cnt
)

Take a certain number of shorts from the buffer and return them in an array.

public static int[]

Returns:

the ints
take
(IntBuffer
the buffer to read
buffer
,
int
the number of ints to take
cnt
)

Take a certain number of ints from the buffer and return them in an array.

public static long[]

Returns:

the longs
take
(LongBuffer
the buffer to read
buffer
,
int
the number of longs to take
cnt
)

Take a certain number of longs from the buffer and return them in an array.

public static byte[]

Returns:

the bytes
take
(ByteBuffer
the buffer to read
buffer
)

Take all of the remaining bytes from the buffer and return them in an array.

public static byte[]

Returns:

the bytes
take
(final ByteBuffer[]
the buffer to read
buffers
,
final int
the offset into the array
offs
,
final int
the number of buffers
len
)

Take all of the remaining bytes from the buffers and return them in an array.

public static char[]

Returns:

the chars
take
(CharBuffer
the buffer to read
buffer
)

Take all of the remaining chars from the buffer and return them in an array.

public static short[]

Returns:

the shorts
take
(ShortBuffer
the buffer to read
buffer
)

Take all of the remaining shorts from the buffer and return them in an array.

public static int[]

Returns:

the ints
take
(IntBuffer
the buffer to read
buffer
)

Take all of the remaining ints from the buffer and return them in an array.

public static long[]

Returns:

the longs
take
(LongBuffer
the buffer to read
buffer
)

Take all of the remaining longs from the buffer and return them in an array.

public static int

Returns:

the actual number of bytes skipped
trySkip
(Buffer
the buffer to set
buffer
,
int
the distance to skip
cnt
)

Attempt to advance a buffer's position relative to its current position.

public static long

Returns:

the actual number of bytes skipped
trySkip
(Buffer[]
the buffers to set
buffers
,
int
the offset into the buffers array
offs
,
int
the number of buffers to consider
len
,
long
the distance to skip
cnt
)

Attempt to advance a series of buffers' overall position relative to its current position.

public static <
the buffer type
T extends Buffer
>
T

Returns:

the buffer instance
unget
(T
the buffer to set
buffer
,
int
the distance to skip backwards
cnt
)

Rewind a buffer's position relative to its current position.

public static void
zero(ByteBuffer
the buffer
buffer
)

Zero a buffer.

public static void
zero(CharBuffer
the buffer
buffer
)

Zero a buffer.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait