org.jpos.iso
Class ISOUtil

java.lang.Object
  extended by org.jpos.iso.ISOUtil

public class ISOUtil
extends Object

varios functions needed to pack/unpack ISO-8583 fields

Version:
$Id$
Author:
apr@cs.com.uy, Hani S. Kirollos, Alwyn Schoeman
See Also:
ISOComponent

Field Summary
static byte[] ASCII2EBCDIC
           
static byte[] EBCDIC2ASCII
           
static String ENCODING
           
static byte ETX
           
static byte FS
           
static byte GS
           
static String[] hexStrings
           
static byte RS
           
static byte STX
           
static byte US
           
 
Constructor Summary
ISOUtil()
           
 
Method Summary
static byte[] asciiToEbcdic(byte[] a)
           
static byte[] asciiToEbcdic(String s)
           
static void asciiToEbcdic(String s, byte[] e, int offset)
           
static String bcd2str(byte[] b, int offset, int len, boolean padLeft)
          converts a BCD representation of a number to a String
static byte[] bitSet2byte(BitSet b)
          converts a BitSet into a binary field used in pack routines
static byte[] bitSet2byte(BitSet b, int bytes)
          converts a BitSet into a binary field used in pack routines
static byte[] bitSet2extendedByte(BitSet b)
          Converts a BitSet into an extended binary field used in pack routines.
static int bitSet2Int(BitSet bs)
           
static String bitSet2String(BitSet b)
          bit representation of a BitSet suitable for dumps and debugging
static String blankUnPad(String s)
          Right unPad with ' '
static BitSet byte2BitSet(BitSet bmap, byte[] b, int bitOffset)
          Converts a binary representation of a Bitmap field into a Java BitSet
static BitSet byte2BitSet(byte[] b, int offset, boolean bitZeroMeansExtended)
          Converts a binary representation of a Bitmap field into a Java BitSet
static BitSet byte2BitSet(byte[] b, int offset, int maxBits)
          Converts a binary representation of a Bitmap field into a Java BitSet
static String[] commaDecode(String s)
          Decodes a comma encoded String as encoded by commaEncode
static String commaEncode(String[] ss)
          Converts a string[] into a comma-delimited String.
static byte[] concat(byte[] array1, byte[] array2)
          Concatenates two byte arrays (array1 and array2)
static byte[] concat(byte[] array1, int beginIndex1, int length1, byte[] array2, int beginIndex2, int length2)
          Concatenates two byte arrays (array1 and array2)
static String dumpString(byte[] b)
          converts a byte array to printable characters
static String ebcdicToAscii(byte[] e)
           
static String ebcdicToAscii(byte[] e, int offset, int len)
           
static byte[] ebcdicToAsciiBytes(byte[] e)
           
static byte[] ebcdicToAsciiBytes(byte[] e, int offset, int len)
           
static String formatAmount(long l, int len)
          prepare long value used as amount for display (implicit 2 decimals)
static String formatAmountConversionRate(double convRate)
          Format a string containing a amount conversion rate in the proper format

Format: The leftmost digit (i.e., position 1) of this data element denotes the number of positions the decimal separator must be moved from the right.

static String formatDouble(double d, int len)
          format double value
static String[] getHexStrings()
           
static BitSet hex2BitSet(BitSet bmap, byte[] b, int bitOffset)
          Converts an ASCII representation of a Bitmap field into a Java BitSet
static BitSet hex2BitSet(byte[] b, int offset, boolean bitZeroMeansExtended)
          Converts an ASCII representation of a Bitmap field into a Java BitSet
static BitSet hex2BitSet(byte[] b, int offset, int maxBits)
          Converts an ASCII representation of a Bitmap field into a Java BitSet
static byte[] hex2byte(byte[] b, int offset, int len)
           
static byte[] hex2byte(String s)
           
static String hexdump(byte[] b)
           
static String hexdump(byte[] b, int offset, int len)
           
