org.jboss.messaging.core.buffers
Class ChannelBuffers

java.lang.Object
  extended by org.jboss.messaging.core.buffers.ChannelBuffers

public class ChannelBuffers
extends java.lang.Object

Creates a new ChannelBuffer by allocating new space or by wrapping or copying existing byte arrays, byte buffers and a string.

Use static import

This classes is intended to be used with Java 5 static import statement:
 import static org.jboss.netty.buffer.ChannelBuffers.*;

 ChannelBuffer heapBuffer = buffer(128);
 ChannelBuffer directBuffer = directBuffer(256);
 ChannelBuffer dynamicBuffer = dynamicBuffer(512);
 ChannelBuffer wrappedBuffer = wrappedBuffer(new byte[128], new byte[256]);
 ChannelBuffer copiedBuffer = copiedBuffer(ByteBuffer.allocate(128));
 

Allocating a new buffer

Three buffer types are provided out of the box.

Creating a wrapped buffer

Wrapped buffer is a buffer which is a view of one or more existing byte arrays and byte buffers. Any changes in the content of the original array or buffer will be reflected in the wrapped buffer. Various wrapper methods are provided and their name is all wrappedBuffer(). You might want to take a look at this method closely if you want to create a buffer which is composed of more than one array to reduce the number of memory copy.

Creating a copied buffer

Copied buffer is a deep copy of one or more existing byte arrays, byte buffers or a string. Unlike a wrapped buffer, there's no shared data between the original data and the copied buffer. Various copy methods are provided and their name is all copiedBuffer(). It is also convenient to use this operation to merge multiple buffers into one buffer.

Miscellaneous utility methods

This class also provides various utility methods to help implementation of a new buffer type, generation of hex dump and swapping an integer's byte order.

Version:
$Rev: 472 $, $Date: 2008-11-14 16:45:53 +0900 (Fri, 14 Nov 2008) $
Author:
The Netty Project (netty-dev@lists.jboss.org), Trustin Lee (tlee@redhat.com)

Field Summary
static HeapChannelBuffer EMPTY_BUFFER
          A buffer whose capacity is 0.
 
Method Summary
static ChannelBuffer buffer(int capacity)
          Creates a new Java heap buffer with the specified endianness and capacity.
static int compare(ChannelBuffer bufferA, ChannelBuffer bufferB)
          Compares the two specified buffers as described in ChannelBuffer.compareTo(ChannelBuffer).
static ChannelBuffer copiedBuffer(byte[] array)
          Creates a new buffer with the specified endianness whose content is a copy of the specified array.
static ChannelBuffer copiedBuffer(java.nio.ByteBuffer buffer)
          Creates a new buffer whose content is a copy of the specified buffer's current slice.
static ChannelBuffer dynamicBuffer(byte[] initialBuffer)
          Reuses the initialBuffer on the creation of the DynamicBuffer.
static ChannelBuffer dynamicBuffer(int estimatedLength)
          Creates a new dynamic buffer with the specified endianness and the specified estimated data length.
static boolean equals(ChannelBuffer bufferA, ChannelBuffer bufferB)
          Returns true if and only if the two specified buffers are identical to each other as described in ChannelBuffer#equals(Object).
static int hashCode(ChannelBuffer buffer)
          Calculates the hash code of the specified buffer.
static java.lang.String hexDump(ChannelBuffer buffer)
          Returns a hex dump of the specified buffer's readable bytes.
static java.lang.String hexDump(ChannelBuffer buffer, int fromIndex, int length)
          Returns a hex dump of the specified buffer's sub-region.
static int indexOf(ChannelBuffer buffer, int fromIndex, int toIndex, byte value)
          The default implementation of ChannelBuffer#indexOf(int, int, byte).
static int swapInt(int value)
          Toggles the endianness of the specified 32-bit integer.
static long swapLong(long value)
          Toggles the endianness of the specified 64-bit long integer.
static int swapMedium(int value)
          Toggles the endianness of the specified 24-bit medium integer.
