cgl.narada.util
Class ByteUtilities

java.lang.Object
  extended by cgl.narada.util.ByteUtilities

public class ByteUtilities
extends Object

This class contains utility classes that are used throughout the API. Some of the methods contained here are helpful in the marshalling and unmarshalling of information contained within the protocol packets. Other methods which include printing bytes, ints ,... as a sequence of 1's and 0's are primarily intended for debugging purposes. Other methods include conversion of integer arrays into byte arrays and vice versa.


Constructor Summary
ByteUtilities(byte[] addressInBytes)
          Constructor initialized using a sequence of bytes.
ByteUtilities(int[] addressInInts)
          Constructor initialized using a sequence of integers.
 
Method Summary
 int[] byteArrayToIntArray(byte[] byteArray)
           
static int[] byteArrayToIntArray(byte[] byteArray, int offset, int numOfBytes)
           
 byte[] getAddressInBytes()
          This allows us to retrieve an integer sequence as a sequence of bytes.
 int[] getAddressInInts()
          This allows us to retrieve an byte sequence as a sequence of integers.
static byte[] getBytes(int value)
          Returns the bytes that are associated with an integer value.
static byte[] getBytes(int[] intArray, int pos)
           
static byte[] getBytes(long value)
          Returns the bytes that are associated with a long value.
static byte[] getBytes(short value)
          Returns the bytes that are associated with a short value.
static int getFirstOneBitPosition(int value)
          Returns the position (1 through 32) of the first "1"-bit in the binary representation of the supplied integer.
static int getFirstZeroBitPosition(int value)
          Returns the position (1 through 32) of the first "0"-bit in the binary representation of the supplied integer.
static int getInt(byte[] data)
          Given a sequence of bytes, this method returns the int value associated with the sequence of bytes.
static int getInt(byte[] data, int offset)
           
static int getIntFromByte(byte a)
           
static int getLastOneBitPosition(int value)
          Returns the position (1 through 32) of the last "1"-bit in the binary representation of the supplied integer.
static long getLong(byte[] data)
          Given a sequence of bytes, this method returns the long value associated with the sequence of bytes.
static long getLong(byte[] data, int offset)
           
static short getShort(byte[] data)
          Given a sequence of bytes, this method returns the short value associated with the sequence of bytes.
 int getValue(byte a, byte b)
          Give two bytes, it returns the Integer value of the 16-bit number encoded within the 2 bytes a, b
 byte[] intArrayToByteArray(int[] intArray)
           
static byte[] intArrayToByteArray(int[] intArray, int offset, int len)
           
static void main(String[] args)
           
static int performNotOperation(int value)
          This performs an inversion of the bits in an integer.
static String printByte(byte b)
          This method is primarily used for debugging purposes, and provides us with a representation of a byte as sequence of 1's and 0's that is of length=8.
static String printInt(int value)
          This method is primarily used for debugging purposes, and provides us with a representation of an integer as sequence of 1's and 0's that is of length=32.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ByteUtilities

public ByteUtilities(byte[] addressInBytes)
Constructor initialized using a sequence of bytes. This allows us to retrieve the same sequence as a sequence of integers.

Parameters:
addressInBytes - The address as a sequence of bytes.

ByteUtilities

public ByteUtilities(int[] addressInInts)
Constructor initialized using a sequence of integers. This allows us to retrieve the same sequence as a sequence of bytes.

Parameters:
addressInInts - The address as a sequence of integers.
Method Detail

getAddressInBytes

public byte[] getAddressInBytes()
This allows us to retrieve an integer sequence as a sequence of bytes.

Returns:
The address as a sequence of bytes.

getAddressInInts

public int[] getAddressInInts()
This allows us to retrieve an byte sequence as a sequence of integers.

Returns:
The address as a sequence of integers.

intArrayToByteArray

public byte[] intArrayToByteArray(int[] intArray)

intArrayToByteArray

public static byte[] intArrayToByteArray(int[] intArray,
                                         int offset,
                                         int len)

byteArrayToIntArray

public int[] byteArrayToIntArray(byte[] byteArray)

byteArrayToIntArray

public static int[] byteArrayToIntArray(byte[] byteArray,
                                        int offset,
                                        int numOfBytes)

getValue

public int getValue(byte a,
                    byte b)
Give two bytes, it returns the Integer value of the 16-bit number encoded within the 2 bytes a, b


getIntFromByte

public static int getIntFromByte(byte a)

getBytes

public static byte[] getBytes(short value)
Returns the bytes that are associated with a short value.

Parameters:
value - The short for which we are seeking an equivalent byte stream representation.
Returns:
The short value as a sequence of bytes

getBytes

public static byte[] getBytes(int value)
Returns the bytes that are associated with an integer value.

Parameters:
value - The integer for which we are seeking an equivalent byte stream representation.
Returns:
The int value as a sequence of bytes

getBytes

public static byte[] getBytes(int[] intArray,
                              int pos)

getBytes

public static byte[] getBytes(long value)
Returns the bytes that are associated with a long value.

Parameters:
value - The long for which we are seeking an equivalent byte stream representation.
Returns:
The long value as a sequence of bytes

getShort

public static short getShort(byte[] data)
Given a sequence of bytes, this method returns the short value associated with the sequence of bytes. This sequence of bytes passed as the argument should be of length=2.

Parameters:
data - The sequence of bytes representing a short value.
Returns:
The short value associated with the sequence of bytes

getInt

public static int getInt(byte[] data)
Given a sequence of bytes, this method returns the int value associated with the sequence of bytes. This sequence of bytes passed as the argument should be of length=4.

Parameters:
data - The sequence of bytes representing a int value.
Returns:
The integer value associated with the sequence of bytes

getInt

public static int getInt(byte[] data,
                         int offset)

getLong

public static long getLong(byte[] data)
Given a sequence of bytes, this method returns the long value associated with the sequence of bytes. This sequence of bytes passed as the argument should be of length=8.

Parameters:
data - The sequence of bytes representing a long value.
Returns:
The long value associated with the sequence of bytes

getLong

public static long getLong(byte[] data,
                           int offset)

printByte

public static String printByte(byte b)
This method is primarily used for debugging purposes, and provides us with a representation of a byte as sequence of 1's and 0's that is of length=8.

Returns:
The String representation of a byte as 1's and 0's

printInt

public static String printInt(int value)
This method is primarily used for debugging purposes, and provides us with a representation of an integer as sequence of 1's and 0's that is of length=32.

Returns:
The String representation of an integer as 1's and 0's

performNotOperation

public static int performNotOperation(int value)
This performs an inversion of the bits in an integer. There are no unary operators which do this in Java. I have implemented the NOT operation using an XOR operator


getFirstOneBitPosition

public static int getFirstOneBitPosition(int value)
Returns the position (1 through 32) of the first "1"-bit in the binary representation of the supplied integer. This method returns a "0" if there are no 1-bits in the binary representation i.e. if the supplied integer is zero.


getLastOneBitPosition

public static int getLastOneBitPosition(int value)
Returns the position (1 through 32) of the last "1"-bit in the binary representation of the supplied integer. This method returns a "0" if there are no 1-bits in the binary representation i.e. if the supplied integer is zero.


getFirstZeroBitPosition

public static int getFirstZeroBitPosition(int value)
Returns the position (1 through 32) of the first "0"-bit in the binary representation of the supplied integer. This method returns a "0" if there are no 0-bits in the binary representation i.e. if the supplied integer is 0xFFFF.


main

public static void main(String[] args)


For comments and suggestions please send e-mail to The NaradaBrokering Project