static String hexor(String op1, String op2)
          Bitwise XOR between corresponding byte arrays represented in hex
static String hexString(byte[] b)
          converts a byte array to hex string (suitable for dumps and ASCII packaging of Binary fields
static String hexString(byte[] b, int offset, int len)
          converts a byte array to hex string (suitable for dumps and ASCII packaging of Binary fields
static BitSet int2BitSet(int value)
           
static BitSet int2BitSet(int value, int offset)
           
static boolean isAlphaNumeric(String s)
          Return true if the string is alphanum.
static boolean isBlank(String s)
           
static boolean isNumeric(String s, int radix)
          Return true if the string represent a number in the specified radix.
static boolean isZero(String s)
           
static String millisToString(long millis)
           
static String normalize(String s)
          XML normalizer (default canonical)
static String normalize(String s, boolean canonical)
          XML normalizer
static String padleft(String s, int len, char c)
          pad to the left
static String padright(String s, int len, char c)
          pad to the right
static double parseAmountConversionRate(String convRate)
          Parse currency amount conversion rate string

Suitble for parse fields 10 and 11

static int parseInt(byte[] bArray)
          Converts a byte array to an integer of radix 10.
static int parseInt(byte[] bArray, int radix)
          Converts a byte array to an integer of base radix.
static int parseInt(char[] cArray)
          Converts a character array to an integer of radix 10.
static int parseInt(char[] cArray, int radix)
          Converts a character array to an integer of base radix.
static int parseInt(String s)
          Converts a String to an integer of radix 10.
static int parseInt(String s, int radix)
          Converts a String to an integer of base radix.
static String protect(String s)
          Protects PAN, Track2, CVC (suitable for logs).
static void sleep(long millis)
          Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.
static byte[] str2bcd(String s, boolean padLeft)
          converts to BCD
static byte[] str2bcd(String s, boolean padLeft, byte fill)
          converts to BCD
static byte[] str2bcd(String s, boolean padLeft, byte[] d, int offset)
          converts to BCD
static String strpad(String s, int len)
          pads to the right
static String strpadf(String s, int len)
          pads a string with 'F's (useful for pinoffset management)
static String takeFirstN(String s, int n)
          return the first n characters of the passed String, left padding where required with 0
static String takeLastN(String s, int n)
          return the last n characters of the passed String, left padding where required with 0
static int[] toIntArray(String s)
           
static String[] toStringArray(String s)
           
static byte[] trim(byte[] array, int length)
          Trims a byte[] to a certain length
static String trim(String s)
          trim String (if not null)
static String trimf(String s)
          reverse the effect of strpadf
static String unPadLeft(String s, char c)
          Unpad from left.
static String unPadRight(String s, char c)
          Unpad from right.
static byte[] xor(byte[] op1, byte[] op2)
          Bitwise XOR between corresponding bytes
static String zeropad(long l, int len)
          zeropads a long without throwing an ISOException (performs modulus operation)
static String zeropad(String s, int len)
          left pad with '0'
static String zeropadRight(String s, int len)
           
static String zeroUnPad(String s)
          Left unPad with '0'
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

hexStrings

public static final String[] hexStrings

ENCODING

public static final String ENCODING
See Also:
Constant Field Values

EBCDIC2ASCII

public static final byte[] EBCDIC2ASCII

ASCII2EBCDIC

public static final byte[] ASCII2EBCDIC

STX

public static final byte STX
See Also:
Constant Field Values

FS

public static final byte FS
See Also:
Constant Field Values

US

public static final byte US
See Also:
Constant Field Values

RS

public static final byte RS
See Also:
Constant Field Values

GS

public static final byte GS
See Also:
Constant Field Values

ETX

public static final byte ETX
See Also:
Constant Field Values
Constructor Detail

ISOUtil

public ISOUtil()
Method Detail

getHexStrings

public static String[] getHexStrings()

ebcdicToAscii

public static String ebcdicToAscii(byte[] e)

ebcdicToAscii

public static String ebcdicToAscii(byte[] e,
                                   int offset,
                                   int len)

ebcdicToAsciiBytes

public static byte[] ebcdicToAsciiBytes(byte[] e)

ebcdicToAsciiBytes

public static byte[] ebcdicToAsciiBytes(byte[] e,
                                        int offset,
                                        int len)

asciiToEbcdic

public static byte[] asciiToEbcdic(String s)

asciiToEbcdic

public static byte[] asciiToEbcdic(byte[] a)

asciiToEbcdic

public static void asciiToEbcdic(String s,
                                 byte[] e,
                                 int offset)

padleft

public static String padleft(String s,
                             int len,
                             char c)
                      throws ISOException
pad to the left

Parameters:
s - - original string
len - - desired len
c - - padding char
Returns:
padded string
Throws:
ISOException - on error

padright

public static String padright(String s,
                              int len,
                              char c)
                       throws ISOException
pad to the right

Parameters:
s - - original string
len - - desired len
c - - padding char
Returns:
padded string
Throws:
ISOException - if String's length greater than pad length

trim

public static String trim(String s)
trim String (if not null)

Parameters:
s - String to trim
Returns:
String (may be null)

zeropad

public static String zeropad(String s,
                             int len)
                      throws ISOException
left pad with '0'

Parameters:
s - - original string
len - - desired len
Returns:
zero padded string
Throws:
ISOException - if string's length greater than len

zeropad

public static String zeropad(long l,
                             int len)
zeropads a long without throwing an ISOException (performs modulus operation)

Parameters:
l - the long
len - the length
Returns:
zeropadded value

strpad

public static String strpad(String s,
                            int len)
pads to the right

Parameters:
s - - original string
len - - desired len
Returns:
space padded string

zeropadRight

public static String zeropadRight(String s,
                                  int len)

str2bcd

public static byte[] str2bcd(String s,
                             boolean padLeft,
                             byte[] d,
                             int offset)
converts to BCD

Parameters:
s - - the number
padLeft - - flag indicating left/right padding
d - The byte array to copy into.
offset - Where to start copying into.
Returns:
BCD representation of the number

str2bcd

public static byte[] str2bcd(String s,
                             boolean padLeft)
converts to BCD

Parameters:
s - - the number
padLeft - - flag indicating left/right padding
Returns:
BCD representation of the number

str2bcd

public static byte[] str2bcd(String s,
                             boolean padLeft,
                             byte fill)
converts to BCD

Parameters:
s - - the number
padLeft - - flag indicating left/right padding
fill - - fill value
Returns:
BCD representation of the number

bcd2str

public static String bcd2str(byte[] b,
                             int offset,
                             int len,
                             boolean padLeft)
converts a BCD representation of a number to a String

Parameters:
b - - BCD representation
offset - - starting offset
len - - BCD field len
padLeft - - was padLeft packed?
Returns:
the String representation of the number

hexString

public static String hexString(byte[] b)
converts a byte array to hex string (suitable for dumps and ASCII packaging of Binary fields

Parameters:
b - - byte array
Returns:
String representation

dumpString

public static String dumpString(byte[] b)
converts a byte array to printable characters

Parameters:
b - - byte array
Returns:
String representation

hexString

public static String hexString(byte[] b,
                               int offset,
                               int len)
converts a byte array to hex string (suitable for dumps and ASCII packaging of Binary fields

Parameters:
b - - byte array
offset - - starting position
len - the length
Returns:
String representation

bitSet2String

public static String bitSet2String(BitSet b)
bit representation of a BitSet suitable for dumps and debugging

Parameters:
b - - the BitSet
Returns:
string representing the bits (i.e. 011010010...)

bitSet2byte

public static byte[] bitSet2byte(BitSet b)
converts a BitSet into a binary field used in pack routines

Parameters:
b - - the BitSet
Returns:
binary representation

bitSet2byte

public static byte[] bitSet2byte(BitSet b,
                                 int bytes)
converts a BitSet into a binary field used in pack routines

Parameters:
b - - the BitSet
bytes - - number of bytes to return
Returns:
binary representation

bitSet2Int

public static int bitSet2Int(BitSet bs)

int2BitSet

public static BitSet int2BitSet(int value)

int2BitSet

public static BitSet int2BitSet(int value,
                                int offset)

byte2BitSet

public static BitSet byte2BitSet(byte[] b,
                                 int offset,
                                 boolean bitZeroMeansExtended)
Converts a binary representation of a Bitmap field into a Java BitSet

Parameters:
b - - binary representation
offset - - staring offset
bitZeroMeansExtended - - true for ISO-8583
Returns:
java BitSet object

byte2BitSet

public static BitSet byte2BitSet(byte[] b,
                                 int offset,
                                 int maxBits)
Converts a binary representation of a Bitmap field into a Java BitSet

Parameters:
b - - binary representation
offset - - staring offset
maxBits - - max number of bits (supports 64,128 or 192)
Returns:
java BitSet object

byte2BitSet

public static BitSet byte2BitSet(BitSet bmap,
                                 byte[] b,
                                 int bitOffset)
Converts a binary representation of a Bitmap field into a Java BitSet

Parameters:
bmap - - BitSet
b - - hex representation
bitOffset - - (i.e. 0 for primary bitmap, 64 for secondary)
Returns:
java BitSet object

hex2BitSet

public static BitSet hex2BitSet(byte[] b,
                                int offset,
                                boolean bitZeroMeansExtended)
Converts an ASCII representation of a Bitmap field into a Java BitSet

Parameters:
b - - hex representation
offset - - starting offset
bitZeroMeansExtended - - true for ISO-8583
Returns:
java BitSet object

hex2BitSet

public static BitSet hex2BitSet(byte[] b,
                                int offset,
                                int maxBits)
Converts an ASCII representation of a Bitmap field into a Java BitSet

Parameters:
b - - hex representation
offset - - starting offset
maxBits - - max number of bits (supports 8, 16, 24, 32, 48, 52, 64,.. 128 or 192)
Returns:
java BitSet object

hex2BitSet

public static BitSet hex2BitSet(BitSet bmap,
                                byte[] b,
                                int bitOffset)
Converts an ASCII representation of a Bitmap field into a Java BitSet

Parameters:
bmap - - BitSet
b - - hex representation
bitOffset - - (i.e. 0 for primary bitmap, 64 for secondary)
Returns:
java BitSet object

hex2byte

public static byte[] hex2byte(byte[] b,
                              int offset,
                              int len)
Parameters:
b - source byte array
offset - starting offset
len - number of bytes in destination (processes len*2)
Returns:
byte[len]

hex2byte

public static byte[] hex2byte(String s)
Parameters:
s - source string (with Hex representation)
Returns:
byte array

formatDouble

public static String formatDouble(double d,
                                  int len)
format double value

Parameters:
d - the amount
len - the field len
Returns:
a String of fieldLen characters (right justified)

formatAmount

public static String formatAmount(long l,
                                  int len)
                           throws ISOException
prepare long value used as amount for display (implicit 2 decimals)

Parameters:
l - value
len - display len
Returns:
formated field
Throws:
ISOException

normalize

public static String normalize(String s,
                               boolean canonical)
XML normalizer

Parameters:
s - source String
canonical - true if we want to normalize \r and \n as well
Returns:
normalized string suitable for XML Output

normalize

public static String normalize(String s)
XML normalizer (default canonical)

Parameters:
s - source String
Returns:
normalized string suitable for XML Output

protect

public static String protect(String s)
Protects PAN, Track2, CVC (suitable for logs).
 "40000101010001" is converted to "400001____0001"
 "40000101010001=020128375" is converted to "400001____0001=0201_____"
 "40000101010001D020128375" is converted to "400001____0001D0201_____"
 "123" is converted to "___"
 

Parameters:
s - string to be protected
Returns:
'protected' String

toIntArray

public static int[] toIntArray(String s)

toStringArray

public static String[] toStringArray(String s)

xor

public static byte[] xor(byte[] op1,
                         byte[] op2)
Bitwise XOR between corresponding bytes

Parameters:
op1 - byteArray1
op2 - byteArray2
Returns:
an array of length = the smallest between op1 and op2

hexor

public static String hexor(String op1,
                           String op2)
Bitwise XOR between corresponding byte arrays represented in hex

Parameters:
op1 - hexstring 1
op2 - hexstring 2
Returns:
an array of length = the smallest between op1 and op2

trim

public static byte[] trim(byte[] array,
                          int length)
Trims a byte[] to a certain length

Parameters:
array - the byte[] to be trimmed
length - the wanted length
Returns:
the trimmed byte[]

concat

public static byte[] concat(byte[] array1,
                            byte[] array2)
Concatenates two byte arrays (array1 and array2)

Parameters:
array1 -
array2 -
Returns:
the concatenated array

concat

public static byte[] concat(byte[] array1,
                            int beginIndex1,
                            int length1,
                            byte[] array2,
                            int beginIndex2,
                            int length2)
Concatenates two byte arrays (array1 and array2)

Parameters:
array1 -
beginIndex1 -
length1 -
array2 -
beginIndex2 -
length2 -
Returns:
the concatenated array

sleep

public static void sleep(long millis)
Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds. The thread does not lose ownership of any monitors. This is the same as Thread.sleep () without throwing InterruptedException

Parameters:
millis - the length of time to sleep in milliseconds.

zeroUnPad

public static String zeroUnPad(String s)
Left unPad with '0'

Parameters:
s - - original string
Returns:
zero unPadded string

blankUnPad

public static String blankUnPad(String s)
Right unPad with ' '

Parameters:
s - - original string
Returns:
blank unPadded string

unPadRight

public static String unPadRight(String s,
                                char c)
Unpad from right.

Parameters:
s - - original string
c - - padding char
Returns:
unPadded string.

unPadLeft

public static String unPadLeft(String s,
                               char c)
Unpad from left.

Parameters:
s - - original string
c - - padding char
Returns:
unPadded string.

isZero

public static boolean isZero(String s)
Returns:
true if the string is zero-filled ( 0 char filled )

isBlank

public static boolean isBlank(String s)
Returns:
true if the string is blank filled (space char filled)

isAlphaNumeric

public static boolean isAlphaNumeric(String s)
Return true if the string is alphanum. {letter digit (.) (_) (-) ( ) (?) }


isNumeric

public static boolean isNumeric(String s,
                                int radix)
Return true if the string represent a number in the specified radix.


bitSet2extendedByte

public static byte[] bitSet2extendedByte(BitSet b)
Converts a BitSet into an extended binary field used in pack routines. The result is always in the extended format: (16 bytes of length)

Parameters:
b - the BitSet
Returns:
binary representation

parseInt

public static int parseInt(String s,
                           int radix)
                    throws NumberFormatException
Converts a String to an integer of base radix.

String constraints are:
  • Number must be less than 10 digits
  • Number must be positive
  • Parameters:
    s - String representation of number
    radix - Number base to use
    Returns:
    integer value of number
    Throws:
    NumberFormatException

    parseInt

    public static int parseInt(String s)
                        throws NumberFormatException
    Converts a String to an integer of radix 10.

    String constraints are:
  • Number must be less than 10 digits
  • Number must be positive
  • Parameters:
    s - String representation of number
    Returns:
    integer value of number
    Throws:
    NumberFormatException

    parseInt

    public static int parseInt(char[] cArray,
                               int radix)
                        throws NumberFormatException
    Converts a character array to an integer of base radix.

    Array constraints are:
  • Number must be less than 10 digits
  • Number must be positive
  • Parameters:
    cArray - Character Array representation of number
    radix - Number base to use
    Returns:
    integer value of number
    Throws:
    NumberFormatException

    parseInt

    public static int parseInt(char[] cArray)
                        throws NumberFormatException
    Converts a character array to an integer of radix 10.

    Array constraints are:
  • Number must be less than 10 digits
  • Number must be positive
  • Parameters:
    cArray - Character Array representation of number
    Returns:
    integer value of number
    Throws:
    NumberFormatException

    parseInt

    public static int parseInt(byte[] bArray,
                               int radix)
                        throws NumberFormatException
    Converts a byte array to an integer of base radix.

    Array constraints are:
  • Number must be less than 10 digits
  • Number must be positive
  • Parameters:
    bArray - Byte Array representation of number
    radix - Number base to use
    Returns:
    integer value of number
    Throws:
    NumberFormatException

    parseInt

    public static int parseInt(byte[] bArray)
                        throws NumberFormatException
    Converts a byte array to an integer of radix 10.

    Array constraints are:
  • Number must be less than 10 digits
  • Number must be positive
  • Parameters:
    bArray - Byte Array representation of number
    Returns:
    integer value of number
    Throws:
    NumberFormatException

    hexdump

    public static String hexdump(byte[] b)
    Parameters:
    b - a byte[] buffer
    Returns:
    hexdump

    hexdump

    public static String hexdump(byte[] b,
                                 int offset,
                                 int len)
    Parameters:
    b - a byte[] buffer
    offset - starting offset
    len - the Length
    Returns:
    hexdump

    strpadf

    public static String strpadf(String s,
                                 int len)
    pads a string with 'F's (useful for pinoffset management)

    Parameters:
    s - an [hex]string
    len - desired length
    Returns:
    string right padded with 'F's

    trimf

    public static String trimf(String s)
    reverse the effect of strpadf

    Parameters:
    s - F padded string
    Returns:
    trimmed string

    takeLastN

    public static String takeLastN(String s,
                                   int n)
                            throws ISOException
    return the last n characters of the passed String, left padding where required with 0

    Parameters:
    s - String to take from
    n - nuber of characters to take
    Returns:
    String (may be null)
    Throws:
    ISOException

    takeFirstN

    public static String takeFirstN(String s,
                                    int n)
                             throws ISOException
    return the first n characters of the passed String, left padding where required with 0

    Parameters:
    s - String to take from
    n - nuber of characters to take
    Returns:
    String (may be null)
    Throws:
    ISOException

    millisToString

    public static String millisToString(long millis)

    formatAmountConversionRate

    public static String formatAmountConversionRate(double convRate)
                                             throws ISOException
    Format a string containing a amount conversion rate in the proper format

    Format: The leftmost digit (i.e., position 1) of this data element denotes the number of positions the decimal separator must be moved from the right. Positions 2–8 of this data element specify the rate. For example, a conversion rate value of 91234567 in this data element would equate to 0.001234567.

    Parameters:
    convRate - - amount conversion rate
    Returns:
    a string containing a amount conversion rate in the proper format, witch is suitable for create fields 10 and 11
    Throws:
    ISOException

    parseAmountConversionRate

    public static double parseAmountConversionRate(String convRate)
    Parse currency amount conversion rate string

    Suitble for parse fields 10 and 11

    Parameters:
    convRate - amount conversation rate
    Returns:
    parsed currency amount conversation rate
    Throws:
    IllegalArgumentException

    commaEncode

    public static String commaEncode(String[] ss)
    Converts a string[] into a comma-delimited String. Takes care of escaping commas using a backlash

    Parameters:
    ss - string array to be comma encoded
    Returns:
    comma encoded string
    See Also:
    commaDecode(String)

    commaDecode

    public static String[] commaDecode(String s)
    Decodes a comma encoded String as encoded by commaEncode

    Parameters:
    s - the command encoded String
    Returns:
    String[]
    See Also:
    commaEncode(String[])


    Copyright © 1998-2012 jPOS.org. All Rights Reserved.