一、前言
关于apache ant的ant-tools源码包org.apache.tools.ant.util.StringUtils字符串工具类,对字符分行分隔lineSplit、获取Throwable异常堆栈PrintWriter输出为StringWriter字符串、特殊字符过滤处理resolveBackSlash、数组连接拼接join等操作。
二、源码说明
package org.apache.tools.ant.util;@b@@b@import java.io.PrintWriter;@b@import java.io.StringWriter;@b@import java.util.Arrays;@b@import java.util.Collection;@b@import java.util.Vector;@b@import java.util.stream.Collector;@b@import java.util.stream.Collectors; @b@import org.apache.tools.ant.BuildException; @b@@b@public final class StringUtils {@b@@b@ private static final long KILOBYTE = 1024;@b@ private static final long MEGABYTE = KILOBYTE * 1024;@b@ private static final long GIGABYTE = MEGABYTE * 1024;@b@ private static final long TERABYTE = GIGABYTE * 1024;@b@ private static final long PETABYTE = TERABYTE * 1024;@b@@b@ @b@ private StringUtils() {@b@ }@b@@b@ @b@ public static final String LINE_SEP = System.getProperty("line.separator");@b@@b@ @b@ public static Vector<String> lineSplit(String data) {@b@ return split(data, '\n');@b@ }@b@@b@ @b@ public static Vector<String> split(String data, int ch) {@b@ Vector<String> elems = new Vector<String>();@b@ int pos = -1;@b@ int i = 0;@b@ while ((pos = data.indexOf(ch, i)) != -1) {@b@ String elem = data.substring(i, pos);@b@ elems.addElement(elem);@b@ i = pos + 1;@b@ }@b@ elems.addElement(data.substring(i));@b@ return elems;@b@ }@b@@b@ @b@ @Deprecated@b@ public static String replace(String data, String from, String to) {@b@ return data.replace(from, to);@b@ }@b@@b@ @b@ public static String getStackTrace(Throwable t) {@b@ StringWriter sw = new StringWriter();@b@ PrintWriter pw = new PrintWriter(sw, true);@b@ t.printStackTrace(pw); //NOSONAR@b@ pw.flush();@b@ pw.close();@b@ return sw.toString();@b@ }@b@@b@ @b@ public static boolean endsWith(StringBuffer buffer, String suffix) {@b@ if (suffix.length() > buffer.length()) {@b@ return false;@b@ }@b@ @b@ int endIndex = suffix.length() - 1;@b@ int bufferIndex = buffer.length() - 1;@b@ while (endIndex >= 0) {@b@ if (buffer.charAt(bufferIndex) != suffix.charAt(endIndex)) {@b@ return false;@b@ }@b@ bufferIndex--;@b@ endIndex--;@b@ }@b@ return true;@b@ }@b@@b@ @b@ public static String resolveBackSlash(String input) {@b@ StringBuilder b = new StringBuilder();@b@ boolean backSlashSeen = false;@b@ for (int i = 0; i < input.length(); ++i) {@b@ char c = input.charAt(i);@b@ if (!backSlashSeen) {@b@ if (c == '\\') {@b@ backSlashSeen = true;@b@ } else {@b@ b.append(c);@b@ }@b@ } else {@b@ switch (c) {@b@ case '\\':@b@ b.append((char) '\\');@b@ break;@b@ case 'n':@b@ b.append((char) '\n');@b@ break;@b@ case 'r':@b@ b.append((char) '\r');@b@ break;@b@ case 't':@b@ b.append((char) '\t');@b@ break;@b@ case 'f':@b@ b.append((char) '\f');@b@ break;@b@ case 's':@b@ b.append(" \t\n\r\f");@b@ break;@b@ default:@b@ b.append(c);@b@ }@b@ backSlashSeen = false;@b@ }@b@ }@b@ return b.toString();@b@ }@b@@b@ @b@ public static long parseHumanSizes(String humanSize) throws Exception { //NOSONAR@b@ long factor = 1L;@b@ char s = humanSize.charAt(0);@b@ switch (s) {@b@ case '+':@b@ humanSize = humanSize.substring(1);@b@ break;@b@ case '-':@b@ factor = -1L;@b@ humanSize = humanSize.substring(1);@b@ break;@b@ default:@b@ break;@b@ }@b@ //last character isn't a digit@b@ char c = humanSize.charAt(humanSize.length() - 1);@b@ if (!Character.isDigit(c)) {@b@ int trim = 1;@b@ switch (c) {@b@ case 'K':@b@ factor *= KILOBYTE;@b@ break;@b@ case 'M':@b@ factor *= MEGABYTE;@b@ break;@b@ case 'G':@b@ factor *= GIGABYTE;@b@ break;@b@ case 'T':@b@ factor *= TERABYTE;@b@ break;@b@ case 'P':@b@ factor *= PETABYTE;@b@ break;@b@ default:@b@ trim = 0;@b@ }@b@ humanSize = humanSize.substring(0, humanSize.length() - trim);@b@ }@b@ try {@b@ return factor * Long.parseLong(humanSize);@b@ } catch (NumberFormatException e) {@b@ throw new BuildException("Failed to parse \"" + humanSize + "\"", e);@b@ }@b@ }@b@@b@ @b@ public static String removeSuffix(String string, String suffix) {@b@ if (string.endsWith(suffix)) {@b@ return string.substring(0, string.length() - suffix.length());@b@ }@b@ return string;@b@ }@b@@b@ @b@ public static String removePrefix(String string, String prefix) {@b@ if (string.startsWith(prefix)) {@b@ return string.substring(prefix.length());@b@ }@b@ return string;@b@ }@b@@b@ @b@ public static String join(Collection<?> collection, CharSequence separator) {@b@ if (collection == null) {@b@ return "";@b@ }@b@ return collection.stream().map(String::valueOf)@b@ .collect(joining(separator));@b@ }@b@@b@ @b@ public static String join(Object[] array, CharSequence separator) {@b@ if (array == null) {@b@ return "";@b@ }@b@ return join(Arrays.asList(array), separator);@b@ }@b@@b@ private static Collector<CharSequence, ?, String> joining(CharSequence separator) {@b@ return separator == null ? Collectors.joining() : Collectors.joining(separator);@b@ }@b@ @b@ public static String trimToNull(String inputString) {@b@ if (inputString == null) {@b@ return null;@b@ }@b@ String tmpString = inputString.trim();@b@ return tmpString.isEmpty() ? null : tmpString;@b@ }@b@@b@}