package com.ruoyi.ext.api.utils;

import java.io.*;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.BreakIterator;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class StringUtilsN {
    static public int unicodetype=-2;
    public StringUtilsN() {


    }

    public static final String replace(String line, String oldString, String newString) {
        if (line == null)
            return null;
        int i = 0;
        if ((i = line.indexOf(oldString, i)) >= 0) {
            char line2[] = line.toCharArray();
            char newString2[] = newString.toCharArray();
            int oLength = oldString.length();
            StringBuffer buf = new StringBuffer(line2.length);
            buf.append(line2, 0, i).append(newString2);
            i += oLength;
            int j;
            for (j = i; (i = line.indexOf(oldString, i)) > 0; j = i) {
                buf.append(line2, j, i - j).append(newString2);
                i += oLength;
            }

            buf.append(line2, j, line2.length - j);
            return buf.toString();
        } else {
            return line;
        }
    }

    public static final String replaceIgnoreCase(String line, String oldString, String newString) {
        if (line == null)
            return null;
        String lcLine = line.toLowerCase();
        String lcOldString = oldString.toLowerCase();
        int i = 0;
        if ((i = lcLine.indexOf(lcOldString, i)) >= 0) {
            char line2[] = line.toCharArray();
            char newString2[] = newString.toCharArray();
            int oLength = oldString.length();
            StringBuffer buf = new StringBuffer(line2.length);
            buf.append(line2, 0, i).append(newString2);
            i += oLength;
            int j;
            for (j = i; (i = lcLine.indexOf(lcOldString, i)) > 0; j = i) {
                buf.append(line2, j, i - j).append(newString2);
                i += oLength;
            }

            buf.append(line2, j, line2.length - j);
            return buf.toString();
        } else {
            return line;
        }
    }

    public static final String replaceIgnoreCase(String line, String oldString, String newString, int count[]) {
        if (line == null)
            return null;
        String lcLine = line.toLowerCase();
        String lcOldString = oldString.toLowerCase();
        int i = 0;
        if ((i = lcLine.indexOf(lcOldString, i)) >= 0) {
            int counter = 0;
            char line2[] = line.toCharArray();
            char newString2[] = newString.toCharArray();
            int oLength = oldString.length();
            StringBuffer buf = new StringBuffer(line2.length);
            buf.append(line2, 0, i).append(newString2);
            i += oLength;
            int j;
            for (j = i; (i = lcLine.indexOf(lcOldString, i)) > 0; j = i) {
                counter++;
                buf.append(line2, j, i - j).append(newString2);
                i += oLength;
            }

            buf.append(line2, j, line2.length - j);
            count[0] = counter;
            return buf.toString();
        } else {
            return line;
        }
    }

    public static final String replace(String line, String oldString, String newString, int count[]) {
        if (line == null)
            return null;
        int i = 0;
        if ((i = line.indexOf(oldString, i)) >= 0) {
            int counter = 0;
            counter++;
            char line2[] = line.toCharArray();
            char newString2[] = newString.toCharArray();
            int oLength = oldString.length();
            StringBuffer buf = new StringBuffer(line2.length);
            buf.append(line2, 0, i).append(newString2);
            i += oLength;
            int j;
            for (j = i; (i = line.indexOf(oldString, i)) > 0; j = i) {
                counter++;
                buf.append(line2, j, i - j).append(newString2);
                i += oLength;
            }

            buf.append(line2, j, line2.length - j);
            count[0] = counter;
            return buf.toString();
        } else {
            return line;
        }
    }

    public static final String escapeHTMLTags(String in) {
        if (in == null)
            return null;
        int i = 0;
        int last = 0;
        char input[] = in.toCharArray();
        int len = input.length;
        StringBuffer out = new StringBuffer((int) ((double) len * 1.3D));
        for (; i < len; i++) {
            char ch = input[i];
            if (ch > '>')
                continue;
            if (ch == '<') {
                if (i > last)
                    out.append(input, last, i - last);
                last = i + 1;
                out.append(LT_ENCODE);
                continue;
            }
            if (ch != '>')
                continue;
            if (i > last)
                out.append(input, last, i - last);
            last = i + 1;
            out.append(GT_ENCODE);
        }

        if (last == 0)
            return in;
        if (i > last)
            out.append(input, last, i - last);
        return out.toString();
    }

    public static final synchronized String hash(String data) {
        if (digest == null)
            try {
                digest = MessageDigest.getInstance("MD5");
            }
            catch (NoSuchAlgorithmException nsae) {
                System.err.println("Failed to load the MD5 MessageDigest. We will be unable to function normally.");
                nsae.printStackTrace();
            }
        digest.update(data.getBytes());
        return encodeHex(digest.digest());
    }

    public static final String encodeHex(byte bytes[]) {
        StringBuffer buf = new StringBuffer(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            if ((bytes[i] & 0xff) < 16)
                buf.append("0");
            buf.append(Long.toString(bytes[i] & 0xff, 16));
        }

        return buf.toString();
    }

    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) {
            byte newByte = 0;
            newByte |= hexCharToByte(chars[i]);
            newByte <<= 4;
            newByte |= hexCharToByte(chars[i + 1]);
            bytes[byteCount] = newByte;
            byteCount++;
        }

        return bytes;
    }

    private static final byte hexCharToByte(char ch) {
        switch (ch) {
            case 48: // '0'
                return 0;

            case 49: // '1'
                return 1;

            case 50: // '2'
                return 2;

            case 51: // '3'
                return 3;

            case 52: // '4'
                return 4;

            case 53: // '5'
                return 5;

            case 54: // '6'
                return 6;

            case 55: // '7'
                return 7;

            case 56: // '8'
                return 8;

            case 57: // '9'
                return 9;

            case 97: // 'a'
                return 10;

            case 98: // 'b'
                return 11;

            case 99: // 'c'
                return 12;

            case 100: // 'd'
                return 13;

            case 101: // 'e'
                return 14;

            case 102: // 'f'
                return 15;

            case 58: // ':'
            case 59: // ';'
            case 60: // '<'
            case 61: // '='
            case 62: // '>'
            case 63: // '?'
            case 64: // '@'
            case 65: // 'A'
            case 66: // 'B'
            case 67: // 'C'
            case 68: // 'D'
            case 69: // 'E'
            case 70: // 'F'
            case 71: // 'G'
            case 72: // 'H'
            case 73: // 'I'
            case 74: // 'J'
            case 75: // 'K'
            case 76: // 'L'
            case 77: // 'M'
            case 78: // 'N'
            case 79: // 'O'
            case 80: // 'P'
            case 81: // 'Q'
            case 82: // 'R'
            case 83: // 'S'
            case 84: // 'T'
            case 85: // 'U'
            case 86: // 'V'
            case 87: // 'W'
            case 88: // 'X'
            case 89: // 'Y'
            case 90: // 'Z'
            case 91: // '['
            case 92: // '\\'
            case 93: // ']'
            case 94: // '^'
            case 95: // '_'
            case 96: // '`'
            default:
                return 0;
        }
    }

    public static String encodeBase64(String data) {
        return encodeBase64(data.getBytes());
    }

    public static String encodeBase64(byte data[]) {
        int len = data.length;
        StringBuffer ret = new StringBuffer((len / 3 + 1) * 4);
        for (int i = 0; i < len; i++) {
            int c = data[i] >> 2 & 0x3f;
            ret.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(c));
            c = data[i] << 4 & 0x3f;
            if (++i < len)
                c |= data[i] >> 4 & 0xf;
            ret.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(c));
            if (i < len) {
                c = data[i] << 2 & 0x3f;
                if (++i < len)
                    c |= data[i] >> 6 & 3;
                ret.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(c));
            } else {
                i++;
                ret.append('=');
            }
            if (i < len) {
                c = data[i] & 0x3f;
                ret.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(c));
            } else {
                ret.append('=');
            }
        }

        return ret.toString();
    }

    public static String decodeBase64(String data) {
        return decodeBase64(data.getBytes());
    }

    public static String decodeBase64(byte data[]) {
        int len = data.length;
        StringBuffer ret = new StringBuffer((len * 3) / 4);
        for (int i = 0; i < len; i++) {
            int c = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".indexOf(data[i]);
            i++;
            int c1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".indexOf(data[i]);
            c = c << 2 | c1 >> 4 & 3;
            ret.append((char) c);
            if (++i < len) {
                c = data[i];
                if (61 == c)
                    break;
                c = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".indexOf((char) c);
                c1 = c1 << 4 & 0xf0 | c >> 2 & 0xf;
                ret.append((char) c1);
            }
            if (++i >= len)
                continue;
            c1 = data[i];
            if (61 == c1)
                break;
            c1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".indexOf((char) c1);
            c = c << 6 & 0xc0 | c1;
            ret.append((char) c);
        }

        return ret.toString();
    }

    public static final String[] toLowerCaseWordArray(String text) {
        if (text == null || text.length() == 0)
            return new String[0];
        ArrayList wordList = new ArrayList();
        BreakIterator boundary = BreakIterator.getWordInstance();
        boundary.setText(text);
        int start = 0;
        for (int end = boundary.next(); end != -1; end = boundary.next()) {
            String tmp = text.substring(start, end).trim();
            tmp = replace(tmp, "+", "");
            tmp = replace(tmp, "/", "");
            tmp = replace(tmp, "\\", "");
            tmp = replace(tmp, "#", "");
            tmp = replace(tmp, "*", "");
            tmp = replace(tmp, ")", "");
            tmp = replace(tmp, "(", "");
            tmp = replace(tmp, "&", "");
            if (tmp.length() > 0)
                wordList.add(tmp);
            start = end;
        }

        return (String[]) (String[]) wordList.toArray(new String[wordList.size()]);
    }

    public static final String randomString(int length) {
        if (length < 1)
            return null;
        char randBuffer[] = new char[length];
        for (int i = 0; i < randBuffer.length; i++)
            randBuffer[i] = numbersAndLetters[randGen.nextInt(71)];

        return new String(randBuffer);
    }

    public static final String chopAtWord(String string, int length) {
        if (string == null)
            return string;
        char charArray[] = string.toCharArray();
        int sLength = string.length();
        if (length < sLength)
            sLength = length;
        for (int i = 0; i < sLength - 1; i++) {
            if (charArray[i] == '\r' && charArray[i + 1] == '\n')
                return string.substring(0, i + 1);
            if (charArray[i] == '\n')
                return string.substring(0, i);
        }

        if (charArray[sLength - 1] == '\n')
            return string.substring(0, sLength - 1);
        if (string.length() < length)
            return string;
        for (int i = length - 1; i > 0; i--)
            if (charArray[i] == ' ')
                return string.substring(0, i).trim();

        return string.substring(0, length);
    }

    public static final String escapeForXML(String string) {
        if (string == null)
            return null;
        int i = 0;
        int last = 0;
        char input[] = string.toCharArray();
        int len = input.length;
        StringBuffer out = new StringBuffer((int) ((double) len * 1.3D));
        for (; i < len; i++) {
            char ch = input[i];
            if (ch > '>')
                continue;
            if (ch == '<') {
                if (i > last)
                    out.append(input, last, i - last);
                last = i + 1;
                out.append(LT_ENCODE);
                continue;
            }
            if (ch == '&') {
                if (i > last)
                    out.append(input, last, i - last);
                last = i + 1;
                out.append(AMP_ENCODE);
                continue;
            }
            if (ch != '"')
                continue;
            if (i > last)
                out.append(input, last, i - last);
            last = i + 1;
            out.append(QUOTE_ENCODE);
        }

        if (last == 0)
            return string;
        if (i > last)
            out.append(input, last, i - last);
        return out.toString();
    }

    public static final String unescapeFromXML(String string) {
        string = replace(string, "&lt;", "<");
        string = replace(string, "&gt;", ">");
        string = replace(string, "&quot;", "\"");
        return replace(string, "&amp;", "&");
    }

    public static final String zeroPadString(String string, int length) {
        if (string == null || string.length() > length) {
            return string;
        } else {
            StringBuffer buf = new StringBuffer(length);
            buf.append(zeroArray, 0, length - string.length()).append(string);
            return buf.toString();
        }
    }

    public static final String zeroAppendString(String string, int length) {
        if (string == null || string.length() > length) {
            return string;
        } else {
            StringBuffer buf = new StringBuffer(length);
            buf.append(string).append(zeroArray, 0, length - string.length());
            return buf.toString();
        }
    }

    public static final String dateToMillis(Date date) {
        return zeroPadString(Long.toString(date.getTime()), 15);
    }

    public static final String collectionToString(Collection c, String spilt) {
        String ret;
        ArrayList a;
        if (c == null)
            return null;
        if (spilt == null)
            return null;
        ret = "";
        a = new ArrayList(c);
        try {
            for (int i = 0; i < a.size(); i++) {
                String t = (String) a.get(i);
                if (i == a.size() - 1)
                    ret = ret + t;
                else
                    ret = ret + t + spilt;
            }

            return ret;
        } catch (Exception e) {
            return null;
        }


    }

    public static String genPassword(int length) {
        if (length < 1)
            return null;
        String strChars[] = {
                "1", "2", "3", "4", "5", "6", "7", "8", "9", "a",
                "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
                "m", "n", "p", "q", "r", "s", "t", "u", "v", "w",
                "x", "y", "z", "a"
        };
        String strPassword = "";
        int nRand = (int) Math.round(Math.random() * 100D);
        for (int i = 0; i < length; i++) {
            nRand = (int) Math.round(Math.random() * 100D);
            strPassword = strPassword + strChars[nRand % (strChars.length - 1)];
        }

        return strPassword;
    }

    public static String genEmptyString(int length) {
        if (length < 1)
            return null;
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++)
            sb.append(" ");

        return sb.toString();
    }

    public static String getHTML(String str) {
        String strret;
        InputStream ins;
        BufferedReader breader;
        InputStreamReader isreader;
        strret = null;
        URL rTmp = null;
        ins = null;
        breader = null;
        isreader = null;
        try {
            rTmp = new URL(str);
            ins = rTmp.openStream();
            isreader = new InputStreamReader(ins);
            breader = new BufferedReader(isreader);
            String info = breader.readLine();
            strret = info;
            for (info = breader.readLine(); info != null; info = breader.readLine())
                strret = strret + "\n" + info;

        }
        catch (Exception e) {
            try {
                if (breader != null)
                    breader.close();
            }
            catch (IOException ex) {
            }
            try {
                if (isreader != null)
                    isreader.close();
            }
            catch (IOException ex1) {
            }
            try {
                if (ins != null)
                    ins.close();
            }
            catch (IOException ex2) {
            }
            return strret;
        }
        finally {
            try {
                if (breader != null)
                    breader.close();
            }
            catch (IOException ex) {
            }
            try {
                if (isreader != null)
                    isreader.close();
            }
            catch (IOException ex1) {
            }
            try {
                if (ins != null)
                    ins.close();
            }
            catch (IOException ex2) {
            }
            return strret;
        }


    }

    public static String getAsciiString(int digit) {
        byte ret[] = new byte[1];
        ret[0] = (byte) digit;
        return new String(ret);
    }

    public static int getAsciiNum(String s) {
        if (s.length() < 1) {
            return 0;
        } else {
            byte b = s.getBytes()[0];
            return b;
        }
    }

    public static String getCurrTime() {
        Date now = new Date();
        SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String s = outFormat.format(now);
        return s;
    }

    public static boolean nullOrBlank(String param) {
        return param == null || param.trim().equals("");
    }

    public static String notNull(String param) {
        return param != null ? param.trim() : "";
    }

    public static int parseInt(String param, int defValue) {
        int i = defValue;
        try {
            i = Integer.parseInt(param);
        }
        catch (Exception e) {
        }
        return i;
    }

    public static long parseLong(String param, long defValue) {
        long l = defValue;
        try {
            l = Long.parseLong(param);
        }
        catch (Exception e) {
        }
        return l;
    }

    public static float parseFloat(String param, float defValue) {
        float f = defValue;
        try {
            f = Float.parseFloat(param);
        }
        catch (Exception e) {
        }
        return f;
    }

    public static double parseDouble(String param, double defValue) {
        double d = defValue;
        try {
            d = Double.parseDouble(param);
        }
        catch (Exception e) {
        }
        return d;
    }

    public static boolean parseBoolean(String param) {
        if (nullOrBlank(param))
            return false;
        switch (param.charAt(0)) {
            case 49: // '1'
            case 84: // 'T'
            case 89: // 'Y'
            case 116: // 't'
            case 121: // 'y'
                return true;
        }
        return false;
    }

    public static String encodeUrlString(String str) {
        String strret = null;
        if (str == null)
            return str;
        try {
            strret = URLEncoder.encode(str, "GBK");
        }
        catch (Exception e) {
            e.printStackTrace(System.err);
            return null;
        }
        return strret;
    }

    public static String decodeUrlString(String str) {
        String strret = null;
        if (str == null)
            return str;
        try {
            strret = URLDecoder.decode(str, "GBK");
        }
        catch (Exception e) {
            e.printStackTrace(System.err);
            return null;
        }
        return strret;
    }

    public static String[] split(String text, String separator, int max) {
        StringTokenizer tok = new StringTokenizer(text, separator);
        int listSize = tok.countTokens();
        if (max != -1 && listSize > max)
            listSize = max;
        String list[] = new String[listSize];
        int i = 0;
        do {
            if (!tok.hasMoreTokens())
                break;
            if (max != -1 && i == listSize - 1) {
                StringBuffer buf = new StringBuffer((1 * text.length() * (listSize - i)) / listSize);
                do {
                    if (!tok.hasMoreTokens())
                        break;
                    buf.append(tok.nextToken());
                    if (tok.hasMoreTokens())
                        buf.append(separator);
                } while (true);
                list[i] = buf.toString();
                break;
            }
            list[i] = tok.nextToken();
            i++;
        } while (true);
        return list;
    }

    public static String[] split(String text, String separator) {
        return split(text, separator, -1);
    }

    public static String join(Object list[], String separator) {
        int listSize = list.length;
        int bufSize = listSize == 0 ? 0 : (list[0].toString().length() + separator.length()) * listSize;
        StringBuffer buf = new StringBuffer(bufSize);
        for (int i = 0; i < listSize; i++) {
            if (i > 0)
                buf.append(separator);
            buf.append(list[i]);
        }

        return buf.toString();
    }

    public static String join(Iterator iterator, String separator) {
        StringBuffer buf = new StringBuffer();
        do {
            if (!iterator.hasNext())
                break;
            buf.append(iterator.next());
            if (iterator.hasNext())
                buf.append(separator);
        } while (true);
        return buf.toString();
    }

    protected static String wrapLine(String line, String newline, int wrapColumn) {
        StringBuffer wrappedLine = new StringBuffer();
        while (line.length() > wrapColumn) {
            int spaceToWrapAt = line.lastIndexOf(' ', wrapColumn);
            if (spaceToWrapAt >= 0) {
                wrappedLine.append(line.substring(0, spaceToWrapAt));
                wrappedLine.append(newline);
                line = line.substring(spaceToWrapAt + 1);
            } else {
                spaceToWrapAt = line.indexOf(' ', wrapColumn);
                if (spaceToWrapAt >= 0) {
                    wrappedLine.append(line.substring(0, spaceToWrapAt));
                    wrappedLine.append(newline);
                    line = line.substring(spaceToWrapAt + 1);
                } else {
                    wrappedLine.append(line);
                    line = "";
                }
            }
        }
        wrappedLine.append(line);
        return wrappedLine.toString();
    }

    public static synchronized String formatChineseString(String text) {
        String ret = text;
        if (ret == null) {
            return ret;
        } else {
            ret = replace(ret, "\uFF10", "0");
            ret = replace(ret, "\uFF11", "1");
            ret = replace(ret, "\uFF12", "2");
            ret = replace(ret, "\uFF13", "3");
            ret = replace(ret, "\uFF14", "4");
            ret = replace(ret, "\uFF15", "5");
            ret = replace(ret, "\uFF16", "6");
            ret = replace(ret, "\uFF17", "7");
            ret = replace(ret, "\uFF18", "8");
            ret = replace(ret, "\uFF19", "9");
            ret = replace(ret, "\uFF03", "#");
            return ret;
        }
    }

    public static String getCurrTimeISO8601() {
        String ret = "";
        Date now = new Date();
        SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd");
        ret = ret + outFormat.format(now) + "T";
        outFormat = new SimpleDateFormat("hh:mm:ss");
        ret = ret + outFormat.format(now) + ".";
        outFormat = new SimpleDateFormat("S");
        ret = ret + zeroAppendString(outFormat.format(now), 7);
        outFormat = new SimpleDateFormat("Z");
        String zone = outFormat.format(now);
        ret = ret + zone.substring(0, zone.length() - 2) + ":" + zone.substring(zone.length() - 2);
        return ret;
    }

    public static byte[] convertBytes(char c[]) {
        if (c == null)
            return null;
        if (c.length < 1)
            return new byte[0];
        byte ret[] = new byte[c.length];
        for (int i = 0; i < c.length; i++)
            ret[i] = (byte) c[i];

        return ret;
    }

    public static void testByte() {
        char c[] = {
                '\027', '\225', '0', '\003'
        };
        byte b[] = new byte[4];
        try {
            CharArrayWriter caw = new CharArrayWriter();
            caw.write(c);
            byte currentBytes[] = convertBytes(c);
            System.arraycopy(currentBytes, 0, b, 0, currentBytes.length);
            caw.reset();
            System.out.println(encodeHex(b));
        }
        catch (IOException ex1) {
        }
        try {
            RandomAccessFile msnObjFile = new RandomAccessFile("d:/abcd", "rw");
            msnObjFile.write(b);
            msnObjFile.close();
        }
        catch (FileNotFoundException ex) {
        }
        catch (IOException ex) {
        }
        try {
            String s = new String(b, 0, b.length, "UTF-8");
            System.out.println(encodeHex(s.getBytes("UTF-8")));
        }
        catch (UnsupportedEncodingException ex2) {
        }
    }


    public static int  getUnicodeType()
    {
        int ret=-1;
        try {
            String s1="",s2="";
            byte[] test="1".getBytes("Unicode");
            for(int i=2;i<test.length;i+=2){
                s1=Integer.toHexString(test[i]&0xff);
                s2=Integer.toHexString(test[i+1]&0xff);
                if (s1.length()==1)
                    s1="0"+s1;
                if (s2.length()==1)
                    s2="0"+s2;
                if (s1.equals("00"))
                    ret=0;
                if (s2.equals("00"))
                    ret=1;
            }

        }
        catch (Exception ex) {

        }
        return ret;
    }
    public static String  StrToUnicodeString(String instr)
    {
        if (unicodetype==-2)  unicodetype=getUnicodeType();
        String HexString="",s1="",s2="";
        if(instr==null || instr.equals(""))
            return HexString;
        try {
            byte[] test=instr.getBytes("Unicode");
            for(int i=2;i<test.length;i+=2){
                s1=Integer.toHexString(test[i]&0xff);
                s2=Integer.toHexString(test[i+1]&0xff);
                if (s1.length()==1)
                    s1="0"+s1;
                if (s2.length()==1)
                    s2="0"+s2;
                if (unicodetype==0)
                    HexString=HexString+"\\"+s1+s2;
                else
                    HexString=HexString+"\\"+s2+s1;
            }

        }
        catch (Exception ex) {
            HexString="";
        }

        return HexString;

    }

    public static String  StrToUnicodeStringA(String instr)
    {
        String HexString="",s1="",s2="";
        if(instr==null || instr.equals(""))
            return HexString;
        try {
            byte[] test=instr.getBytes("Unicode");
            for(int i=2;i<test.length;i+=2){
                s1=Integer.toHexString(test[i]&0xff);
                s2=Integer.toHexString(test[i+1]&0xff);
                if (s1.length()==1)
                    s1="0"+s1;
                if (s2.length()==1)
                    s2="0"+s2;
                HexString=HexString+"\\"+s2+s1;
            }

        }
        catch (Exception ex) {
            HexString="";
        }

        return HexString;

    }
    public   static String  StrToUnicodeString10(String instr)
    {
        String HexString="",s1="",s2="";
        if(instr==null || instr.equals(""))
            return HexString;
        try {
            byte[] test=instr.getBytes("Unicode");
            for(int i=2;i<test.length;i+=2){
                s1=Integer.toHexString(test[i]&0xff);
                s2=Integer.toHexString(test[i+1]&0xff);
                if (s1.length()==1)
                    s1="0"+s1;
                if (s2.length()==1)
                    s2="0"+s2;
                // Integer.decode("0x6d4b").toString()
                HexString=HexString+"&#"+Integer.decode("0x"+s2+s1).toString()+";";
            }

        }
        catch (Exception ex) {
            HexString="";
        }

        return HexString;

    }



    public static String  StrToGBKString(String instr)
    {
        String HexString="",s1="",s2="";
        if(instr==null || instr.equals(""))
            return HexString;
        try {
            byte[] test=instr.getBytes("gbk");
            for(int i=0;i<test.length;i+=2){
                s1=Integer.toHexString(test[i]&0xff);
                s2=Integer.toHexString(test[i+1]&0xff);
                if (s1.length()==1)
                    s1="0"+s1;
                if (s2.length()==1)
                    s2="0"+s2;
                HexString=HexString+"\\"+s2+s1;
            }

        }
        catch (Exception ex) {
            HexString="";
        }
        return HexString;
    }

    public static String HexCodeToStr(String HexCodestr,String type) {
        String restr = "无";
        int a = 0, b = 0;
        try {
            byte[] test = restr.getBytes(type);
            a = Integer.parseInt(HexCodestr.substring(0, 2), 16);
            b = Integer.parseInt(HexCodestr.substring(2, 4), 16);
            if (a > 127)
                a = a - 256;
            if (b > 127)
                b = b - 256;
            test[2] = (byte) b;
            test[3] = (byte) a;
            restr = new String(test, type);
        }
        catch (Exception ex) {
            restr = "";
        }

        return restr;
    }

    public static byte[] inttobytes(int in) {
        String restr = "无";
        int a = 0, b = 0;
        byte[] sssx=null;
        String sss=Integer.toHexString(in);
        /* try {
           byte[] test = restr.getBytes(type);
           a = Integer.parseInt(HexCodestr.substring(0, 2), 16);
           b = Integer.parseInt(HexCodestr.substring(2, 4), 16);
           if (a > 127)
             a = a - 256;
           if (b > 127)
             b = b - 256;
           test[2] = (byte) b;
           test[3] = (byte) a;
           restr = new String(test, type);
         }
         catch (Exception ex) {
           restr = "";
         }*/

        return sssx;
    }
    public static String decodeDateTimeStr(String dateStr)
    {
        String retStr = dateStr;
        retStr = retStr.replaceAll("[.]", "");
        retStr = retStr.replaceAll("[/]", "");
        retStr = retStr.replaceAll("[-]", "");
        retStr = retStr.replaceAll("[:]", "");
        retStr = retStr.replaceAll("[ ]", "");

        return retStr;
    }


    public static int compareDate(String dateStr1, String dateStr2)
    {
        int ret = -1;
        Date date1 = new Date(Integer.parseInt(dateStr1.substring(0, 4)),
                Integer.parseInt(dateStr1.substring(4, 6)),
                Integer.parseInt(dateStr1.substring(6, 8)));
        Date date2 = new Date(Integer.parseInt(dateStr2.substring(0, 4)),
                Integer.parseInt(dateStr2.substring(4, 6)),
                Integer.parseInt(dateStr2.substring(6, 8)));

        ret = date1.compareTo(date2);

        return ret;
    }


    public static int compareDateTime(String dateStr1, String dateStr2)
    {
        int ret = -1;
        Date date1 = new Date(Integer.parseInt(dateStr1.substring(0, 4)),
                Integer.parseInt(dateStr1.substring(4, 6)),
                Integer.parseInt(dateStr1.substring(6, 8)),
                Integer.parseInt(dateStr1.substring(8, 10)),
                Integer.parseInt(dateStr1.substring(10, 12)),
                Integer.parseInt(dateStr1.substring(12, 14))
        );
        Date date2 = new Date(Integer.parseInt(dateStr2.substring(0, 4)),
                Integer.parseInt(dateStr2.substring(4, 6)),
                Integer.parseInt(dateStr2.substring(6, 8)),
                Integer.parseInt(dateStr2.substring(8, 10)),
                Integer.parseInt(dateStr2.substring(10, 12)),
                Integer.parseInt(dateStr2.substring(12, 14))

        );

        ret = date1.compareTo(date2);
        return ret;
    }


    public static String getEncodeDateTimeStr(String oldStr)
    {
        String retStr = oldStr;

        if(retStr == null)return oldStr;
        if(retStr.length() <= 0)return oldStr;

        if(retStr.length() != 14)return oldStr;

        try
        {
            retStr = retStr.substring(0, 4) + "-" + retStr.substring(4, 6) +
                    "-" +
                    retStr.substring(6, 8)
                    + " " + retStr.substring(8, 10) + ":" + retStr.substring(10, 12) +
                    ":" + retStr.substring(12, 14);
        }
        catch(Exception e)
        {
            System.out.println("生成日期时间格式（年月日时分秒）失败" + e + oldStr);
        }

        return retStr;
    }


    public static String getEncodeDateTimeStr_Minute(String oldStr)
    {
        String retStr = oldStr;

        if(retStr == null)return oldStr;
        if(retStr.length() <= 0)return oldStr;

        if(retStr.length() != 12)return oldStr;

        try
        {
            retStr = retStr.substring(0, 4) + "-" + retStr.substring(4, 6) +
                    "-" +
                    retStr.substring(6, 8)
                    + " " + retStr.substring(8, 10) + ":" + retStr.substring(10, 12);
        }
        catch(Exception e)
        {
            System.out.println("生成日期时间格式（年月日时分）失败" + e + oldStr);
        }

        return retStr;
    }

    public static String formatDateTimeStr(String in)
    {
        if(in == null) return in;
        int len = in.length();
        if(len != 8 && len != 10 && len != 12 && len != 14) return in;

        String retStr = "";

        if (len == 8)
        {
            retStr = in.substring(0, 4) + "-" + in.substring(4, 6) +"-" +in.substring(6, 8);
        }
        else if (len == 10)
        {
            retStr = in.substring(0, 4) + "-" + in.substring(4, 6) +"-" +in.substring(6, 8)+ " " + in.substring(8, 10);
        }
        else if (len == 12)
        {
            retStr = in.substring(0, 4) + "-" + in.substring(4, 6) +"-" +in.substring(6, 8)+ " " + in.substring(8, 10)+ ":" + in.substring(10, 12);
        }
        else if (len == 14)
        {
            retStr = in.substring(0, 4) + "-" + in.substring(4, 6) +"-" +in.substring(6, 8)+ " " + in.substring(8, 10)+ ":" + in.substring(10, 12)+":" + in.substring(12, 14);
        }

        return retStr;
    }


    public static String getEncodeDateStr(String oldStr)
    {
        String retStr = oldStr;

        if(retStr == null)return oldStr;
        if(retStr.length() <= 0)return oldStr;

        if(retStr.length() != 8)return oldStr;

        try
        {
            retStr = retStr.substring(0, 4) + "-" + retStr.substring(4, 6) +
                    "-" +
                    retStr.substring(6, 8);
        }
        catch(Exception e)
        {
            System.out.println("生成日期格式（年月日）失败" + e + oldStr);
        }

        return retStr;
    }
    public static String[] array_cat(String[] array1, String[] array2)
    {

        String[] array0 = new String[array1.length + array2.length];

        for(int i = 0; i < array1.length; i++)
        {
            array0[i] = array1[i];
        }

        for(int i = 0; i < array2.length; i++)
        {
            array0[i + array1.length] = array2[i];
        }
        return array0;
    }


    public static String[] array_cat(String[] array1, String[] array2, String str)
    {

        String[] array0 = new String[array1.length + array2.length + 1];

        for(int i = 0; i < array1.length; i++)
        {
            array0[i] = array1[i];
        }

        for(int i = 0; i < array2.length; i++)
        {
            array0[i + array1.length] = array2[i];
        }

        array0[array0.length - 1] = str;
        return array0;
    }



    public static String array2String(String[] array) {
        StringBuffer str = new StringBuffer();
        for (int i = 0; i < array.length; i ++ ) {
            str.append(array[i]);
            if (i < array.length - 1) {
                str.append(",");
            }
        }
        return str.toString();
    }
    //added end by lmm
    public static boolean isNumber(String value) {
        try {
            Integer.parseInt(value);
            return true;
        } catch (IllegalArgumentException e) {
        }
        return false;
    }
    private static final char QUOTE_ENCODE[] = "&quot;".toCharArray();
    private static final char AMP_ENCODE[] = "&amp;".toCharArray();
    private static final char LT_ENCODE[] = "&lt;".toCharArray();
    private static final char GT_ENCODE[] = "&gt;".toCharArray();
    private static MessageDigest digest = null;
    private static final int fillchar = 61;
    private static final String cvt = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    private static Random randGen = new Random();
    private static char numbersAndLetters[] = "0123456789abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
    private static final char zeroArray[] = "0000000000000000".toCharArray();


    /**
     * 判断字符串是否含有汉字。
     *
     * @param str 字符串
     * @return 有返回TRUE,没有返回FALSE。
     */
    public static boolean isContainChinese(String str) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }
}
