⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 isoutil.java

📁 POS is a Java&#174 platform-based, mission-critical, ISO-8583 based financial transaction library/fr
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     * @param array the byte[] to be trimmed     * @param length the wanted length     * @return the trimmed byte[]     */    public static byte[] trim (byte[] array, int length) {        byte[] trimmedArray = new byte[length];        System.arraycopy(array, 0, trimmedArray, 0, length);        return  trimmedArray;    }    /**     * Concatenates two byte arrays (array1 and array2)     * @param array1     * @param array2     * @return the concatenated array     */    public static byte[] concat (byte[] array1, byte[] array2) {        byte[] concatArray = new byte[array1.length + array2.length];        System.arraycopy(array1, 0, concatArray, 0, array1.length);        System.arraycopy(array2, 0, concatArray, array1.length, array2.length);        return  concatArray;    }    /**     * Concatenates two byte arrays (array1 and array2)     * @param array1     * @param beginIndex1     * @param length1     * @param array2     * @param beginIndex2     * @param length2     * @return the concatenated array     */    public static byte[] concat (byte[] array1, int beginIndex1, int length1, byte[] array2,            int beginIndex2, int length2) {        byte[] concatArray = new byte[length1 + length2];        System.arraycopy(array1, beginIndex1, concatArray, 0, length1);        System.arraycopy(array2, beginIndex2, concatArray, length1, length2);        return  concatArray;    }    /**      * 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     *     * @param      millis   the length of time to sleep in milliseconds.     */    public static void sleep (long millis) {        try {            Thread.sleep (millis);        } catch (InterruptedException e) { }    }    /**     * Left unPad with '0'     * @param s - original string     * @return zero unPadded string     */    public static String zeroUnPad( String s ) {        return unPadLeft(s, '0');    }    /**     * Right unPad with ' '     * @param s - original string     * @return blank unPadded string     */    public static String blankUnPad( String s ) {        return unPadRight(s, ' ');    }    /**     * Unpad from right.      * @param s - original string     * @param c - padding char     * @return unPadded string.     */    public static String unPadRight(String s, char c) {        int end = s.length();        if (end == 0)            return s;        while ( ( 0 < end) && (s.charAt(end-1) == c) ) end --;        return ( 0 < end )? s.substring( 0, end ): s.substring( 0, 1 );    }    /**     * Unpad from left.      * @param s - original string     * @param c - padding char     * @return unPadded string.     */    public static String unPadLeft(String s, char c) {        int fill = 0, end = s.length();        if (end == 0)            return s;        while ( (fill < end) && (s.charAt(fill) == c) ) fill ++;        return ( fill < end )? s.substring( fill, end ): s.substring( fill-1, end );    }    /**     * @return true if the string is zero-filled ( 0 char filled )     **/    public static boolean isZero( String s ) {        int i = 0, len = s.length();        while ( i < len && ( s.charAt( i ) == '0' ) ){            i++;        }        return ( i >= len );    }    /**     * @return true if the string is blank filled (space char filled)     */    public static boolean isBlank( String s ){        return (s.trim().length() == 0);    }    /**     * Return true if the string is alphanum.     * <code>{letter digit (.) (_) (-) ( ) (?) }</code>     *      **/    public static boolean isAlphaNumeric ( String s ) {        int i = 0, len = s.length();        while ( i < len && ( Character.isLetterOrDigit( s.charAt( i ) ) ||                             s.charAt( i ) == ' ' || s.charAt( i ) == '.' ||                             s.charAt( i ) == '-' || s.charAt( i ) == '_' )                             || s.charAt( i ) == '?' ){            i++;        }        return ( i >= len );    }    /**     * Return true if the string represent a number     * in the specified radix.     * <br><br>     **/    public static boolean isNumeric ( String s, int radix ) {        int i = 0, len = s.length();        while ( i < len && Character.digit( s.charAt( i ), radix ) > -1  ){            i++;        }        return ( i >= len );    }    /**     * Converts a BitSet into an extended binary field     * used in pack routines. The result is always in the     * extended format: (16 bytes of length)     * <br><br>     * @param b the BitSet     * @return binary representation     */    public static byte[] bitSet2extendedByte ( BitSet b ){        int len = 128;        byte[] d = new byte[len >> 3];        for ( int i=0; i<len; i++ )            if (b.get(i+1))                d[i >> 3] |= (0x80 >> (i % 8));        d[0] |= 0x80;        return d;    }    /**     * Converts a String to an integer of base radix.     * <br><br>     * String constraints are:     * <li>Number must be less than 10 digits</li>     * <li>Number must be positive</li>     * @param s String representation of number     * @param radix Number base to use     * @return integer value of number     * @throws NumberFormatException     */    public static int parseInt (String s, int radix) throws NumberFormatException {        int length = s.length();        if (length > 9)            throw new NumberFormatException ("Number can have maximum 9 digits");        int result = 0;        int index = 0;        int digit = Character.digit (s.charAt(index++), radix);        if (digit == -1)            throw new NumberFormatException ("String contains non-digit");        result = digit;        while (index < length) {            result *= radix;            digit = Character.digit (s.charAt(index++), radix);            if (digit == -1)                throw new NumberFormatException ("String contains non-digit");            result += digit;        }        return result;    }    /**     * Converts a String to an integer of radix 10.     * <br><br>     * String constraints are:     * <li>Number must be less than 10 digits</li>     * <li>Number must be positive</li>     * @param s String representation of number     * @return integer value of number     * @throws NumberFormatException     */    public static int parseInt (String s) throws NumberFormatException {        return parseInt (s, 10);    }    /**     * Converts a character array to an integer of base radix.     * <br><br>     * Array constraints are:     * <li>Number must be less than 10 digits</li>     * <li>Number must be positive</li>     * @param cArray Character Array representation of number     * @param radix Number base to use     * @return integer value of number     * @throws NumberFormatException     */    public static int parseInt (char[] cArray, int radix) throws NumberFormatException {        int length = cArray.length;        if (length > 9)            throw new NumberFormatException ("Number can have maximum 9 digits");        int result = 0;        int index = 0;        int digit = Character.digit(cArray[index++], radix);        if (digit == -1)            throw new NumberFormatException ("Char array contains non-digit");        result = digit;        while (index < length) {            result *= radix;            digit = Character.digit(cArray[index++],radix);            if (digit == -1)                throw new NumberFormatException ("Char array contains non-digit");            result += digit;        }        return result;    }    /**     * Converts a character array to an integer of radix 10.     * <br><br>     * Array constraints are:     * <li>Number must be less than 10 digits</li>     * <li>Number must be positive</li>     * @param cArray Character Array representation of number     * @return integer value of number     * @throws NumberFormatException     */    public static int parseInt (char[] cArray) throws NumberFormatException {        return parseInt (cArray,10);    }    /**     * Converts a byte array to an integer of base radix.     * <br><br>     * Array constraints are:     * <li>Number must be less than 10 digits</li>     * <li>Number must be positive</li>     * @param bArray Byte Array representation of number     * @param radix Number base to use     * @return integer value of number     * @throws NumberFormatException     */    public static int parseInt (byte[] bArray, int radix) throws NumberFormatException {        int length = bArray.length;        if (length > 9)            throw new NumberFormatException ("Number can have maximum 9 digits");        int result = 0;        int index = 0;        int digit = Character.digit((char)bArray[index++], radix);        if (digit == -1)            throw new NumberFormatException ("Byte array contains non-digit");        result = digit;        while (index < length) {            result *= radix;            digit = Character.digit((char)bArray[index++],radix);            if (digit == -1)                throw new NumberFormatException ("Byte array contains non-digit");            result += digit;        }        return result;    }    /**     * Converts a byte array to an integer of radix 10.     * <br><br>     * Array constraints are:     * <li>Number must be less than 10 digits</li>     * <li>Number must be positive</li>     * @param bArray Byte Array representation of number     * @return integer value of number     * @throws NumberFormatException     */    public static int parseInt (byte[] bArray) throws NumberFormatException {        return parseInt (bArray,10);    }    private static String hexOffset (int i) {        i = (i>>4) << 4;        int w = i > 0xFFFF ? 8 : 4;        try {            return zeropad (Integer.toString (i, 16), w);        } catch (ISOException e) {            // should not happen            return e.getMessage();        }    }    /**     * @param b a byte[] buffer     * @return hexdump     */    public static String hexdump (byte[] b) {        return hexdump (b, 0, b.length);    }    /**     * @param b a byte[] buffer     * @param offset starting offset     * @param len the Length     * @return hexdump     */    public static String hexdump (byte[] b, int offset, int len) {        StringBuffer sb    = new StringBuffer ();        StringBuffer hex   = new StringBuffer ();        StringBuffer ascii = new StringBuffer ();        String sep         = "  ";        String lineSep     = System.getProperty ("line.separator");        for (int i=offset; i<len; i++) {            char hi = Character.forDigit ((b[i] >> 4) & 0x0F, 16);            char lo = Character.forDigit (b[i] & 0x0F, 16);            hex.append (Character.toUpperCase(hi));            hex.append (Character.toUpperCase(lo));            hex.append (' ');            char c = (char) b[i];            ascii.append ((c >= 32 && c < 127) ? c : '.');            int j = i % 16;            switch (j) {                case 7 :                    hex.append (' ');                    break;                case 15 :                    sb.append (hexOffset (i));                    sb.append (sep);                    sb.append (hex.toString());                    sb.append (' ');                    sb.append (ascii.toString());                    sb.append (lineSep);                    hex   = new StringBuffer ();                    ascii = new StringBuffer ();                    break;            }        }        if (hex.length() > 0) {            while (hex.length () < 49)                hex.append (' ');            sb.append (hexOffset (len));            sb.append (sep);            sb.append (hex.toString());            sb.append (' ');            sb.append (ascii.toString());            sb.append (lineSep);        }        return sb.toString();    }    /**     * pads a string with 'F's (useful for pinoffset management)     * @param s an [hex]string     * @param len desired length     * @return string right padded with 'F's     */    public static String strpadf (String s, int len) {        StringBuffer d = new StringBuffer(s);        while (d.length() < len)            d.append('F');        return d.toString();    }    /**     * reverse the effect of strpadf     * @param s F padded string     * @return trimmed string     */    public static String trimf (String s) {        if (s != null) {            int l = s.length();            if (l > 0) {                while (--l >= 0) {                    if (s.charAt (l) != 'F')                        break;                }                s = l == 0 ? "" : s.substring (0, l+1);            }        }        return s;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -