marf.util
Class ByteUtils

java.lang.Object
  extended by marf.util.ByteUtils

public class ByteUtils
extends java.lang.Object

Byte-Array Conversion Utility Functions.

$Id: ByteUtils.java,v 1.9 2008/02/21 19:57:44 mokhov Exp $

Since:
0.3.0.1
Version:
$Revision: 1.9 $
Author:
Serguei Mokhov

Constructor Summary
protected ByteUtils()
          Allow derivatives.
 
Method Summary
static int byteArrayToInt(byte[] paRawBytes, boolean pbBigEndian)
          Converts a byte array to int value.
static int byteArrayToInt(byte[] paRawBytes, int piOffset, boolean pbBigEndian)
          Converts a portion of a byte array with given offset to int value.
static short byteArrayToShort(byte[] paRawBytes, boolean pbBigEndian)
          Converts a byte array to short value.
static short byteArrayToShort(byte[] paRawBytes, int piOffset, boolean pbBigEndian)
          Converts a portion of a byte array with given offset to short value.
static java.lang.String byteArrayToString(byte[] paRawBytes)
          Converts a byte array to String value.
static java.lang.String byteArrayToString(byte[] paRawBytes, int piOffset, int piLength)
          Converts a portion of a byte array to String value.
static java.lang.String getMARFSourceCodeRevision()
          Returns source code revision information.
static byte[] intToByteArray(int piValueToConvert, boolean pbBigEndian)
          Converts an int value to a byte array.
static byte[] stringToByteArray(java.lang.String pstrStringToConvert)
          Converts a String value to a byte array in US-ASCII charset.
static byte[] stringToByteArray(java.lang.String pstrStringToConvert, java.lang.String pstrCharSet)
          Attempts to convert a String value to a byte array in specified charset.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ByteUtils

protected ByteUtils()
Allow derivatives.

Method Detail

byteArrayToShort

public static short byteArrayToShort(byte[] paRawBytes,
                                     boolean pbBigEndian)
Converts a byte array to short value. Equivalent to byteArrayToShort(paRawBytes, 0, pbBigEndian);

Parameters:
paRawBytes - the byte array
pbBigEndian - true if the bytes are in Big-endian order; false otherwise
Returns:
short representation of the bytes

byteArrayToShort

public static short byteArrayToShort(byte[] paRawBytes,
                                     int piOffset,
                                     boolean pbBigEndian)
Converts a portion of a byte array with given offset to short value.

Parameters:
paRawBytes - the byte array
piOffset - offset in the original array to start reading bytes from
pbBigEndian - true if the bytes are in Big-endian order; false otherwise
Returns:
short representation of the bytes

byteArrayToInt

public static int byteArrayToInt(byte[] paRawBytes,
                                 boolean pbBigEndian)
Converts a byte array to int value. Equivalent to intArrayToShort(paRawBytes, 0, pbBigEndian);

Parameters:
paRawBytes - the byte array
pbBigEndian - true if the bytes are in Big-endian order; false otherwise
Returns:
int representation of the bytes

byteArrayToInt

public static int byteArrayToInt(byte[] paRawBytes,
                                 int piOffset,
                                 boolean pbBigEndian)
Converts a portion of a byte array with given offset to int value.

Parameters:
paRawBytes - the byte array
piOffset - offset in the original array to start reading bytes from
pbBigEndian - true if the bytes are in Big-endian order; false otherwise
Returns:
int representation of the bytes

intToByteArray

public static byte[] intToByteArray(int piValueToConvert,
                                    boolean pbBigEndian)
Converts an int value to a byte array.

Parameters:
piValueToConvert - the original integer
pbBigEndian - true if the bytes are in Big-endian order; false otherwise
Returns:
byte[] representation of the int

byteArrayToString

public static java.lang.String byteArrayToString(byte[] paRawBytes)
Converts a byte array to String value. Cleans up non-word characters along the way. Equivalent to byteArrayToString(paRawBytes, 0, paRawBytes.length);

Parameters:
paRawBytes - the byte array, non-UNICODE
Returns:
UNICODE String representation of the bytes

byteArrayToString

public static java.lang.String byteArrayToString(byte[] paRawBytes,
                                                 int piOffset,
                                                 int piLength)
Converts a portion of a byte array to String value. Cleans up non-word characters along the way.

Parameters:
paRawBytes - the byte array, non-UNICODE
piOffset - offset in the original array to start reading bytes from
piLength - how many bytes of the array parameter to interpret as String
Returns:
UNICODE String representation of the bytes with trailing garbage stripped; "" if array length is less than piOffset + piLength; "" if the generated string begins with garbage

stringToByteArray

public static byte[] stringToByteArray(java.lang.String pstrStringToConvert)
Converts a String value to a byte array in US-ASCII charset. Equivalent to stringToByteArray(pstrStringToConvert, "US-ASCII");

Parameters:
pstrStringToConvert - the original string
Returns:
null-terminated byte[] representation of the String

stringToByteArray

public static byte[] stringToByteArray(java.lang.String pstrStringToConvert,
                                       java.lang.String pstrCharSet)
Attempts to convert a String value to a byte array in specified charset. If the charset is invalid, returns plain byte-representation of the host environment.

Parameters:
pstrStringToConvert - the original string
pstrCharSet - character set to assume for the original string
Returns:
null-terminated byte[] representation of the String

getMARFSourceCodeRevision

public static java.lang.String getMARFSourceCodeRevision()
Returns source code revision information.

Returns:
revision string


SourceForge Logo