Sea 0.2.2

gov.lbl.dsd.sea.nio.util
Class BufferUtil

java.lang.Object
  extended bygov.lbl.dsd.sea.nio.util.BufferUtil

public class BufferUtil
extends Object

Various utilities related to the ByteBuffer buffers; in particular String conversions using a Charset.

Version:
$Revision: 1.5 $, $Date: 2004/06/01 20:47:20 $

Method Summary
static ByteBuffer concat(ByteBuffer[] buffers)
          Creates and returns a new buffer containing the remaining bytes in the given buffers; Leaves the input buffers unmodified.
static ByteBuffer copy(ByteBuffer src)
          Creates a new buffer that is a deep copy of the remaining bytes in the given buffer (between index buf.position() and buf.limit()); leaves the src buffer unmodified.
static void copy(ByteBuffer src, int srcPos, ByteBuffer dest, int destPos, int length)
          The buffer equivalent of System.arraycopy(java.lang.Object, int, java.lang.Object, int, int); copies src[srcPos]..src[srcPos+length-1] to dest[destPos]..dest[destPos+length-1], leaving the mark, position and limit of both src and dest unmodified; Behaves as expected even if src==dest.
static ByteBuffer ensureCapacity(ByteBuffer buffer, int minCapacity)
          Ensures that a given buffer can hold up to minCapacity bytes.
static String getString(ByteBuffer buffer, Charset charset)
          Returns a string holding a decoded representation of the remaining bytes in the given buffer (relative bulk operation).
static boolean hasMark(Buffer buffer)
          Returns whether or not a given buffer has the mark defined.
static void putString(ByteBuffer buffer, String str, Charset charset)
          Fills the bytes of the encoded string into the given buffer (relative bulk operation).
static byte[] toByteArray(ByteBuffer src)
          Creates and returns a byte array filled with the remaining bytes of given buffer (between index buf.position() and buf.limit()); leaves the src buffer unmodified.
static byte[] toByteArray(String str, Charset charset)
          Returns a byte array holding the encoded bytes of the given string.
static ByteBuffer toByteBuffer(String str, Charset charset)
          Returns a bytebuffer holding the encoded bytes of the given string.
static String toString(byte[] bytes, Charset charset)
          Returns a decoded string representation of the given bytes.
static String toString(byte[] bytes, int offset, int length, Charset charset)
          Returns a decoded string representation of the given bytes.
static String toString(ByteBuffer buffer, Charset charset)
          Returns a decoded string representation of the bytes in the given buffer; leaves the buffer unmodified.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

ensureCapacity

public static ByteBuffer ensureCapacity(ByteBuffer buffer,
                                        int minCapacity)
Ensures that a given buffer can hold up to minCapacity bytes. Returns the identical buffer if it can hold at least the number of bytes specified. Otherwise, returns a new buffer with increased capacity containing the same bytes, ensuring that it can hold at least the number of elements specified by the minimum capacity argument. The new buffer will have a limit equal to the new capacity. Leaves the position and byte order unmodified.

Parameters:
minCapacity - the desired minimum capacity.

concat

public static ByteBuffer concat(ByteBuffer[] buffers)
Creates and returns a new buffer containing the remaining bytes in the given buffers; Leaves the input buffers unmodified.

Parameters:
buffers - - the buffers to concatenate
Returns:
the new buffer, ready to read from

copy

public static void copy(ByteBuffer src,
                        int srcPos,
                        ByteBuffer dest,
                        int destPos,
                        int length)
The buffer equivalent of System.arraycopy(java.lang.Object, int, java.lang.Object, int, int); copies src[srcPos]..src[srcPos+length-1] to dest[destPos]..dest[destPos+length-1], leaving the mark, position and limit of both src and dest unmodified; Behaves as expected even if src==dest.

Parameters:
src - the buffer to read from
srcPos - the index of the first byte to read
dest - the buffer to write to
destPos - the index of the first byte to write
length - the number of bytes to copy

copy

public static ByteBuffer copy(ByteBuffer src)
Creates a new buffer that is a deep copy of the remaining bytes in the given buffer (between index buf.position() and buf.limit()); leaves the src buffer unmodified. High performance implementation.

Parameters:
src - the buffer to copy
Returns:
the new buffer, ready to read from

getString

public static String getString(ByteBuffer buffer,
                               Charset charset)
Returns a string holding a decoded representation of the remaining bytes in the given buffer (relative bulk operation).

Parameters:
buffer - the buffer to convert.
charset - the requested charset to convert with (e.g. Charset.forName("US-ASCII"), Charset.forName("UTF-8"))
Returns:
the string

hasMark

public static boolean hasMark(Buffer buffer)
Returns whether or not a given buffer has the mark defined.

Parameters:
buffer - the buffer to check
Returns:
true if it has a mark

putString

public static void putString(ByteBuffer buffer,
                             String str,
                             Charset charset)
Fills the bytes of the encoded string into the given buffer (relative bulk operation).

Parameters:
buffer - the buffer to fill into.
str - the string to convert.
charset - the requested charset to convert with (e.g. Charset.forName("US-ASCII"), Charset.forName("UTF-8"))

toByteArray

public static byte[] toByteArray(ByteBuffer src)
Creates and returns a byte array filled with the remaining bytes of given buffer (between index buf.position() and buf.limit()); leaves the src buffer unmodified. High performance implementation.

Parameters:
src - the bytebuffer to read from
Returns:
the byte array

toByteBuffer

public static ByteBuffer toByteBuffer(String str,
                                      Charset charset)
Returns a bytebuffer holding the encoded bytes of the given string.

Parameters:
str - the string to convert.
charset - the requested charset to convert with (e.g. Charset.forName("US-ASCII"), Charset.forName("UTF-8"))
Returns:
the byte buffer

toByteArray

public static byte[] toByteArray(String str,
                                 Charset charset)
Returns a byte array holding the encoded bytes of the given string.

Parameters:
str - the string to convert.
charset - the requested charset to convert with (e.g. Charset.forName("US-ASCII"), Charset.forName("UTF-8"))
Returns:
the byte array

toString

public static String toString(ByteBuffer buffer,
                              Charset charset)
Returns a decoded string representation of the bytes in the given buffer; leaves the buffer unmodified.

Parameters:
buffer - the buffer to convert
charset - the requested charset to convert with (e.g. Charset.forName("US-ASCII"), Charset.forName("UTF-8"))
Returns:
the string representation

toString

public static String toString(byte[] bytes,
                              int offset,
                              int length,
                              Charset charset)
Returns a decoded string representation of the given bytes.

Parameters:
bytes - the bytes to convert
offset - The offset of the subarray to be used; must be non-negative and no larger than bytes.
length - The length of the subarray to be used; must be non-negative and no larger than bytes.length - offset.
charset - the requested charset to convert with (e.g. Charset.forName("US-ASCII"), Charset.forName("UTF-8"))
Returns:
the string representation

toString

public static String toString(byte[] bytes,
                              Charset charset)
Returns a decoded string representation of the given bytes.

Parameters:
bytes - the bytes to convert
charset - the requested charset to convert with (e.g. Charset.forName("US-ASCII"), Charset.forName("UTF-8"))
Returns:
the string representation

Sea 0.2.2