static short swapShort(short value)
          Toggles the endianness of the specified 16-bit short integer.
static ChannelBuffer wrappedBuffer(byte[] array)
          Creates a new buffer which wraps the specified array with the specified endianness.
static ChannelBuffer wrappedBuffer(java.nio.ByteBuffer buffer)
          Creates a new buffer which wraps the specified NIO buffer's current slice.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_BUFFER

public static final HeapChannelBuffer EMPTY_BUFFER
A buffer whose capacity is 0.

Method Detail

buffer

public static ChannelBuffer buffer(int capacity)
Creates a new Java heap buffer with the specified endianness and capacity. The new buffer's readerIndex and writerIndex are 0.


dynamicBuffer

public static ChannelBuffer dynamicBuffer(byte[] initialBuffer)
Reuses the initialBuffer on the creation of the DynamicBuffer. This avoids a copy, but you should only call this method if the buffer is not being modified after the call of this method.


dynamicBuffer

public static ChannelBuffer dynamicBuffer(int estimatedLength)
Creates a new dynamic buffer with the specified endianness and the specified estimated data length. More accurate estimation yields less unexpected reallocation overhead. The new buffer's readerIndex and writerIndex are 0.


wrappedBuffer

public static ChannelBuffer wrappedBuffer(byte[] array)
Creates a new buffer which wraps the specified array with the specified endianness. A modification on the specified array's content will be visible to the returned buffer.


wrappedBuffer

public static ChannelBuffer wrappedBuffer(java.nio.ByteBuffer buffer)
Creates a new buffer which wraps the specified NIO buffer's current slice. A modification on the specified buffer's content and endianness will be visible to the returned buffer. The new buffer's readerIndex and writerIndex are 0 and buffer.remaining respectively. Note: This method differs from the Original Netty version.


copiedBuffer

public static ChannelBuffer copiedBuffer(byte[] array)
Creates a new buffer with the specified endianness whose content is a copy of the specified array. The new buffer's readerIndex and writerIndex are 0 and array.length respectively.


copiedBuffer

public static ChannelBuffer copiedBuffer(java.nio.ByteBuffer buffer)
Creates a new buffer whose content is a copy of the specified buffer's current slice. The new buffer's readerIndex and writerIndex are 0 and buffer.remaining respectively.


hexDump

public static java.lang.String hexDump(ChannelBuffer buffer)
Returns a hex dump of the specified buffer's readable bytes.


hexDump

public static java.lang.String hexDump(ChannelBuffer buffer,
                                       int fromIndex,
                                       int length)
Returns a hex dump of the specified buffer's sub-region.


hashCode

public static int hashCode(ChannelBuffer buffer)
Calculates the hash code of the specified buffer. This method is useful when implementing a new buffer type.


equals

public static boolean equals(ChannelBuffer bufferA,
                             ChannelBuffer bufferB)
Returns true if and only if the two specified buffers are identical to each other as described in ChannelBuffer#equals(Object). This method is useful when implementing a new buffer type.


compare

public static int compare(ChannelBuffer bufferA,
                          ChannelBuffer bufferB)
Compares the two specified buffers as described in ChannelBuffer.compareTo(ChannelBuffer). This method is useful when implementing a new buffer type.


indexOf

public static int indexOf(ChannelBuffer buffer,
                          int fromIndex,
                          int toIndex,
                          byte value)
The default implementation of ChannelBuffer#indexOf(int, int, byte). This method is useful when implementing a new buffer type.


swapShort

public static short swapShort(short value)
Toggles the endianness of the specified 16-bit short integer.


swapMedium

public static int swapMedium(int value)
Toggles the endianness of the specified 24-bit medium integer.


swapInt

public static int swapInt(int value)
Toggles the endianness of the specified 32-bit integer.


swapLong

public static long swapLong(long value)
Toggles the endianness of the specified 64-bit long integer.



Copyright © 2006 JBoss Inc. All Rights Reserved.