Module 
Package org.xnio

Class Buffers

java.lang.Object
org.xnio.Buffers

public final class Buffers extends Object
Buffer utility methods.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final ByteBuffer
    The empty byte buffer.
    static final Pooled<ByteBuffer>
    The empty pooled byte buffer.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    Add a random amount of random data to the target buffer using the thread-local RNG.
    static void
    addRandom(ByteBuffer target, int count)
    Add count 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)
    Add count bytes of random data to the target buffer.
    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.
    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>
    T
    clear(T buffer)
    Clear a buffer.
    static int
    copy(int count, ByteBuffer[] destinations, int offset, int length, ByteBuffer source)
    Copy at most count bytes from sources into destinations in a "scatter" fashion.
    static int
    copy(int count, ByteBuffer destination, ByteBuffer source)
    Copy at most count bytes from source into destination.
    static int
    copy(int count, ByteBuffer destination, ByteBuffer[] sources, int offset, int length)
    Copy at most count bytes from sources into destination in a "gather" fashion.
    static long
    copy(long count, ByteBuffer[] destinations, int destOffset, int destLength, ByteBuffer[] sources, int srcOffset, int srcLength)
    Copy at most count bytes from sources into destinations by a combined "scatter"/"gather" operation.
    static int
    copy(ByteBuffer[] destinations, int offset, int length, ByteBuffer source)
    Copy as many bytes as possible from sources into destinations 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 from sources into destinations 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 from source into destination.
    static int
    copy(ByteBuffer destination, ByteBuffer[] sources, int offset, int length)
    Copy as many bytes as possible from sources into destination 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 its toString() 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 its toString() method is called.
    static String
    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.
    Create a "pooled" empty buffer.
    static void
    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(IntBuffer buffer, int value, int count)
    Fill a buffer with a repeated value.
    static LongBuffer
    fill(LongBuffer buffer, long value, int count)
    Fill a buffer with a repeated value.
    fill(ShortBuffer buffer, int value, int count)
    Fill a buffer with a repeated value.
    static int
    Fill a buffer from an input stream.
    static <T extends Buffer>
    T
    flip(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.
    Create a pooled wrapper around a buffer that was allocated via ByteBufferPool.
    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
    isDirect(Buffer... buffers)
    Determine whether the given buffers list is comprised solely of direct buffers or solely of heap buffers.
    static boolean
    isDirect(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
    Determine whether the given pool is a secure pool.
    static <T extends Buffer>
    T
    limit(T buffer, int limit)
    Set the buffer limit.
    static <T extends Buffer>
    T
    mark(T buffer)
    Set the buffer mark.
    static <B extends Buffer>
    Pooled<B>
    pooledWrapper(B buffer)
    Create a pooled wrapper around a buffer.
    static <T extends Buffer>
    T
    position(T buffer, int position)
    Set the buffer position.
    static ByteBuffer
    Put the string into the byte buffer, encoding it using "modified UTF-8" encoding.
    static void
    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
    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 of EOL.
    static boolean
    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
    Read the remainder of a buffer as Latin-1 text, appending the results to the given string builder.
    static boolean
    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
    Read a NUL-terminated Latin-1 string from a byte buffer, appending the results to the given string builder.
    static boolean
    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
    Read a single line of modified 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 of modified 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 of modified UTF-8 text from a byte buffer, appending the results to the given string builder.
    static boolean
    Read a NUL-terminated modified 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 a NUL-terminated modified UTF-8 string from a byte buffer, appending the results to the given string builder.
    static long
    remaining(Buffer[] buffers)
    Get the total remaining size of all the given buffers.
    static long
    remaining(Buffer[] buffers, int offs, int len)
    Get the total remaining size of all the given buffers.
    static <T extends Buffer>
    T
    reset(T buffer)
    Reset the buffer.
    static <T extends Buffer>
    T
    rewind(T buffer)
    Rewind the buffer.
    A byte buffer pool which zeroes the content of the buffer before re-pooling it.
    static <T extends Buffer>
    T
    skip(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(IntBuffer buffer, int sliceSize)
    Slice the buffer.
    static LongBuffer
    slice(LongBuffer buffer, int sliceSize)
    Slice the buffer.
    slice(ShortBuffer buffer, int sliceSize)
    Slice the 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(IntBuffer buffer)
    Take all of the remaining ints from the buffer and return them in an array.
    static int[]
    take(IntBuffer buffer, int cnt)
    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
    trySkip(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(Buffer buffer, int cnt)
    Attempt to advance a buffer's position relative to its current position.
    static <T extends Buffer>
    T
    unget(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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • EMPTY_BYTE_BUFFER

      public static final ByteBuffer EMPTY_BYTE_BUFFER
      The empty byte buffer.
    • EMPTY_POOLED_BYTE_BUFFER

      public static final Pooled<ByteBuffer> EMPTY_POOLED_BYTE_BUFFER
      The empty pooled byte buffer. Freeing or discarding this buffer has no effect.
  • Method Details

    • flip

      public static <T extends 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:
    • clear

      public static <T extends 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:
    • limit

      public static <T extends Buffer> T limit(T buffer, int limit)
      Set the buffer limit.
      Type Parameters:
      T - the buffer type
      Parameters:
      buffer - the buffer to set
      limit - the new limit
      Returns:
      the buffer instance
      See Also:
    • mark

      public static <T extends 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:
    • position

      public static <T extends Buffer> T position(T buffer, int position)
      Set the buffer position.
      Type Parameters:
      T - the buffer type
      Parameters:
      buffer - the buffer to set
      position - the new position
      Returns:
      the buffer instance
      See Also:
    • reset

      public static <T extends 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:
    • rewind

      public static <T extends 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:
    • slice

      public static ByteBuffer slice(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 slice
      sliceSize - the size of the slice
      Returns:
      the buffer slice
      See Also:
    • copy

      public static ByteBuffer copy(ByteBuffer buffer, int count, BufferAllocator<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 slice
      count - the size of the copy
      allocator - the buffer allocator to use
      Returns:
      the buffer slice
    • copy

      public static int copy(ByteBuffer destination, ByteBuffer source)
      Copy as many bytes as possible from source into destination.
      Parameters:
      destination - the destination buffer
      source - the source buffer
      Returns:
      the number of bytes put into the destination buffer
    • copy

      public static int copy(ByteBuffer[] destinations, int offset, int length, ByteBuffer source)
      Copy as many bytes as possible from sources into destinations in a "scatter" fashion.
      Parameters:
      destinations - the destination buffers
      offset - the offset into the destination buffers array
      length - the number of buffers to update
      source - the source buffer
      Returns:
      the number of bytes put into the destination buffers
    • copy

      public static int copy(ByteBuffer destination, ByteBuffer[] sources, int offset, int length)
      Copy as many bytes as possible from sources into destination in a "gather" fashion.
      Parameters:
      destination - the destination buffer
      sources - the source buffers
      offset - the offset into the source buffers array
      length - 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 from sources into destinations by a combined "scatter"/"gather" operation.
      Parameters:
      destinations - the destination buffers
      destOffset - the offset into the destination buffers array
      destLength - the number of buffers to write to
      sources - the source buffers
      srcOffset - the offset into the source buffers array
      srcLength - the number of buffers to read from
      Returns:
      the number of bytes put into the destination buffers
    • copy

      public static int copy(int count, ByteBuffer destination, ByteBuffer source)
      Copy at most count bytes from source into destination.
      Parameters:
      count - the maximum number of bytes to copy
      destination - the destination buffer
      source - 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 most count bytes from sources into destinations in a "scatter" fashion.
      Parameters:
      count - the maximum number of bytes to copy
      destinations - the destination buffers
      offset - the offset into the destination buffers array
      length - the number of buffers to update
      source - 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 most count bytes from sources into destination in a "gather" fashion.
      Parameters:
      count - the maximum number of bytes to copy
      destination - the destination buffer
      sources - the source buffers
      offset - the offset into the source buffers array
      length - 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 most count bytes from sources into destinations by a combined "scatter"/"gather" operation.
      Parameters:
      count - the maximum number of bytes to copy
      destinations - the destination buffers
      destOffset - the offset into the destination buffers array
      destLength - the number of buffers to write to
      sources - the source buffers
      srcOffset - the offset into the source buffers array
      srcLength - the number of buffers to read from
      Returns:
      the number of bytes put into the destination buffers
    • fill

      public static ByteBuffer fill(ByteBuffer buffer, int value, int count)
      Fill a buffer with a repeated value.
      Parameters:
      buffer - the buffer to fill
      value - the value to fill
      count - the number of bytes to fill
      Returns:
      the buffer instance
    • slice

      public static CharBuffer slice(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 slice
      sliceSize - the size of the slice
      Returns:
      the buffer slice
      See Also:
    • fill

      public static CharBuffer fill(CharBuffer buffer, int value, int count)
      Fill a buffer with a repeated value.
      Parameters:
      buffer - the buffer to fill
      value - the value to fill
      count - the number of chars to fill
      Returns:
      the buffer instance
    • slice

      public static ShortBuffer slice(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 slice
      sliceSize - the size of the slice
      Returns:
      the buffer slice
      See Also:
    • fill

      public static ShortBuffer fill(ShortBuffer buffer, int value, int count)
      Fill a buffer with a repeated value.
      Parameters:
      buffer - the buffer to fill
      value - the value to fill
      count - the number of shorts to fill
      Returns:
      the buffer instance
    • slice

      public static IntBuffer slice(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 slice
      sliceSize - the size of the slice
      Returns:
      the buffer slice
      See Also:
    • fill

      public static IntBuffer fill(IntBuffer buffer, int value, int count)
      Fill a buffer with a repeated value.
      Parameters:
      buffer - the buffer to fill
      value - the value to fill
      count - the number of ints to fill
      Returns:
      the buffer instance
    • slice

      public static LongBuffer slice(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 slice
      sliceSize - the size of the slice
      Returns:
      the buffer slice
      See Also:
    • fill

      public static LongBuffer fill(LongBuffer buffer, long value, int count)
      Fill a buffer with a repeated value.
      Parameters:
      buffer - the buffer to fill
      value - the value to fill
      count - the number of longs to fill
      Returns:
      the buffer instance
    • skip

      public static <T extends Buffer> T skip(T buffer, int cnt) throws BufferUnderflowException
      Advance a buffer's position relative to its current position.
      Type Parameters:
      T - the buffer type
      Parameters:
      buffer - the buffer to set
      cnt - the distance to skip
      Returns:
      the buffer instance
      Throws:
      BufferUnderflowException - if there are fewer than cnt bytes remaining
      See Also:
    • trySkip

      public static int trySkip(Buffer buffer, int cnt)
      Attempt to advance a buffer's position relative to its current position.
      Parameters:
      buffer - the buffer to set
      cnt - the distance to skip
      Returns:
      the actual number of bytes skipped
      See Also:
    • trySkip

      public static long trySkip(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 set
      offs - the offset into the buffers array
      len - the number of buffers to consider
      cnt - the distance to skip
      Returns:
      the actual number of bytes skipped
      See Also:
    • unget

      public static <T extends 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 set
      cnt - the distance to skip backwards
      Returns:
      the buffer instance
      See Also:
    • take

      public static byte[] take(ByteBuffer buffer, int cnt)
      Take a certain number of bytes from the buffer and return them in an array.
      Parameters:
      buffer - the buffer to read
      cnt - the number of bytes to take
      Returns:
      the bytes
    • take

      public static char[] take(CharBuffer buffer, int cnt)
      Take a certain number of chars from the buffer and return them in an array.
      Parameters:
      buffer - the buffer to read
      cnt - the number of chars to take
      Returns:
      the chars
    • take

      public static short[] take(ShortBuffer buffer, int cnt)
      Take a certain number of shorts from the buffer and return them in an array.
      Parameters:
      buffer - the buffer to read
      cnt - the number of shorts to take
      Returns:
      the shorts
    • take

      public static int[] take(IntBuffer buffer, int cnt)
      Take a certain number of ints from the buffer and return them in an array.
      Parameters:
      buffer - the buffer to read
      cnt - the number of ints to take
      Returns:
      the ints
    • take

      public static long[] take(LongBuffer buffer, int cnt)
      Take a certain number of longs from the buffer and return them in an array.
      Parameters:
      buffer - the buffer to read
      cnt - the number of longs to take
      Returns:
      the longs
    • take

      public static byte[] take(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(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 read
      offs - the offset into the array
      len - the number of buffers
      Returns:
      the bytes
    • take

      public static char[] take(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(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(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(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 Object createDumper(ByteBuffer buffer, int indent, int columns)
      Create an object that returns the dumped form of the given byte buffer when its toString() method is called. Useful for logging byte buffers; if the toString() method is never called, the process of dumping the buffer is never performed.
      Parameters:
      buffer - the buffer
      indent - the indentation to use
      columns - 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 buffer
      dest - the target
      indent - the indentation to use
      columns - the number of 8-byte columns
      Throws:
      IOException - if an error occurs during append
    • createDumper

      public static Object createDumper(CharBuffer buffer, int indent, int columns)
      Create an object that returns the dumped form of the given character buffer when its toString() method is called. Useful for logging character buffers; if the toString() method is never called, the process of dumping the buffer is never performed.
      Parameters:
      buffer - the buffer
      indent - the indentation to use
      columns - 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 buffer
      dest - the target
      indent - the indentation to use
      columns - the number of 8-byte columns
      Throws:
      IOException - if an error occurs during append
    • hasRemaining

      public static boolean hasRemaining(Buffer[] buffers, int offs, int len)
      Determine whether any of the buffers has remaining data.
      Parameters:
      buffers - the buffers
      offs - the offset into the buffers array
      len - the number of buffers to check
      Returns:
      true if any of the selected buffers has remaining data
    • hasRemaining

      public static boolean hasRemaining(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(Buffer[] buffers, int offs, int len)
      Get the total remaining size of all the given buffers.
      Parameters:
      buffers - the buffers
      offs - the offset into the buffers array
      len - the number of buffers to check
      Returns:
      the number of remaining elements
    • remaining

      public static long remaining(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 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 buffer
      orig - 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

      public static String getModifiedUtf8Z(ByteBuffer src) throws 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:
      BufferUnderflowException - if the end of the buffer was reached before encountering a 0
    • getModifiedUtf8

      public static String getModifiedUtf8(ByteBuffer src) throws BufferUnderflowException
      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

      public 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. If no NUL 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 buffer
      builder - the destination builder
      Returns:
      true if the entire string was read, false if more data is needed
    • readAsciiZ

      public 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. If no NUL 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 by replacement is written to the string builder in its place.
      Parameters:
      src - the source buffer
      builder - the destination builder
      replacement - 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)
      Read a single line of ASCII text from a byte buffer, appending the results to the given string builder. If no EOL 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. The EOL character will be included in the resultant string.
      Parameters:
      src - the source buffer
      builder - the destination builder
      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)
      Read a single line of ASCII text from a byte buffer, appending the results to the given string builder. If no EOL 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 by replacement is written to the string builder in its place. The EOL character will be included in the resultant string.
      Parameters:
      src - the source buffer
      builder - the destination builder
      replacement - 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 of EOL. 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 by replacement is written to the string builder in its place. The delimiter character will be included in the resultant string.
      Parameters:
      src - the source buffer
      builder - the destination builder
      replacement - the replacement character for invalid bytes
      delimiter - 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(ByteBuffer src, 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 buffer
      builder - the destination builder
    • readAscii

      public 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. If an invalid byte is read, the character designated by replacement is written to the string builder in its place.
      Parameters:
      src - the source buffer
      builder - the destination builder
      replacement - the replacement character for invalid bytes
    • readAscii

      public 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. If an invalid byte is read, the character designated by replacement is written to the string builder in its place.
      Parameters:
      src - the source buffer
      builder - the destination builder
      limit - the maximum number of characters to write
      replacement - the replacement character for invalid bytes
    • readLatin1Z

      public static boolean readLatin1Z(ByteBuffer src, StringBuilder builder)
      Read a NUL-terminated Latin-1 string from a byte buffer, appending the results to the given string builder. If no NUL 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 buffer
      builder - the destination builder
      Returns:
      true if the entire string was read, false if more data is needed
    • readLatin1Line

      public 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. If no EOL 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 EOL character will be included in the resultant string.
      Parameters:
      src - the source buffer
      builder - the destination builder
      Returns:
      true if the entire string was read, false if more data is needed
    • readLatin1Line

      public 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. 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 buffer
      builder - the destination builder
      delimiter - 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(ByteBuffer src, StringBuilder builder)
      Read the remainder of a buffer as Latin-1 text, appending the results to the given string builder.
      Parameters:
      src - the source buffer
      builder - the destination builder
    • readModifiedUtf8Z

      public static boolean readModifiedUtf8Z(ByteBuffer src, StringBuilder builder)
      Read a NUL-terminated modified UTF-8 string from a byte buffer, appending the results to the given string builder. If no NUL 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 buffer
      builder - the destination builder
      Returns:
      true if the entire string was read, false if more data is needed
    • readModifiedUtf8Z

      public static boolean readModifiedUtf8Z(ByteBuffer src, StringBuilder builder, char replacement)
      Read a NUL-terminated modified UTF-8 string from a byte buffer, appending the results to the given string builder. If no NUL 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 by replacement is written to the string builder in its place.
      Parameters:
      src - the source buffer
      builder - the destination builder
      replacement - the replacement character to use
      Returns:
      true if the entire string was read, false if more data is needed
    • readModifiedUtf8Line

      public static boolean readModifiedUtf8Line(ByteBuffer src, StringBuilder builder)
      Read a single line of modified UTF-8 text from a byte buffer, appending the results to the given string builder. If no EOL 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. The EOL character will be included in the resultant string.
      Parameters:
      src - the source buffer
      builder - the destination builder
      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)
      Read a single line of modified UTF-8 text from a byte buffer, appending the results to the given string builder. If no EOL 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 by replacement is written to the string builder in its place. The EOL character will be included in the resultant string.
      Parameters:
      src - the source buffer
      builder - the destination builder
      replacement - 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 of modified UTF-8 text from a byte buffer, appending the results to the given string builder. If no EOL 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 by replacement is written to the string builder in its place. The delimiter character will be included in the resultant string.
      Parameters:
      src - the source buffer
      builder - the destination builder
      replacement - the replacement character for invalid bytes
      delimiter - 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(ByteBuffer src, StringBuilder builder, CharsetDecoder decoder)
      Read a single line of text from a byte buffer, appending the results to the given string builder. If no EOL 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 the decoder 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 EOL character will be included in the resultant string.
      Parameters:
      src - the source buffer
      builder - the destination builder
      decoder - 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 the decoder 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 buffer
      builder - the destination builder
      decoder - the decoder to use
      delimiter - 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 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<ByteBuffer> globalPooledWrapper(ByteBuffer buffer)
      Create a pooled wrapper around a buffer that was allocated via ByteBufferPool. The buffer is freed to the global pool when freed.
      Parameters:
      buffer - the buffer to wrap
      Returns:
      the pooled wrapper
    • emptyPooledByteBuffer

      public static Pooled<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<ByteBuffer> sliceAllocator(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 in BufferUnderflowException.
      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 allocator
      size - the buffer size
      Returns:
      the buffer pool
    • secureBufferPool

      public static Pool<ByteBuffer> secureBufferPool(Pool<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(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(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(Buffer... buffers) throws 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:
      IllegalArgumentException - if both direct and heap buffers were found, or if a buffer is null
    • isDirect

      public static boolean isDirect(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:
      IllegalArgumentException - if both direct and heap buffers were found, or if a buffer is null
    • assertWritable

      public static void assertWritable(Buffer[] buffers, int offs, int len) throws ReadOnlyBufferException
      Assert the writability of the given buffers.
      Parameters:
      buffers - the buffers array
      offs - the offset in the array to start searching
      len - the number of buffers to check
      Throws:
      ReadOnlyBufferException - if any of the buffers are read-only
    • assertWritable

      public static void assertWritable(Buffer... buffers) throws ReadOnlyBufferException
      Assert the writability of the given buffers.
      Parameters:
      buffers - the buffers array
      Throws:
      ReadOnlyBufferException - if any of the buffers are read-only
    • addRandom

      public static void addRandom(ByteBuffer target, Random random, int count)
      Add count bytes of random data to the target buffer.
      Parameters:
      target - the target buffer
      random - the RNG
      count - the number of bytes to add
    • addRandom

      public static void addRandom(ByteBuffer target, int count)
      Add count bytes of random data to the target buffer using the thread-local RNG.
      Parameters:
      target - the target buffer
      count - the number of bytes to add
    • addRandom

      public static void addRandom(ByteBuffer target, Random random)
      Add a random amount of random data to the target buffer.
      Parameters:
      target - the target buffer
      random - the RNG
    • addRandom

      public static void addRandom(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(ByteBuffer target, InputStream source) throws 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 buffer
      source - 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

      public static String debugString(ByteBuffer buffer)
      Get a debug-friendly description of the buffer.
      Parameters:
      buffer - the buffer to describe
      Returns:
      the string
    • debugString

      public static String debugString(ByteBuffer[] buffers, int offs, int len)
      Get a debug-friendly description of the buffer.
      Parameters:
      buffers - the buffers to describe
      offs - the offset into the array
      len - the number of buffers
      Returns:
      the string
    • emptyToStream

      public static void emptyToStream(OutputStream target, ByteBuffer source) throws 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 stream
      source - the source buffer
      Throws:
      IOException - if the stream write fails