iaik.me.utils
Class Util

java.lang.Object
  |
  +--iaik.me.utils.Util

public class Util
extends Object

This class provides several, often used methods for string/byte conversion, stream operations etc. . All methods are static so this class needs not to be instantiated.


Method Summary
static byte[] concat(byte[] b1, byte[] b2)
          Concatenates two bytearrays and returns a new bytearray of the size form bytearray one plus the size from bytearray two.
static byte[] decodeByteArray(String s)
          Decodes a given Base64 coded string and returns the values in a byte array.
static int[] decodeIntArray(String s)
          Decodes a given Base64 coded string and returns the values in an integerarray.
static boolean equals(Object a, Object b)
          Indicates whether some other object is "equal to" this one.
static Reader getASCIIReader(InputStream in)
          Fertches a reader that returns data from the stream assuming that ASCII encoding is used.
static Writer getASCIIWriter(OutputStream out)
          Fetches a writer that writes data to the stream in ASCII encoding.
static int hashCode(byte[] data)
          Generates a hashcode from a given bytearray.
static int hashCode(Object obj)
          Generates a hashcode for a given object.
static byte[] increment(byte[] b)
          Increments each value of a byte array (b[0]++, b[1]++ ...).
static boolean isAvailable(String className)
          Checks whether a given class is available or not.
static boolean isNull(Object[] obj)
          Checks if the objectarray is set to null.
static Class loadClass(String className)
          Loads a given class via the Class.forName(....) method.
static int max(int a, int b)
          Returns the bigger one of two integers.
static int min(int a, int b)
          Returns the lower one of two integers.
static byte[] readCompleteStream(InputStream in)
          Read the contents of the stream into a bytearray.
static int readN(InputStream in, byte[] buffer)
          Reads from an inputstream until the given buffer is full.
static int readN(InputStream in, byte[] buffer, int offset, int length)
          Reads N bytes from an inputstream into a buffer and tries reading a view times before throwing an exception in case the stream blocks.
static void setSeparator(String c)
          Sets the seperator character (or String) for the 'toString()' methods.
static byte[] toASCIIByteArray(String s)
          Converts a string into a asciibytearray.
static String toASCIIString(byte[] b)
          Converts a bytearray into an asciistring.
static int toByte(char c, int radix)
          Converts the given char to an int.
static byte[] toByteArray(String s)
          Converts the given string with hex values to a bytearray.
static String toString(byte[] b)
          Converts a given byte array into a formated hex-string.
static String toString(byte[] b, int ofs, int len)
          Converts a given byte array into a formated hex-string.
static String toString(int i)
          Turns an integer into a formatted Hex number.
static byte[] toUTF8ByteArray(String s)
          Converts an UTF8 string into a bytearray.
static String toUTF8String(byte[] b)
          Converts a bytearray into an UTF8 string.
static void waitKey()
          Wait for the user to press the return key on System.in.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

loadClass

public static Class loadClass(String className)
Loads a given class via the Class.forName(....) method. If you use this method within applets, pay attention to the fact that classes that are not within the JAR file are serched on the server the applet was downloaded from.
Parameters:
className - the fully qualified name of the desired class, e.g.:
Class clazz = Utils.loadClass("java.lang.Thread");
Returns:
the class object or null if the class cannot be found.
Since:
3.0
See Also:
Class.forName(String)

isAvailable

public static boolean isAvailable(String className)
Checks whether a given class is available or not. If you use this method within applets, pay attention to the fact that classes that are not within the JAR file are serched on the server the applet was downloaded from.
Parameters:
className - the classname of the class to be checked
Returns:
true if the class is available or false if the class cannot be found
Since:
3.0

concat

public static byte[] concat(byte[] b1,
                            byte[] b2)
Concatenates two bytearrays and returns a new bytearray of the size form bytearray one plus the size from bytearray two.
Parameters:
b1 - the first bytearray
b2 - the second bytearray
Returns:
b3 = b1+b2
Since:
3.0

