org.littleshoot.util
Class ByteBufferUtils

java.lang.Object
  extended by org.littleshoot.util.ByteBufferUtils

public final class ByteBufferUtils
extends Object

Utility class for manipulating ByteBuffers.


Constructor Summary
ByteBufferUtils()
           
 
Method Summary
static ByteBuffer combine(Collection<ByteBuffer> buffers)
          Combines the remaining data from the given Collection of ByteBuffers into a single consolidated ByteBuffer.
static String getHexdump(ByteBuffer in)
          Utility method for dumping a buffer in hex.
static void logBufferToWrite(ByteBuffer buffer)
          Logs the data contained in a ByteBuffer that's ready to be written to the network.
static byte[] readBytes16(ByteBuffer buf)
           
static int remaining(Collection<ByteBuffer> buffers)
           
static Collection<ByteBuffer> split(ByteBuffer buffer, int chunkSize)
          Splits the specified ByteBuffer into smaller ByteBuffers of the specified size.
static ByteBuffer[] toArray(byte[] byteArray, int sizeLimit)
          Splits the specified byte array into an array of ByteBuffers, each with the specified maximum size.
static byte[] toRawBytes(ByteBuffer buf)
           
static String toString(ByteBuffer buffer)
          Returns the buffer as a string while preserving the buffer position and limit.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ByteBufferUtils

public ByteBufferUtils()
Method Detail

toArray

public static ByteBuffer[] toArray(byte[] byteArray,
                                   int sizeLimit)
Splits the specified byte array into an array of ByteBuffers, each with the specified maximum size. This is useful, for example, when trying to limit sizes to a network MTU.

Parameters:
byteArray - The array to segment.
sizeLimit - The maximum size of each byte buffer.
Returns:
The new array of ByteBuffers.

split

public static Collection<ByteBuffer> split(ByteBuffer buffer,
                                           int chunkSize)
Splits the specified ByteBuffer into smaller ByteBuffers of the specified size. The remaining bytes in the buffer must be greater than the chunk size. This method will create smaller buffers of the specified size until there are fewer remaining bytes than the chunk size, when it will simply add a buffer the same size as the number of bytes remaining.

Parameters:
buffer - The ByteBuffer to split.
chunkSize - The size of the smaller buffers to create.
Returns:
A Collection of ByteBuffers of the specified size. The final buffer in the Collection will have a size > 0 and <= the chunk size.

combine

public static ByteBuffer combine(Collection<ByteBuffer> buffers)
Combines the remaining data from the given Collection of ByteBuffers into a single consolidated ByteBuffer.

Parameters:
buffers - The Collection of ByteBuffers to make into a single buffer.
Returns:
A new ByteBuffer combining the remaining data of the Collection of ByteBuffers.

remaining

public static int remaining(Collection<ByteBuffer> buffers)

logBufferToWrite

public static void logBufferToWrite(ByteBuffer buffer)
Logs the data contained in a ByteBuffer that's ready to be written to the network.

Parameters:
buffer - The buffer to log.

toString

public static String toString(ByteBuffer buffer)
Returns the buffer as a string while preserving the buffer position and limit.

Parameters:
buffer - The buffer to create a string from.
Returns:
The buffer string.

readBytes16

public static byte[] readBytes16(ByteBuffer buf)

getHexdump

public static String getHexdump(ByteBuffer in)
Utility method for dumping a buffer in hex.

Parameters:
in - The buffer to dump.
Returns:
The hex string.

toRawBytes

public static byte[] toRawBytes(ByteBuffer buf)


Copyright © 2011-2013 LittleShoot. All Rights Reserved.