com.objsys.asn1j.runtime
Class Asn1Util

java.lang.Object
  extended by com.objsys.asn1j.runtime.Asn1Util

public class Asn1Util
extends java.lang.Object

This class contains some general purpose static utility functions.


Constructor Summary
Asn1Util()
           
 
Method Summary
static java.lang.String BCDToString(byte[] bcd)
          Translates a BCD string to an ASCII string.
static byte[] decodeBase64Array(byte[] srcArray)
          Translates the specified Base64 array into byte array.
static byte[] encodeBase64Array(byte[] srcArray)
          Translates the specified byte array to Base64 byte array.
static java.lang.String firstSortedElement(java.util.Vector v)
          Returns the element of a Vector (that is assumed to contain strings) that is lexicographically the lowest element in the Vector.
static int getBytesCount(long val)
          Calculate the count of bytes necessary to represent a signed long value.
static java.util.Vector getKeys(java.util.Hashtable h)
          Returns a Vector containing the keys (assumed to be strings) of a passed Hashtable.
static int getUlongBytesCount(long val)
          Calculate the count of bytes necessary to represent an unsigned long value.
static boolean isLetter(char c)
          Determine if a specified character is a letter.
static boolean isSpaceChar(char ch)
          Determine if a specified character is a space.
static boolean isWhitespace(char ch)
          Determine if a specified character is whitespace.
static double log2(double val)
          Calculate the log base 2 of value
static double pow(int base, int exponent)
          Raises an integer base to an integer power and returns the result as a double.
static java.lang.String[] splitOnWhitespace(java.lang.String delimitedString)
          Split a string around whitespace characters.
static byte[] stringToBCD(java.lang.String str)
          Translates an ASCII string to a BCD string.
static byte[] stringToTBCD(java.lang.String str)
          Translates an ASCII String to a TBCD String.
static java.lang.String stripWhitespace(java.lang.String value)
          Return the given string with all whitespace characters removed.
static java.lang.String TBCDToString(byte[] bcd)
          Translates a TBCD string to an ASCII string.
static java.lang.String toHexString(byte b)
          Convert a byte value to a hex string.
static java.lang.String toHexString(byte[] b, int offset, int nbytes)
          Convert a string of bytes into a hex string.
static java.lang.StringBuffer toHexString(byte b, java.lang.StringBuffer hexbuf)
          Convert a byte value to a hex string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Asn1Util

public Asn1Util()
Method Detail

stripWhitespace

public static java.lang.String stripWhitespace(java.lang.String value)
Return the given string with all whitespace characters removed. Here, "whitespace characters" means those characters defined by X.680 12.1.6 as whitespace: HT, LF, VT, FF, CR, SPACE.

Parameters:
value -
Returns:
value, with all whitespace removed; if the input string has no whitespace, it is returned itself.

toHexString

public static java.lang.String toHexString(byte b)
Convert a byte value to a hex string. Unlike the Java built-in function, this will: a. not sign extend the byte value out to 32 bits if the MSB is set, and b. put a zero padding byte in front if less than 0xf In other words, a character string of length 2 is always returned.


toHexString

public static java.lang.StringBuffer toHexString(byte b,
                                                 java.lang.StringBuffer hexbuf)
Convert a byte value to a hex string. Unlike the Java built-in function, this will: a. not sign extend the byte value out to 32 bits if the MSB is set, and b. put a zero padding byte in front if less than or equal to 0xf In other words, a character string of length 2 is always appended to hexbuf.


toHexString

public static java.lang.String toHexString(byte[] b,
                                           int offset,
                                           int nbytes)
Convert a string of bytes into a hex string.


getBytesCount

public static int getBytesCount(long val)
Calculate the count of bytes necessary to represent a signed long value.

Parameters:
val - Integer value in which to count bytes.
Returns:
Bit count.

log2

public static double log2(double val)
Calculate the log base 2 of value