toString

public static String toString(byte[] b,
                              int ofs,
                              int len)
Converts a given byte array into a formated hex-string. For example: 200 -> "00:00:00:C8"
Parameters:
b - the byte array
ofs - startingpoint of the conversion within the array
len - number of bytes to be converted
Returns:
the converted string or the string "(null)" if the bytearray was null.
Since:
3.0

toString

public static String toString(byte[] b)
Converts a given byte array into a formated hex-string. For example: 200 -> "00:00:00:C8"
Parameters:
b - the byte array
Returns:
the converted string or the string "(null)" if the bytearray was null.
Since:
3.0

decodeByteArray

public static byte[] decodeByteArray(String s)
Decodes a given Base64 coded string and returns the values in a byte array.
Parameters:
s - the Base64 coded string
Returns:
the decoded string as byte array or null if an exception occured during number conversion.
Since:
3.0

decodeIntArray

public static int[] decodeIntArray(String s)
Decodes a given Base64 coded string and returns the values in an integerarray.
Parameters:
s - the Base64 coded string
Returns:
the decoded string as intarray or null if an exception occured during number conversion.
Since:
3.0

equals

public static boolean equals(Object a,
                             Object b)
Indicates whether some other object is "equal to" this one. The equals method implements an equivalence relation on non-null object references: It is reflexive: for any non-null reference value x, x.equals(x) should return true. It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true. It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true. It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified. For any non-null reference value x, x.equals(null) should return false. The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).
Bytearrays are compared item by item ( a[0] == b[0], a[1] == b[1].....). If only one item is different, the bytearrays are different. If a and b are null the objects are considered equal.
Parameters:
a - the first object
b - the second object
Returns:
true if the objects are equal (or both null) or false if not.
Since:
3.0

isNull

public static boolean isNull(Object[] obj)
Checks if the objectarray is set to null. If the array is not null each element of the array is checked whether it is null or not.
Parameters:
obj - the objectarray
Returns:
true if the objectarray or one of its items is null
Since:
3.0

toString

public static String toString(int i)
Turns an integer into a formatted Hex number. For example: 200 -> "00:00:00:C8" The size of the
Parameters:
i - the number to convert
Returns:
the converted hexnumber
Since:
3.0

increment

public static byte[] increment(byte[] b)
Increments each value of a byte array (b[0]++, b[1]++ ...).
Parameters:
b - a byte array
Returns:
the incremented byte array
Since:
3.0

waitKey

public static void waitKey()
Wait for the user to press the return key on System.in. As System.in is not available on the J2ME platform
Since:
3.0

toByte

public static int toByte(char c,
                         int radix)
Converts the given char to an int. '0' maps to 0, 'a' and 'A' to 10, 'z' and 'Z' to 36, and so on. For all other characters and for resulting values equal to or larger than radix -1 is returned.
For e.g.:Utils.toByte('A', 16); (16 for hex numbers).
Parameters:
c - character
radix -  
Returns:
the int value of the character.
Since:
3.0

toByteArray

public static byte[] toByteArray(String s)
Converts the given string with hex values to a bytearray. For example "001122" is turned into {0, 0x11, 0x22}. All characters outside the range of '0'-'9', 'a'-'z', and 'A'-'Z' are simply ignored.
Parameters:
s - string
Returns:
the values as bytearray
Since:
3.0

readCompleteStream

public static byte[] readCompleteStream(InputStream in)
                                 throws IOException
Read the contents of the stream into a bytearray. The stream is read until it returns EOF.
Parameters:
in - the InputStream to be read
Returns:
the content of the stream in a bytearray
Throws:
IOException - if a error during reading of the stream occurs.
Since:
3.0

readN

public static int readN(InputStream in,
                        byte[] buffer,
                        int offset,
                        int length)
                 throws IOException
