net.sourceforge.apphere.util
Class BytesUtil

java.lang.Object
  extended bynet.sourceforge.apphere.util.BytesUtil

public class BytesUtil
extends java.lang.Object

Provides functions to easily convert short, int, string and float into/from byte[]


Field Summary
static int BYTE_LEN
           
static int DOUBLE_LEN
           
static int FLOAT_LEN
           
static int INT_LEN
           
static int LONG_LEN
           
static int SHORT_LEN
           
 
Constructor Summary
BytesUtil()
           
 
Method Summary
static double bytesToDouble(byte[] bytes, int startIndex)
          Given an array of bytes, convert it to a double, least significant byte is stored in the beginning.
static float bytesToFloat(byte[] bytes, int startIndex)
          Given a byte array, restore it as an int
static int bytesToInt(byte[] bytes, int startIndex)
          Given a byte array, restore it as an int
static long bytesToLong(byte[] bytes, int startIndex)
          Given an array of bytes, convert it to a long, least significant byte is stored in the beginning.
static short bytesToShort(byte[] bytes, int startIndex)
          Given a byte array, restore it as a short
static java.lang.String bytesToString(byte[] bytes, int startIndex)
          Given a byte array, restore a String out of it.
static int doubleToBytes(double dnum, byte[] bytes, int startIndex)
          Given a double, convert it into a byte array
static int floatToBytes(float fnum, byte[] bytes, int startIndex)
          translate float into bytes, stored in byte array starting from startIndex
static int intToBytes(int num, byte[] bytes, int startIndex)
          translate int into bytes, stored in byte array starting from startIndex
static int longToBytes(long lnum, byte[] bytes, int startIndex)
          Given a long, convert it into a byte array
static int shortToBytes(short num, byte[] bytes, int startIndex)
          translate short into bytes, stored in byte array starting from startIndex
static int stringToBytes(java.lang.String str, byte[] bytes, int startIndex)
          Give a String less than 255 bytes, store it as byte array, starting with the length of the string.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BYTE_LEN

public static final int BYTE_LEN
See Also:
Constant Field Values

SHORT_LEN

public static final int SHORT_LEN
See Also:
Constant Field Values

INT_LEN

public static final int INT_LEN
See Also:
Constant Field Values

FLOAT_LEN

public static final int FLOAT_LEN
See Also:
Constant Field Values

LONG_LEN

public static final int LONG_LEN
See Also:
Constant Field Values

DOUBLE_LEN

public static final int DOUBLE_LEN
See Also:
Constant Field Values
Constructor Detail

BytesUtil

public BytesUtil()
Method Detail

intToBytes

public static int intToBytes(int num,
                             byte[] bytes,
                             int startIndex)
translate int into bytes, stored in byte array starting from startIndex

Parameters:
num - the integer to be translated
startIndex - starting to store in this index
Returns:
the index of the cell after storing the number.

bytesToInt

public static int bytesToInt(byte[] bytes,
                             int startIndex)
Given a byte array, restore it as an int

Parameters:
bytes - the byte array
startIndex - the starting index of the place the int is stored

floatToBytes

public static int floatToBytes(float fnum,
                               byte[] bytes,
                               int startIndex)
translate float into bytes, stored in byte array starting from startIndex

Parameters:
startIndex - starting to store in this index
Returns:
the index of the cell after storing the number.

bytesToFloat

public static float bytesToFloat(byte[] bytes,
                                 int startIndex)
Given a byte array, restore it as an int

Parameters:
bytes - the byte array
startIndex - the starting index of the place the int is stored

shortToBytes

public static int shortToBytes(short num,
                               byte[] bytes,
                               int startIndex)
translate short into bytes, stored in byte array starting from startIndex

Parameters:
num - the short to be translated
startIndex - starting to store in this index
Returns:
the index of the cell after storing the number.

bytesToShort

public static short bytesToShort(byte[] bytes,
                                 int startIndex)
Given a byte array, restore it as a short

Parameters:
bytes - the byte array
startIndex - the starting index of the place the int is stored

stringToBytes

public static int stringToBytes(java.lang.String str,
                                byte[] bytes,
                                int startIndex)
Give a String less than 255 bytes, store it as byte array, starting with the length of the string. If the length of the String is longer than 255, a warning is generated, and the string will be truncated.

Parameters:
str - the string that is less than 255 bytes
bytes - the byte array
startIndex - the starting index where the string will be stored.
Returns:
the index of the array after storing this string

bytesToString

public static java.lang.String bytesToString(byte[] bytes,
                                             int startIndex)
Given a byte array, restore a String out of it. the first cell stores the length of the String

Parameters:
bytes - the byte array
startIndex - the starting index where the string is stored, the first cell stores the length
Returns:
the string out of the byte array.

longToBytes

public static int longToBytes(long lnum,
                              byte[] bytes,
                              int startIndex)
Given a long, convert it into a byte array

Parameters:
lnum - the long given to convert
bytes - the bytes where to store the result
startIndex - the starting index of the array where the result is stored.
Returns:
the index of the array after storing this long

bytesToLong

public static long bytesToLong(byte[] bytes,
                               int startIndex)
Given an array of bytes, convert it to a long, least significant byte is stored in the beginning.

Parameters:
bytes - the byte array
startIndex - the starting index of the array where the long is stored.
Returns:
the long result.

doubleToBytes

public static int doubleToBytes(double dnum,
                                byte[] bytes,
                                int startIndex)
Given a double, convert it into a byte array

Parameters:
dnum - the double given to convert
bytes - the bytes where to store the result
startIndex - the starting index of the array where the result is stored.
Returns:
the index of the array after storing this double

bytesToDouble

public static double bytesToDouble(byte[] bytes,
                                   int startIndex)
Given an array of bytes, convert it to a double, least significant byte is stored in the beginning.

Parameters:
bytes - the byte array
startIndex - the starting index of the array where the long is stored.
Returns:
the double result.