XNIO API 2.0.0.GA

org.jboss.xnio
Class Buffers

java.lang.Object
  extended by org.jboss.xnio.Buffers

public final class Buffers
extends java.lang.Object

Buffer utility methods.


Field Summary
static java.nio.ByteBuffer EMPTY_BYTE_BUFFER
          The empty byte buffer.
 
Method Summary
static
<T extends java.nio.Buffer>
T
clear(T buffer)
          Clear a buffer.
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 its toString() 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 its toString() method is called.
static Pool<java.nio.ByteBuffer> createHeapByteBufferAllocator(int size)
          Create a heap-based buffer allocator.
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 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
<T extends java.nio.Buffer>
T
flip(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 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
<T extends java.nio.Buffer>
T
limit(T buffer, int limit)
          Set the buffer limit.
static
<T extends java.nio.Buffer>
T
mark(T buffer)
          Set the buffer mark.
static
<T extends java.nio.Buffer>
T
position(T buffer, int position)
          Set the buffer position.
static long put(java.nio.ByteBuffer[] dsts, int doffs, int dlen, java.nio.ByteBuffer src)
          Put as many bytes as possible from src into the byte buffers in a scatter fashion.
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 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>
T
reset(T buffer)
          Reset the buffer.
static
<T extends java.nio.Buffer>
T
rewind(T buffer)
          Rewind the buffer.
static
<T extends java.nio.Buffer>
T
skip(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 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, int cnt)
          Take a certain number of chars 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, int cnt)
          Take a certain number of longs 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
<T extends java.nio.Buffer>
T
unget(T buffer, int cnt)
          Rewind a buffer's position relative to its current position.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_BYTE_BUFFER

public static final java.nio.ByteBuffer EMPTY_BYTE_BUFFER
The empty byte buffer.

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 set
limit - 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 set
position - 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 slice
sliceSize - the size of the slice
Returns:
the buffer slice
See Also:
ByteBuffer.slice()

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 fill
value - the value to fill
count - 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 slice
sliceSize - 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 fill
value - the value to fill
count - 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 slice
sliceSize - 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 fill
value - the value to fill
count - 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 slice
sliceSize - 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 fill
value - the value to fill
count - 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 slice
sliceSize - 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 fill
value - the value to fill
count - the number of longs to fill
Returns:
the buffer instance

skip

public static <T extends java.nio.Buffer> T skip(T buffer,
                                                 int cnt)
Advance a buffer's position relative to its current position.

Type Parameters:
T - the buffer type
Parameters:
buffer - the buffer to set
cnt - the distantce to skip
Returns:
the buffer instance
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 set
cnt - the distantce 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 read
cnt - 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 read
cnt - 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 read
cnt - 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 read
cnt - 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 read
cnt - the number of longs to take
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 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(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 buffer
dest - the target
indent - the indentation to use
columns - 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 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(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 buffer
dest - the target
indent - the indentation to use
columns - the number of 8-byte columns
Throws:
java.io.IOException - if an error occurs during append

createHeapByteBufferAllocator

public static Pool<java.nio.ByteBuffer> createHeapByteBufferAllocator(int size)
Create a heap-based buffer allocator.

Parameters:
size - the size of the returned buffers
Returns:
the buffer allocator
Since:
1.1

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 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(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 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(java.nio.Buffer[] buffers)
Get the total remaining size of all the given buffers.

Parameters:
buffers - the buffers
Returns:
the number of remaining elements

put

public static long put(java.nio.ByteBuffer[] dsts,
                       int doffs,
                       int dlen,
                       java.nio.ByteBuffer src)
Put as many bytes as possible from src into the byte buffers in a scatter fashion.

Parameters:
dsts - the destination buffers
doffs - the offset into the destination buffers array
dlen - the number of buffers to update
src - the source buffer

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 buffer
orig - 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 a 0

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

XNIO API 2.0.0.GA

Copyright © 2009 JBoss, a division of Red Hat, Inc.