Reads N bytes from an inputstream into a buffer and tries reading a view times before throwing an exception in case the stream blocks.
Parameters:
in - the InputStream
buffer - the buffer where to write the bytes to
offset - where to start writing in the buffer
length - how much bytes shall be read
Returns:
number of bytes read
Throws:
IOException - if the stream can't be read
Since:
3.0

readN

public static int readN(InputStream in,
                        byte[] buffer)
                 throws IOException
Reads from an inputstream until the given buffer is full.
Parameters:
in - inputstream
buffer - the buffer where to write the data into
Returns:
number of read bytes
Throws:
IOException - if stream can't be read
Since:
3.0

min

public static int min(int a,
                      int b)
Returns the lower one of two integers.
Parameters:
a - first int
b - second int
Returns:
the value of a if the first int is lower than the second one
Since:
3.0

max

public static int max(int a,
                      int b)
Returns the bigger one of two integers.
Parameters:
a - first int
b - second int
Returns:
the value of the first int if it is larger than the second one
Since:
3.0

hashCode

public static int hashCode(byte[] data)
Generates a hashcode from a given bytearray. *
Parameters:
data - the bytearray
Returns:
the hashcode from the bytearray
Since:
3.0

hashCode

public static int hashCode(Object obj)
Generates a hashcode for a given object.
Parameters:
obj -  
Returns:
the hashvalue of the object
Since:
3.0

toASCIIString

public static String toASCIIString(byte[] b)
Converts a bytearray into an asciistring.
Parameters:
byte - array
Returns:
asciistring
Since:
3.0

toUTF8String

public static String toUTF8String(byte[] b)
Converts a bytearray into an UTF8 string. If the bytearray cannot be converted into an UTF8 string, it is conveterd into an ASCII string.
Parameters:
bytearray -  
Returns:
UTF8 string
Since:
3.0

toASCIIByteArray

public static byte[] toASCIIByteArray(String s)
Converts a string into a asciibytearray.
Parameters:
s - the ASCII string
Returns:
string as asciibytearray
Since:
3.0

toUTF8ByteArray

public static byte[] toUTF8ByteArray(String s)
Converts an UTF8 string into a bytearray. If an encoding error occurs the remaining string is converted into an ASCIIbytearray.
Parameters:
s - utf8 string
Returns:
the utf8 string as bytearray
Since:
3.0

getASCIIReader

public static Reader getASCIIReader(InputStream in)
Fertches a reader that returns data from the stream assuming that ASCII encoding is used. This is useful when connecting over the network as both peers obviously have to use the same encoding which is defined as ASCII for most common text protocols (HTTP, HTTPS, etc.).
For e.g.: Reader r = Util.getASCIIReader( new FileInputStream("temp.txt") );
Parameters:
in - the inputstream
Returns:
Reader object
Since:
3.0

getASCIIWriter

public static Writer getASCIIWriter(OutputStream out)
Fetches a writer that writes data to the stream in ASCII encoding. This is useful when connecting over the network as both peers obviously have to use the same encoding which is defined as ASCII for most common text protocols (HTTP, HTTPS, etc.).
Writer r = Util.getASCIIWriter( new FileOutputStream("temp.txt") );
Parameters:
out - the outputstream
Returns:
the writer object.
Since:
3.0

setSeparator

public static void setSeparator(String c)
Sets the seperator character (or String) for the 'toString()' methods. The default seperator is ':' which can be replaced by a different character with this method. For example, if the seperator is set to '.' the bytearray [ 2, 67, 1] will be displayed as "2.67.1".
Parameters:
c - new seperator String
Since:
3.04

This Javadoc may contain text parts from IETF Internet Standard specifications, see copyright note) and RSA Data Security Public-Key Cryptography Standards (see copyright note).

IAIK-JCE ME 3.04, (c) 2002 IAIK, (c) 2003 to 2006 Stiftung SIC