org.clazzes.util.lang
Class Util

java.lang.Object
  extended by org.clazzes.util.lang.Util

public class Util
extends java.lang.Object


Nested Class Summary
static class Util.OperatingSystem
          Enumeration of all OS we are able to detect
 
Field Summary
static java.lang.String CHARSET_ASCII
           
static java.lang.String CHARSET_ISO88591
           
static java.lang.String CHARSET_UTF8
           
 
Method Summary
static byte[] asBCD16(int pin)
          Computes an byte[2] containing the BCD representation of the given int
static java.lang.String asHex(byte[] buf)
          Turns array of bytes into string.
static java.lang.String asHex(byte[] buf, java.lang.String sep)
          Turns array of bytes into string.
static int byteToU8(byte b)
          Converts a (pseudo-signed) byte to an unsigned 8 bit value.
static byte[] concat(byte[] a, byte[] b)
          Copy two Byte arrays into a single resulting Byte array.
static byte[] createZeroTerminatedString(java.lang.String s)
          Converts a String object to an byte[] containing the ASCII coded string and a 0 byte.
static byte[] createZeroTerminatedString(java.lang.String s, java.lang.String charset)
          Converts a String object to an byte[] containing the string coded with the given charset and a 0 byte.
static int decodeBCD(byte[] ba)
          Decodes a BCD coded integer
static byte[] decodeHexstring(java.lang.String s)
          Decodes a string contain hex digits to a byte[]
static boolean equals(byte[] a, byte[] b)
          Test if two Byte arrays are equal.
static boolean equalsNullAware(java.lang.Object a, java.lang.Object b)
          Compares a and b and returns true, if both values are null or both are equal.
static byte[] filledByteArray(int length, byte fillWith)
          Procudes an explicitely filled byte array with the given length.
static byte[] fillUp(byte[] src, int length, byte fillWith)
          Fills up a Byte Array to a multiple of x Bytes. fillUp(byte[12], 16, 0x00) will produce a byte[16], the last 4 Bytes are 0x00.
