com.mindtree.techworks.insight.remoteprotocol.util
Class ByteUtils

java.lang.Object
  extended by com.mindtree.techworks.insight.remoteprotocol.util.ByteUtils

public class ByteUtils
extends java.lang.Object

Util class for byte manipulation.

Version:
$Revision: $ $Date: $
Author:
Bindul Bhowmik

Constructor Summary
ByteUtils()
           
 
Method Summary
static byte[] getByteArrayForCharacterArray(char[] inputArray)
          Creates a byte array representing the input character array.
static byte[] getByteArrayForInt(int valueToConvert)
          Creates a byte array for an int.
static byte[] getByteArrayForShort(short valueToConvert)
          Creates a byte array for a short.
static byte[] getByteArrayForString(java.lang.String inputString)
          Creates a byte array representing the Input String.
static void putBytesInArrayForInt(int valueToConvert, byte[] buf, int offset)
          Creates a byte array for an int.
static void putBytesInArrayForShort(short valueToConvert, byte[] buf, int offset)
          Creates a byte array for a short.
static int readIntFromByteArray(byte[] inputArr, int offset)
          Reads an int value from a byte array.
static short readShortFromByteArray(byte[] inputArr, int offset)
          Reads a short value from a byte array.
static java.lang.String readStringFromByteArray(byte[] inputArr, int offset, int length)
          Decodes a string value from the byte Array passed in.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ByteUtils

public ByteUtils()
Method Detail

readShortFromByteArray

public static short readShortFromByteArray(byte[] inputArr,
                                           int offset)
Reads a short value from a byte array.

Parameters:
inputArr - The byte array to read data from
offset - The offset to read data from
Returns:
The short read from the array
Throws:
java.lang.ArrayIndexOutOfBoundsException - If data cannot be read from specified position

readIntFromByteArray

public static int readIntFromByteArray(byte[] inputArr,
                                       int offset)
Reads an int value from a byte array.

Parameters:
inputArr - The byte array to read data from
offset - The offset to read data from
Returns:
The int read from the array
Throws:
java.lang.ArrayIndexOutOfBoundsException - If data cannot be read from specified position

getByteArrayForShort

public static byte[] getByteArrayForShort(short valueToConvert)
Creates a byte array for a short. The returned array always has 2 elements.

Parameters:
valueToConvert - The short value to convert
Returns:
The byte array containing the two bytes of the short. The size is always 2.

putBytesInArrayForShort

public static void putBytesInArrayForShort(short valueToConvert,
                                           byte[] buf,
                                           int offset)
Creates a byte array for a short. The array values are placed in the input array buf from the position specified by the offset.

Parameters:
valueToConvert - The short value to convert
buf - The buffer to place the bytes in
offset - The offset to insert the values from
Throws:
java.lang.ArrayIndexOutOfBoundsException - If the specified position in the array is not available.

getByteArrayForInt

public static byte[] getByteArrayForInt(int valueToConvert)
Creates a byte array for an int. The returned array always has 4 elements.

Parameters:
valueToConvert - The integer value to convert
Returns:
The byte array containing the four bytes of the integer. The size is always 4.

putBytesInArrayForInt

public static void putBytesInArrayForInt(int valueToConvert,
                                         byte[] buf,
                                         int offset)
Creates a byte array for an int. The array values are placed in the input array buf from the position specified by the offset.

Parameters:
valueToConvert - The integer value to convert
buf - The buffer to place the bytes in
offset - The offset to insert the values from
Throws:
java.lang.ArrayIndexOutOfBoundsException - If the specified position in the array is not available.

getByteArrayForString

public static byte[] getByteArrayForString(java.lang.String inputString)
                                    throws java.nio.charset.CharacterCodingException
Creates a byte array representing the Input String. Every character is represented in two bytes. This method is same as calling #getByteArrayForCharacterArray(char[]) method with the character array representing the string.

Parameters:
inputString - The input string to convert
Returns:
A byte array for the string.
Throws:
java.nio.charset.CharacterCodingException - If there is an exception encoding the characters.

getByteArrayForCharacterArray

public static byte[] getByteArrayForCharacterArray(char[] inputArray)
                                            throws java.nio.charset.CharacterCodingException
Creates a byte array representing the input character array. The size of the array returned is twice that of the array input.

Parameters:
inputArray - The array to convert
Returns:
An array containing the bytes for the characters
Throws:
java.nio.charset.CharacterCodingException - If there is an exception encoding the characters.

readStringFromByteArray

public static java.lang.String readStringFromByteArray(byte[] inputArr,
                                                       int offset,
                                                       int length)
                                                throws java.nio.charset.CharacterCodingException
Decodes a string value from the byte Array passed in.

Parameters:
inputArr - The input array
offset - The position to start reading from
length - The length to read upto
Returns:
The string read
Throws:
java.nio.charset.CharacterCodingException - If there is an exception decoding the characters.


Copyright © 2003-2008 MindTree Consulting Ltd.. All Rights Reserved.