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

📄 utility.java

📁 对xml很好的java处理引擎,编译中绑定xml
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
     * @param date date to be converted     * @return converted dateTime text     */    public static String serializeDateTime(Date date) {        return serializeDateTime(date.getTime(), true);    }//#!j2me{    /**     * Serialize timestamp to general dateTime text. Timestamp values are     * represented in the same way as regular dates, but allow more precision in     * the fractional second value (down to nanoseconds).     *     * @param stamp timestamp to be converted     * @return converted dateTime text     */    public static String serializeTimestamp(Timestamp stamp) {                // check for nanosecond value to be included        int nano = stamp.getNanos();        if (nano > 0) {                        // convert the number of nanoseconds to text            String value = serializeInt(nano);                        // pad with leading zeros if less than 9 digits            StringBuffer digits = new StringBuffer(9);            if (value.length() < 9) {                int lead = 9 - value.length();                for (int i = 0; i < lead; i++) {                    digits.append('0');                }            }            digits.append(value);                        // strip trailing zeros from value            int last = 9;            while (--last >= 0) {                if (digits.charAt(last) != '0') {                    break;                }            }            digits.setLength(last+1);                        // finish by appending to rounded time with decimal separator            long time = stamp.getTime();            return serializeDateTime(time - time % 1000, false) + '.' +                digits + 'Z';                    } else {            return serializeDateTime(stamp.getTime(), true);        }    }    /**     * Serialize time to standard text. Time values are formatted in W3C XML     * Schema standard format as hh:mm:ss, with optional trailing seconds     * decimal, as necessary. The standard conversion does not append a time     * zone indication.     *     * @param time time to be converted     * @return converted time text     */    public static String serializeSqlTime(Time time) {        StringBuffer buff = new StringBuffer(12);        serializeTime((int)time.getTime(), buff);        return buff.toString();    }//#j2me}        /**     * General object comparison method. Don't know why Sun hasn't seen fit to     * include this somewhere, but at least it's easy to write (over and over     * again).     *      * @param a first object to be compared     * @param b second object to be compared     * @return <code>true</code> if both objects are <code>null</code>, or if     * <code>a.equals(b)</code>; <code>false</code> otherwise     */    public static boolean isEqual(Object a, Object b) {        return (a == null) ? b == null : a.equals(b);    }    /**     * Find text value in enumeration. This first does a binary search through     * an array of allowed text matches. If a separate array of corresponding     * values is supplied, the value at the matched position is returned;     * otherwise the match index is returned directly.     *     * @param target text to be found in enumeration     * @param enums ordered array of texts included in enumeration     * @param vals array of values to be returned for corresponding text match     * positions (position returned directly if this is <code>null</code>)     * @return enumeration value for target text     * @throws JiBXException if target text not found in enumeration     */    public static int enumValue(String target, String[] enums, int[] vals)        throws JiBXException {        int base = 0;        int limit = enums.length - 1;        while (base <= limit) {            int cur = (base + limit) >> 1;            int diff = target.compareTo(enums[cur]);            if (diff < 0) {                limit = cur - 1;            } else if (diff > 0) {                base = cur + 1;            } else if (vals != null) {                return vals[cur];            } else {                return cur;            }        }        throw new JiBXException("Target value \"" + target +            "\" not found in enumeration");    }    /**     * Decode a chunk of data from base64 encoding. The length of a chunk is     * always 4 characters in the base64 representation, but may be 1, 2, or 3     * bytes of data, as determined by whether there are any pad characters at     * the end of the base64 representation      *     * @param base starting offset within base64 character array     * @param chrs character array for base64 text representation     * @param fill starting offset within byte data array     * @param byts byte data array     * @return number of decoded bytes     */	private static int decodeChunk(int base, char[] chrs, int fill,        byte[] byts) {                // find the byte count to be decoded		int length = 3;		if (chrs[base+3] == PAD_CHAR) {			length = 2;			if (chrs[base+2] == PAD_CHAR) {				length = 1;			}		}                // get 6-bit values		int v0 = s_base64Values[chrs[base+0]];		int v1 = s_base64Values[chrs[base+1]];		int v2 = s_base64Values[chrs[base+2]];		int v3 = s_base64Values[chrs[base+3]];                // convert and store bytes of data		switch (length) {			case 3:				byts[fill+2] = (byte)(v2 << 6 | v3);			case 2:				byts[fill+1] = (byte)(v1 << 4 | v2 >> 2);			case 1:				byts[fill] = (byte)(v0 << 2 | v1 >> 4);				break;		}		return length;	}    /**     * Parse base64 data from text. This converts the base64 data into a byte     * array of the appopriate length. In keeping with the recommendations,     *     * @param text text to be parsed (may include extra characters)     * @return byte array of data     * @throws JiBXException if invalid character in base64 representation     */	public static byte[] parseBase64(String text) throws JiBXException {                // convert raw text to base64 character array        char[] chrs = new char[text.length()];        int length = 0;        for (int i = 0; i < text.length(); i++) {            char chr = text.charAt(i);            if (chr < 128 && s_base64Values[chr] >= 0) {                chrs[length++] = chr;            }        }                // check the text length		if (length % 4 != 0) {			throw new JiBXException                ("Text length for base64 must be a multiple of 4");		} else if (length == 0) {			return new byte[0];		}                // find corresponding byte count for data		int blength = length / 4 * 3;		if (chrs[length-1] == PAD_CHAR) {			blength--;			if (chrs[length-2] == PAD_CHAR) {				blength--;			}		}                // convert text to actual bytes of data		byte[] byts = new byte[blength];		int fill = 0;		for (int i = 0; i < length; i += 4) {			fill += decodeChunk(i, chrs, fill, byts);		}		if (fill != blength) {			throw new JiBXException                ("Embedded padding characters in byte64 text");		}		return byts;	}    /**     * Parse base64 data from text. This converts the base64 data into a byte     * array of the appopriate length. In keeping with the recommendations,     *     * @param text text to be parsed (may be null, or include extra characters)     * @return byte array of data     * @throws JiBXException if invalid character in base64 representation     */    public static byte[] deserializeBase64(String text) throws JiBXException {        if (text == null) {            return null;        } else {            return parseBase64(text);        }    }    /**     * Encode a chunk of data to base64 encoding. Converts the next three bytes     * of data into four characters of text representation, using padding at the     * end of less than three bytes of data remain.     *     * @param base starting offset within byte array     * @param byts byte data array     * @param buff buffer for encoded text     */	public static void encodeChunk(int base, byte[] byts, StringBuffer buff) {                // get actual byte data length to be encoded		int length = 3;		if (base + length > byts.length) {			length = byts.length - base;		}                // convert up to three bytes of data to four characters of text		int b0 = byts[base];		int value = (b0 >> 2) & 0x3F;		buff.append(s_base64Chars[value]);		if (length > 1) {			int b1 = byts[base+1];			value = ((b0 & 3) << 4) + ((b1 >> 4) & 0x0F);			buff.append(s_base64Chars[value]);			if (length > 2) {				int b2 = byts[base+2];				value = ((b1 & 0x0F) << 2) + ((b2 >> 6) & 3);				buff.append(s_base64Chars[value]);				value = b2 & 0x3F;				buff.append(s_base64Chars[value]);			} else {				value = (b1 & 0x0F) << 2;				buff.append(s_base64Chars[value]);				buff.append(PAD_CHAR);			}		} else {			value = (b0 & 3) << 4;			buff.append(s_base64Chars[value]);			buff.append(PAD_CHAR);			buff.append(PAD_CHAR);		}	}    /**     * Serialize byte array to base64 text. In keeping with the specification,     * this adds a line break every 76 characters in the encoded representation.     *     * @param byts byte data array     * @return base64 encoded text     */	public static String serializeBase64(byte[] byts) {        StringBuffer buff = new StringBuffer((byts.length + 2) / 3 * 4);		for (int i = 0; i < byts.length; i += 3) {			encodeChunk(i, byts, buff);            if (i > 0 && i % 57 == 0 && (i + 3) < byts.length) {                buff.append("\r\n");            }		}		return buff.toString();	}        /**     * Resize array of arbitrary type.     *     * @param size new aray size     * @param base array to be resized     * @return resized array, with all data to minimum of the two sizes copied     */    public static Object resizeArray(int size, Object base) {        int prior = Array.getLength(base);        if (size == prior) {            return base;        } else {            Class type = base.getClass().getComponentType();            Object copy = Array.newInstance(type, size);            int count = Math.min(size, prior);            System.arraycopy(base, 0, copy, 0, count);            return copy;        }    }        /**     * Grow array of arbitrary type. The returned array is double the size of     * the original array, providing this is at least the defined minimum size.     *     * @param base array to be grown     * @return array of twice the size as original array, with all data copied     */    public static Object growArray(Object base) {        int length = Array.getLength(base);        Class type = base.getClass().getComponentType();        int newlen = Math.max(length*2, MINIMUM_GROWN_ARRAY_SIZE);        Object copy = Array.newInstance(type, newlen);        System.arraycopy(base, 0, copy, 0, length);        return copy;    }        /**     * Factory method to create a <code>java.util.ArrayList</code> as the     * implementation of a <code>java.util.List</code>.     *     * @return new <code>java.util.ArrayList</code>     */    public static List arrayListFactory() {        return new ArrayList();    }        /**     * Convert whitespace-separated list of values. This implements the     * whitespace skipping, calling the supplied item deserializer for handling     * each individual value.     *      * @param text value to be converted     * @param ideser list item deserializer     * @return list of deserialized items (<code>null</code> if input     * <code>null</code>, or if nonrecoverable error)     * @throws JiBXException if error in deserializing text     */    public static ArrayList deserializeList(String text,        IListItemDeserializer ideser) throws JiBXException {        if (text == null) {            return null;        } else {                        // scan text to find whitespace breaks between items            ArrayList items = new ArrayList();            int length = text.length();            int base = 0;            boolean space = true;            for (int i = 0; i < length; i++) {                char chr = text.charAt(i);                switch (chr) {                                        case 0x09:                    case 0x0A:                    case 0x0D:                    case ' ':                        // ignore if preceded by space                        if (!space) {                            String itext = text.substring(base, i);                            items.add(ideser.deserialize(itext));                            space = true;                        }                        base = i + 1;                        break;                                            default:                        space = false;                        break;                }            }                        // finish last item            if (base < length) {                String itext = text.substring(base);                items.add(ideser.deserialize(itext));            }                        // check if any items found            if (items.size() > 0) {                return items;            } else {                return null;            }        }    }    /**     * Deserialize a list of whitespace-separated tokens into an array of     * strings.     *      * @param text value text     * @return created class instance     * @throws JiBXException on error in marshalling     */    public static String[] deserializeTokenList(String text) throws JiBXException {                // use basic qualified name deserializer to handle items        IListItemDeserializer ldser = new IListItemDeserializer() {            public Object deserialize(String text) throws JiBXException {                return text;            }        };        ArrayList list = Utility.deserializeList(text, ldser);        if (list == null) {            return null;        } else {            return (String[])list.toArray(new String[list.size()]);        }    }        /**     * Serialize an array of strings into a whitespace-separated token list.     *      * @param tokens array of strings to be serialized     * @return list text     */    public static String serializeTokenList(String[] tokens) {        StringBuffer buff = new StringBuffer();        for (int i = 0; i < tokens.length; i++) {            if (buff.length() > 0) {                buff.append(' ');            }            buff.append(tokens[i]);        }        return buff.toString();    }}

⌨️ 快捷键说明

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