一、前言
关于ivy源码包中org.apache.ivy.util.StringUtils字符串工具类,获取Throwable异常堆栈字符串信息(getStackTrace)、获取异常错误信息部分(getErrorMessage)、字符串数据简单转码加密解密处理(encrypt/decrypt)等。
二、源码说明
package org.apache.ivy.util;@b@@b@import java.io.PrintWriter;@b@import java.io.StringWriter;@b@import java.lang.reflect.InvocationTargetException;@b@import java.util.Locale; @b@ @b@public final class StringUtils {@b@@b@ private StringUtils() {@b@ // Utility class@b@ }@b@@b@ public static String uncapitalize(String string) {@b@ if (string == null || string.length() == 0) {@b@ return string;@b@ }@b@ if (string.length() == 1) {@b@ return string.toLowerCase(Locale.US);@b@ }@b@ return string.substring(0, 1).toLowerCase(Locale.US) + string.substring(1);@b@ }@b@@b@ /**@b@ * Returns the error message associated with the given Throwable. The error message returned@b@ * will try to be as precise as possible, handling cases where e.getMessage() is not meaningful,@b@ * like {@link NullPointerException} for instance.@b@ * @b@ * @param t@b@ * the throwable to get the error message from@b@ * @return the error message of the given exception@b@ */@b@ public static String getErrorMessage(Throwable t) {@b@ if (t == null) {@b@ return "";@b@ }@b@ if (t instanceof InvocationTargetException) {@b@ InvocationTargetException ex = (InvocationTargetException) t;@b@ t = ex.getTargetException();@b@ }@b@ String errMsg = t instanceof RuntimeException ? t.getMessage() : t.toString();@b@ if (errMsg == null || errMsg.length() == 0 || "null".equals(errMsg)) {@b@ errMsg = t.getClass().getName() + " at " + t.getStackTrace()[0].toString();@b@ }@b@ return errMsg;@b@ }@b@@b@ /**@b@ * Returns the exception stack trace as a String.@b@ * @b@ * @param e@b@ * the exception to get the stack trace from.@b@ * @return the exception stack trace@b@ */@b@ public static String getStackTrace(Throwable e) {@b@ if (e == null) {@b@ return "";@b@ }@b@ StringWriter sw = new StringWriter();@b@ PrintWriter printWriter = new PrintWriter(sw, true);@b@ e.printStackTrace(printWriter);@b@ return sw.getBuffer().toString();@b@ }@b@@b@ /**@b@ * Joins the given object array in one string, each separated by the given separator.@b@ * @b@ * Example:@b@ * @b@ * <pre>@b@ * join(new String[] {"one", "two", "three"}, ", ") -> "one, two, three"@b@ * </pre>@b@ * @b@ * @param objs@b@ * The array of objects (<code>toString()</code> is used).@b@ * @param sep@b@ * The separator to use.@b@ * @return The concatinated string.@b@ */@b@ public static String join(Object[] objs, String sep) {@b@ StringBuffer buf = new StringBuffer();@b@ for (int i = 0; i < objs.length; i++) {@b@ buf.append(objs[i]).append(sep);@b@ }@b@ if (objs.length > 0) {@b@ buf.setLength(buf.length() - sep.length()); // delete sep@b@ }@b@ return buf.toString();@b@ }@b@@b@ // basic string codec (same algo as CVS passfile, inspired by ant CVSPass class@b@ /** Array contain char conversion data */@b@ private static final char[] SHIFTS = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,@b@ 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 114, 120, 53, 79, 96, 109,@b@ 72, 108, 70, 64, 76, 67, 116, 74, 68, 87, 111, 52, 75, 119, 49, 34, 82, 81, 95, 65,@b@ 112, 86, 118, 110, 122, 105, 41, 57, 83, 43, 46, 102, 40, 89, 38, 103, 45, 50, 42, 123,@b@ 91, 35, 125, 55, 54, 66, 124, 126, 59, 47, 92, 71, 115, 78, 88, 107, 106, 56, 36, 121,@b@ 117, 104, 101, 100, 69, 73, 99, 63, 94, 93, 39, 37, 61, 48, 58, 113, 32, 90, 44, 98,@b@ 60, 51, 33, 97, 62, 77, 84, 80, 85, 223, 225, 216, 187, 166, 229, 189, 222, 188, 141,@b@ 249, 148, 200, 184, 136, 248, 190, 199, 170, 181, 204, 138, 232, 218, 183, 255, 234,@b@ 220, 247, 213, 203, 226, 193, 174, 172, 228, 252, 217, 201, 131, 230, 197, 211, 145,@b@ 238, 161, 179, 160, 212, 207, 221, 254, 173, 202, 146, 224, 151, 140, 196, 205, 130,@b@ 135, 133, 143, 246, 192, 159, 244, 239, 185, 168, 215, 144, 139, 165, 180, 157, 147,@b@ 186, 214, 176, 227, 231, 219, 169, 175, 156, 206, 198, 129, 164, 150, 210, 154, 177,@b@ 134, 127, 182, 128, 158, 208, 162, 132, 167, 209, 149, 241, 153, 251, 237, 236, 171,@b@ 195, 243, 233, 253, 240, 194, 250, 191, 155, 142, 137, 245, 235, 163, 242, 178, 152};@b@@b@ /**@b@ * Encrypt the given string in a way which anybody having access to this method algorithm can@b@ * easily decrypt. This is useful only to avoid clear string storage in a file for example, but@b@ * shouldn't be considered as a real mean of security. This only works with simple characters@b@ * (char < 256).@b@ * @b@ * @param str@b@ * the string to encrypt@b@ * @return the encrypted version of the string@b@ */@b@ public static final String encrypt(String str) {@b@ if (str == null) {@b@ return null;@b@ }@b@ StringBuffer buf = new StringBuffer();@b@ for (int i = 0; i < str.length(); i++) {@b@ char c = str.charAt(i);@b@ if (c >= SHIFTS.length) {@b@ throw new IllegalArgumentException(@b@ "encrypt method can only be used with simple characters. '" + c@b@ + "' not allowed");@b@ }@b@ buf.append(SHIFTS[c]);@b@ }@b@ return buf.toString();@b@ }@b@@b@ /**@b@ * Decrypts a string encrypted with encrypt.@b@ * @b@ * @param str@b@ * the encrypted string to decrypt@b@ * @return The decrypted string.@b@ */@b@ public static final String decrypt(String str) {@b@ if (str == null) {@b@ return null;@b@ }@b@ StringBuffer buf = new StringBuffer();@b@ for (int i = 0; i < str.length(); i++) {@b@ buf.append(decrypt(str.charAt(i)));@b@ }@b@ return buf.toString();@b@ }@b@@b@ private static char decrypt(char c) {@b@ for (char i = 0; i < SHIFTS.length; i++) {@b@ if (SHIFTS[i] == c) {@b@ return i;@b@ }@b@ }@b@ throw new IllegalArgumentException("Impossible to decrypt '" + c@b@ + "'. Unhandled character.");@b@ }@b@@b@ public static String repeat(String str, int count) {@b@ StringBuffer sb = new StringBuffer();@b@ for (int i = 0; i < count; i++) {@b@ sb.append(str);@b@ }@b@ return sb.toString();@b@ }@b@@b@}