Parameters:
val - double value for which to count log(base2) val.
Returns:
result value.

getUlongBytesCount

public static int getUlongBytesCount(long val)
Calculate the count of bytes necessary to represent an unsigned long value.

Parameters:
val - Integer value in which to count bytes.
Returns:
Bit count.

encodeBase64Array

public static byte[] encodeBase64Array(byte[] srcArray)
Translates the specified byte array to Base64 byte array. The resulting array could be converted to the String by new String (byte[]) constructor.

Parameters:
srcArray - byte array to be translated
Returns:
Base64 encoded byte array

decodeBase64Array

public static byte[] decodeBase64Array(byte[] srcArray)
Translates the specified Base64 array into byte array. The resulting array could be converted to the String by new String (byte[]) constructor.

Parameters:
srcArray - Base64 byte array to be translated
Returns:
decoded byte array

stringToBCD

public static byte[] stringToBCD(java.lang.String str)
                          throws Asn1ValueParseException
Translates an ASCII string to a BCD string. The ASCII string must contain only characters in the range [0..9] & ([A..F] | [a..f]). If the length of the source string is not even, the unused part of the last byte will be set to 0xF.

Parameters:
str - the source ASCII string
Returns:
the BCD string as a byte array.
Throws:
Asn1ValueParseException - If invalid characters are in the source string.

BCDToString

public static java.lang.String BCDToString(byte[] bcd)
Translates a BCD string to an ASCII string. The 0xF half-byte is assumed as end of string indicator.

Parameters:
bcd - the source BCD string
Returns:
the ASCII string.

stringToTBCD

public static byte[] stringToTBCD(java.lang.String str)
                           throws Asn1ValueParseException
Translates an ASCII String to a TBCD String. TBCD strings are packed such that the least significant nibble occupies the upper four bits in each byte. The ASCII string "12345" would be encoded 0x2143f5.

Parameters:
str - the source ASCII string
Returns:
the BCD string as a byte array.
Throws:
Asn1ValueParseException - If invalid characters are in the source string.

TBCDToString

public static java.lang.String TBCDToString(byte[] bcd)
Translates a TBCD string to an ASCII string. The 0xF half-byte is assumed as end of string indicator.

Parameters:
bcd - the source TBCD string
Returns:
the ASCII string.

pow

public static double pow(int base,
                         int exponent)
Raises an integer base to an integer power and returns the result as a double.

Parameters:
base - the base to be raised to the power.
exponent - the power to which base is to be raised.
Returns:
base raised to exponent as a double.

isWhitespace

public static boolean isWhitespace(char ch)
Determine if a specified character is whitespace.

Parameters:
ch - the character to test.
Returns:
true if the character is whitespace, false if it isn't.

isSpaceChar

public static boolean isSpaceChar(char ch)
Determine if a specified character is a space.

Parameters:
ch - the character to test.
Returns:
true if the character is a space, false if it isn't.

firstSortedElement

public static java.lang.String firstSortedElement(java.util.Vector v)
Returns the element of a Vector (that is assumed to contain strings) that is lexicographically the lowest element in the Vector.

Parameters:
v - the Vector to be examined.
Returns:
the lowest value in the Vector as a String.

getKeys

public static java.util.Vector getKeys(java.util.Hashtable h)
Returns a Vector containing the keys (assumed to be strings) of a passed Hashtable.

Parameters:
h - the Hashtable to be examined.
Returns:
a Vector containing the keys.

isLetter

public static boolean isLetter(char c)
Determine if a specified character is a letter.

Parameters:
c - The character to examine.
Returns:
true if the character is a letter, false otherwise.

splitOnWhitespace

public static java.lang.String[] splitOnWhitespace(java.lang.String delimitedString)
Split a string around whitespace characters.

Parameters:
delimitedString - the whitespace-delimited string to parse.
Returns:
an array of Strings with each element being a piece of the original string.