一、前言
关于apache的ftpserver的核心源码包ftpserver-core中org.apache.ftpserver.util.StringUtils字符串工具类,对字符串常用数据类型替换replaceString、对HTML字符串格式化formatHtml、字符串对于二进制byte[]数组相互转换toHexString/toByteArray等处理。
二、代码示例
package org.apache.ftpserver.util;@b@@b@import java.util.Map;@b@@b@public class StringUtils@b@{@b@ public static final String replaceString(String source, String oldStr, String newStr)@b@ {@b@ StringBuilder sb = new StringBuilder(source.length());@b@ int sind = 0;@b@ int cind = 0;@b@ while ((cind = source.indexOf(oldStr, sind)) != -1) {@b@ sb.append(source.substring(sind, cind));@b@ sb.append(newStr);@b@ sind = cind + oldStr.length();@b@ }@b@ sb.append(source.substring(sind));@b@ return sb.toString();@b@ }@b@@b@ public static final String replaceString(String source, Object[] args)@b@ {@b@ int startIndex = 0;@b@ int openIndex = source.indexOf(123, startIndex);@b@ if (openIndex == -1) {@b@ return source;@b@ }@b@@b@ int closeIndex = source.indexOf(125, startIndex);@b@ if ((closeIndex == -1) || (openIndex > closeIndex)) {@b@ return source;@b@ }@b@@b@ StringBuilder sb = new StringBuilder();@b@ sb.append(source.substring(startIndex, openIndex));@b@ while (true) {@b@ String intStr = source.substring(openIndex + 1, closeIndex);@b@ int index = Integer.parseInt(intStr);@b@ sb.append(args[index]);@b@@b@ startIndex = closeIndex + 1;@b@ openIndex = source.indexOf(123, startIndex);@b@ if (openIndex == -1) {@b@ sb.append(source.substring(startIndex));@b@ break;@b@ }@b@@b@ closeIndex = source.indexOf(125, startIndex);@b@ if ((closeIndex == -1) || (openIndex > closeIndex)) {@b@ sb.append(source.substring(startIndex));@b@ break;@b@ }@b@ sb.append(source.substring(startIndex, openIndex));@b@ }@b@ return sb.toString();@b@ }@b@@b@ public static final String replaceString(String source, Map<String, Object> args)@b@ {@b@ int startIndex = 0;@b@ int openIndex = source.indexOf(123, startIndex);@b@ if (openIndex == -1) {@b@ return source;@b@ }@b@@b@ int closeIndex = source.indexOf(125, startIndex);@b@ if ((closeIndex == -1) || (openIndex > closeIndex)) {@b@ return source;@b@ }@b@@b@ StringBuilder sb = new StringBuilder();@b@ sb.append(source.substring(startIndex, openIndex));@b@ while (true) {@b@ String key = source.substring(openIndex + 1, closeIndex);@b@ Object val = args.get(key);@b@ if (val != null) {@b@ sb.append(val);@b@ }@b@@b@ startIndex = closeIndex + 1;@b@ openIndex = source.indexOf(123, startIndex);@b@ if (openIndex == -1) {@b@ sb.append(source.substring(startIndex));@b@ break;@b@ }@b@@b@ closeIndex = source.indexOf(125, startIndex);@b@ if ((closeIndex == -1) || (openIndex > closeIndex)) {@b@ sb.append(source.substring(startIndex));@b@ break;@b@ }@b@ sb.append(source.substring(startIndex, openIndex));@b@ }@b@ return sb.toString();@b@ }@b@@b@ public static final String formatHtml(String source, boolean bReplaceNl, boolean bReplaceTag, boolean bReplaceQuote)@b@ {@b@ StringBuilder sb = new StringBuilder();@b@ int len = source.length();@b@ for (int i = 0; i < len; ++i) {@b@ char c = source.charAt(i);@b@ switch (c)@b@ {@b@ case '"':@b@ if (bReplaceQuote)@b@ sb.append(""");@b@ else@b@ sb.append(c);@b@ break;@b@ case '<':@b@ if (bReplaceTag)@b@ sb.append("<");@b@ else@b@ sb.append(c);@b@ break;@b@ case '>':@b@ if (bReplaceTag)@b@ sb.append(">");@b@ else@b@ sb.append(c);@b@ break;@b@ case '\n':@b@ if (bReplaceNl)@b@ if (bReplaceTag)@b@ sb.append("<br>");@b@ else@b@ sb.append("<br>");@b@ else@b@ sb.append(c);@b@@b@ break;@b@ case '\r':@b@ break;@b@ case '&':@b@ sb.append("&");@b@ break;@b@ default:@b@ sb.append(c);@b@ }@b@ }@b@@b@ return sb.toString();@b@ }@b@@b@ public static final String pad(String src, char padChar, boolean rightPad, int totalLength)@b@ {@b@ int srcLength = src.length();@b@ if (srcLength >= totalLength) {@b@ return src;@b@ }@b@@b@ int padLength = totalLength - srcLength;@b@ StringBuilder sb = new StringBuilder(padLength);@b@ for (int i = 0; i < padLength; ++i) {@b@ sb.append(padChar);@b@ }@b@@b@ if (rightPad)@b@ return src + sb.toString();@b@@b@ return sb.toString() + src;@b@ }@b@@b@ public static final String toHexString(byte[] res)@b@ {@b@ StringBuilder sb = new StringBuilder(res.length << 1);@b@ for (int i = 0; i < res.length; ++i) {@b@ String digit = Integer.toHexString(0xFF & res[i]);@b@ if (digit.length() == 1)@b@ sb.append('0');@b@@b@ sb.append(digit);@b@ }@b@ return sb.toString().toUpperCase();@b@ }@b@@b@ public static final byte[] toByteArray(String hexString)@b@ {@b@ int arrLength = hexString.length() >> 1;@b@ byte[] buff = new byte[arrLength];@b@ for (int i = 0; i < arrLength; ++i) {@b@ int index = i << 1;@b@ String digit = hexString.substring(index, index + 2);@b@ buff[i] = (byte)Integer.parseInt(digit, 16);@b@ }@b@ return buff;@b@ }@b@}