stringutils.java

来自「开源项目openfire的完整源程序」· Java 代码 · 共 2,677 行 · 第 1/5 页

JAVA
2,677
字号
                if (i > last) {

                    out.append(input, last, i - last);

                }

                last = i + 1;

                out.append(LT_ENCODE);

            }
            else if (ch == '>') {

                if (i > last) {

                    out.append(input, last, i - last);

                }

                last = i + 1;

                out.append(GT_ENCODE);

            }
            else if (ch == '"') {

                if (i > last) {

                    out.append(input, last, i - last);

                }

                last = i + 1;

                out.append(QUOTE_ENCODE);

            }

        }

        if (last == 0) {

            return in;

        }

        if (i > last) {

            out.append(input, last, i - last);

        }

        return out.toString();

    }


    /**
     * Used by the hash method.
     */

    private static MessageDigest digest = null;


    /**
     * Hashes a String using the Md5 algorithm and returns the result as a
     * <p/>
     * String of hexadecimal numbers. This method is synchronized to avoid
     * <p/>
     * excessive MessageDigest object creation. If calling this method becomes
     * <p/>
     * a bottleneck in your code, you may wish to maintain a pool of
     * <p/>
     * MessageDigest objects instead of using this method.
     * <p/>
     * <p/>
     * <p/>
     * A hash is a one-way function -- that is, given an
     * <p/>
     * input, an output is easily computed. However, given the output, the
     * <p/>
     * input is almost impossible to compute. This is useful for passwords
     * <p/>
     * since we can store the hash and a hacker will then have a very hard time
     * <p/>
     * determining the original password.
     * <p/>
     * <p/>
     * <p/>
     * In Jive, every time a user logs in, we simply
     * <p/>
     * take their plain text password, compute the hash, and compare the
     * <p/>
     * generated hash to the stored hash. Since it is almost impossible that
     * <p/>
     * two passwords will generate the same hash, we know if the user gave us
     * <p/>
     * the correct password or not. The only negative to this system is that
     * <p/>
     * password recovery is basically impossible. Therefore, a reset password
     * <p/>
     * method is used instead.
     *
     * @param data the String to compute the hash of.
     * @return a hashed version of the passed-in String
     */

    public synchronized static final String hash(String data) {

        if (digest == null) {

            try {

                digest = MessageDigest.getInstance("MD5");

            }

            catch (NoSuchAlgorithmException nsae) {

            }

        }

        // Now, compute hash.

        try {

            digest.update(data.getBytes("utf-8"));

        }

        catch (UnsupportedEncodingException e) {

        }

        return encodeHex(digest.digest());

    }

    public synchronized static final String hash(byte[] data) {

        if (digest == null) {

            try {

                digest = MessageDigest.getInstance("MD5");

            }

            catch (NoSuchAlgorithmException nsae) {

            }

        }

        // Now, compute hash.


        digest.update(data);

        return encodeHex(digest.digest());

    }


    /**
     * Turns an array of bytes into a String representing each byte as an
     * <p/>
     * unsigned hex number.
     * <p/>
     * <p/>
     * <p/>
     * Method by Santeri Paavolainen, Helsinki Finland 1996<br>
     * <p/>
     * (c) Santeri Paavolainen, Helsinki Finland 1996<br>
     * <p/>
     * Distributed under LGPL.
     *
     * @param bytes an array of bytes to convert to a hex-string
     * @return generated hex string
     */

    public static final String encodeHex(byte[] bytes) {

        StringBuffer buf = new StringBuffer(bytes.length * 2);

        int i;


        for (i = 0; i < bytes.length; i++) {

            if (((int)bytes[i] & 0xff) < 0x10) {

                buf.append("0");

            }

            buf.append(Long.toString((int)bytes[i] & 0xff, 16));

        }

        return buf.toString();

    }


    /**
     * Turns a hex encoded string into a byte array. It is specifically meant
     * <p/>
     * to "reverse" the toHex(byte[]) method.
     *
     * @param hex a hex encoded String to transform into a byte array.
     * @return a byte array representing the hex String[
     */

    public static final byte[] decodeHex(String hex) {

        char [] chars = hex.toCharArray();

        byte[] bytes = new byte[chars.length / 2];

        int byteCount = 0;

        for (int i = 0; i < chars.length; i += 2) {

            int newByte = 0x00;

            newByte |= hexCharToByte(chars[i]);

            newByte <<= 4;

            newByte |= hexCharToByte(chars[i + 1]);

            bytes[byteCount] = (byte)newByte;

            byteCount++;

        }

        return bytes;

    }


    /**
     * Returns the the byte value of a hexadecmical char (0-f). It's assumed
     * <p/>
     * that the hexidecimal chars are lower case as appropriate.
     *
     * @param ch a hexedicmal character (0-f)
     * @return the byte value of the character (0x00-0x0F)
     */

    private static final byte hexCharToByte(char ch) {

        switch (ch) {

            case '0':
                return 0x00;

            case '1':
                return 0x01;

            case '2':
                return 0x02;

            case '3':
                return 0x03;

            case '4':
                return 0x04;

            case '5':
                return 0x05;

            case '6':
                return 0x06;

            case '7':
                return 0x07;

            case '8':
                return 0x08;

            case '9':
                return 0x09;

            case 'a':
                return 0x0A;

            case 'b':
                return 0x0B;

            case 'c':
                return 0x0C;

            case 'd':
                return 0x0D;

            case 'e':
                return 0x0E;

            case 'f':
                return 0x0F;

        }

        return 0x00;

    }

    //*********************************************************************

    //* Base64 - a simple base64 encoder and decoder.

    //*

    //*     Copyright (c) 1999, Bob Withers - bwit@pobox.com

    //*

    //* This code may be freely used for any purpose, either personal

    //* or commercial, provided the authors copyright notice remains

    //* intact.

    //*********************************************************************


    /**
     * Encodes a String as a base64 String.
     *
     * @param data a String to encode.
     * @return a base64 encoded String.
     */

    public static String encodeBase64(String data) {

        byte [] bytes = null;

        try {

            bytes = data.getBytes("ISO-8859-1");

        }

        catch (UnsupportedEncodingException uee) {

        }

        return encodeBase64(bytes);

    }


    /**
     * Encodes a byte array into a base64 String.
     *
     * @param data a byte array to encode.
     * @return a base64 encode String.
     */

    public static String encodeBase64(byte[] data) {

        int c;

        int len = data.length;

        StringBuffer ret = new StringBuffer(((len / 3) + 1) * 4);

        for (int i = 0; i < len; ++i) {

            c = (data[i] >> 2) & 0x3f;

            ret.append(cvt.charAt(c));

            c = (data[i] << 4) & 0x3f;

            if (++i < len)

                c |= (data[i] >> 4) & 0x0f;


            ret.append(cvt.charAt(c));

            if (i < len) {

                c = (data[i] << 2) & 0x3f;

                if (++i < len)

                    c |= (data[i] >> 6) & 0x03;


                ret.append(cvt.charAt(c));

            }

            else {

                ++i;

                ret.append((char)fillchar);

            }


            if (i < len) {

                c = data[i] & 0x3f;

                ret.append(cvt.charAt(c));

            }

            else {

                ret.append((char)fillchar);

            }

        }

        return ret.toString();

    }


    /**
     * Decodes a base64 String.
     *
     * @param data a base64 encoded String to decode.
     * @return the decoded String.
     */

    public static String decodeBase64(String data) {

        byte [] bytes = null;

        try {

            bytes = data.getBytes("ISO-8859-1");

        }

        catch (UnsupportedEncodingException uee) {

        }

        return decodeBase64(bytes);

    }


    /**
     * Decodes a base64 aray of bytes.
     *
     * @param data a base64 encode byte array to decode.
     * @return the decoded String.
     */

    public static String decodeBase64(byte[] data) {

        int c, c1;

        int len = data.length;

        StringBuffer ret = new StringBuffer((len * 3) / 4);

        for (int i = 0; i < len; ++i) {

            c = cvt.indexOf(data[i]);

            ++i;

            c1 = cvt.indexOf(data[i]);

            c = ((c << 2) | ((c1 >> 4) & 0x3));

            ret.append((char)c);

            if (++i < len) {

                c = data[i];

                if (fillchar == c)

                    break;


                c = cvt.indexOf(c);

                c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf);

                ret.append((char)c1);

            }


            if (++i < len) {

                c1 = data[i];

                if (fillchar == c1)

                    break;


                c1 = cvt.indexOf(c1);

                c = ((c << 6) & 0xc0) | c1;

                ret.append((char)c);

            }

        }

        return ret.toString();

    }


    /**
     * The method below is under the following license
     * <p/>
     * <p/>
     * <p/>
     * ====================================================================
     * <p/>
     * <p/>
     * <p/>
     * The Apache Software License, Version 1.1
     * <p/>
     * <p/>

⌨️ 快捷键说明

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