static java.util.Calendar getCalendar(int year, int month, int day)
          Get a Calendar-Object for the given date (starting at 00:00:00.000.
static int getCRC32(byte[] data)
          Calculate a standard CRC32 Checksum using java.util.zip.CRC32.
static Util.OperatingSystem getOperatingSystem()
          Returns which type of OS we have, after dedecting if necessary
static byte[] getRandom(int len)
          Give me a SecureRandom of a desired length.
static byte[] getSHA1hash(byte[] input)
          Give me the SHA1 Value of a Byte array.
static java.lang.String getSHA1hash(java.lang.String str)
          Give me the SHA1 Value of a String.
static java.util.Calendar getUTCnow()
           
static java.util.Calendar getUTCtimestamp(long millis)
           
static byte[] invert(byte[] a)
          Invert a Byte array.
static boolean inWindows()
          Tells if we are under M$ Windows, after checking if necessary
static java.lang.String parseZeroTerminatedString(byte[] ba)
          Converts a zero terminated ASCII string to a java String object, suggesting ASCII charset.
static java.lang.String parseZeroTerminatedString(byte[] ba, java.lang.String charset)
          Converts a zero terminated ASCII string to a java String object, using the given charset.
static byte[] subArray(byte[] src, int offset, int length)
           
static byte u8ToByte(int u)
          Converts an unsigned 8 bit value to a (pseudo-signed) byte.
static byte[] xor(byte[] a, byte[] b)
          Calculate the XOR between two Byte arrays;
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CHARSET_ASCII

public static final java.lang.String CHARSET_ASCII
See Also:
Constant Field Values

CHARSET_ISO88591

public static final java.lang.String CHARSET_ISO88591
See Also:
Constant Field Values

CHARSET_UTF8

public static final java.lang.String CHARSET_UTF8
See Also:
Constant Field Values
Method Detail

equalsNullAware

public static boolean equalsNullAware(java.lang.Object a,
                                      java.lang.Object b)
Compares a and b and returns true, if both values are null or both are equal.

Parameters:
a - The first object to compare.
b - The second object to compare.
Returns:
true, if a==null && b==null or a.equals(b).

getCRC32

public static int getCRC32(byte[] data)
Calculate a standard CRC32 Checksum using java.util.zip.CRC32.

Parameters:
data - The Bytes to calculate the Checksum from.
Returns:
The Checksum

fillUp

public static byte[] fillUp(byte[] src,
                            int length,
                            byte fillWith)
Fills up a Byte Array to a multiple of x Bytes. fillUp(byte[12], 16, 0x00) will produce a byte[16], the last 4 Bytes are 0x00. fillUp(byte[16], 16, 0x13) will produce a copy of the byte[16]. fillUp(byte[18], 16, 0x01) will produce a byte[32], the lase 14 Bytes are 0x01. fillUp(byte[97], 16, 0xFF) will produce a byte[112], the last 15 Bytes are 0xFF.

Parameters:
src - The Byte Array to fill up.
length - The src ByteArray will be filled up to a length of n*length Bytes.
fillWith - The value of the extra Bytes.
Returns:
The filled up Byte Array.

filledByteArray

public static byte[] filledByteArray(int length,
                                     byte fillWith)
Procudes an explicitely filled byte array with the given length. Useful to produce "empty" arrays whose bytes are reliable set to 0.

Parameters:
length - The length of the byte array to be created.
fillWith - The value of all bytes of the new byte array.
Returns:
The filled Byte Array.

getUTCnow

public static java.util.Calendar getUTCnow()

getUTCtimestamp

public static java.util.Calendar getUTCtimestamp(long millis)

getCalendar

public static java.util.Calendar getCalendar(int year,
                                             int month,
                                             int day)
Get a Calendar-Object for the given date (starting at 00:00:00.000.

Parameters:
year - The human readable year (2006)
month - The human readable month (April = 4)
day - The day of the month.
Returns:

xor

public static byte[] xor(byte[] a,
                         byte[] b)
Calculate the XOR between two Byte arrays;

Parameters:
a -
b -
Returns:
Throws:
java.lang.Exception

invert

public static byte[] invert(byte[] a)
Invert a Byte array.

Parameters:
a -
Returns:
Throws:
java.lang.Exception

getRandom

public static byte[] getRandom(int len)
Give me a SecureRandom of a desired length.

Parameters:
len - The Length of the random value.
Returns:
The random value.

getSHA1hash

public static byte[] getSHA1hash(byte[] input)
                          throws java.lang.Exception
Give me the SHA1 Value of a Byte array.

Parameters:
input -
Returns:
Throws:
java.lang.Exception

getSHA1hash

public static java.lang.String getSHA1hash(java.lang.String str)
                                    throws java.lang.Exception
Give me the SHA1 Value of a String.

Parameters:
input -
Returns:
Throws:
java.lang.Exception

equals

public static boolean equals(byte[] a,
                             byte[] b)
                      throws java.lang.NullPointerException
Test if two Byte arrays are equal.

Parameters:
a -
b -
Returns:
Throws:
java.lang.NullPointerException

concat

public static byte[] concat(byte[] a,
                            byte[] b)
Copy two Byte arrays into a single resulting Byte array.

Parameters:
a -
b -
Returns:

subArray

public static byte[] subArray(byte[] src,
                              int offset,
                              int length)

asHex

public static java.lang.String asHex(byte[] buf)
Turns array of bytes into string.

Parameters:
buf - Array of bytes to convert to hex string, with a space between each byte.
Returns:
Generated hex string (without any 0x prefix).

asHex

public static java.lang.String asHex(byte[] buf,
                                     java.lang.String sep)
Turns array of bytes into string.

Parameters:
buf - Array of bytes to convert to hex string.
sep - Separator to put between each byte, null stands for ""
Returns:
Generated hex string (without any 0x prefix).

decodeHexstring

public static byte[] decodeHexstring(java.lang.String s)
Decodes a string contain hex digits to a byte[]

Parameters:
s - The string to decode.
Returns:
The decoded byte[]
Throws:
some - runtimeexceptions if the string is invalid

byteToU8

public static int byteToU8(byte b)
Converts a (pseudo-signed) byte to an unsigned 8 bit value.

Parameters:
u -
Returns:

u8ToByte

public static byte u8ToByte(int u)
Converts an unsigned 8 bit value to a (pseudo-signed) byte.

Parameters:
u -
Returns:

createZeroTerminatedString

public static byte[] createZeroTerminatedString(java.lang.String s)
Converts a String object to an byte[] containing the ASCII coded string and a 0 byte.

Parameters:
s -
Returns:

createZeroTerminatedString

public static byte[] createZeroTerminatedString(java.lang.String s,
                                                java.lang.String charset)
Converts a String object to an byte[] containing the string coded with the given charset and a 0 byte.

Parameters:
s -
charset - We recommend to use this class' CHARSET_* constants
Returns:

parseZeroTerminatedString

public static java.lang.String parseZeroTerminatedString(byte[] ba)
Converts a zero terminated ASCII string to a java String object, suggesting ASCII charset.

Parameters:
ba -
Returns:

parseZeroTerminatedString

public static java.lang.String parseZeroTerminatedString(byte[] ba,
                                                         java.lang.String charset)
Converts a zero terminated ASCII string to a java String object, using the given charset.

Parameters:
ba -
charset - We recommend to use this class' CHARSET_* constants
Returns:

getOperatingSystem

public static Util.OperatingSystem getOperatingSystem()
Returns which type of OS we have, after dedecting if necessary

Returns:

inWindows

public static boolean inWindows()
Tells if we are under M$ Windows, after checking if necessary

Returns:

asBCD16

public static byte[] asBCD16(int pin)
Computes an byte[2] containing the BCD representation of the given int

Parameters:
pin - The pin to encodde, 1 .. 9999
Returns:

decodeBCD

public static int decodeBCD(byte[] ba)
Decodes a BCD coded integer

Parameters:
ba - The bytearray containing the BCD bytes
Returns:
The decoded value


Copyright © 2010. All Rights Reserved.