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.


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 BufferAllocator<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
<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
<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)
          Set 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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)
Set 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)

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 BufferAllocator<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