一、前言
关于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 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 ':@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("
");@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 > 1;@b@ byte[] buff = new byte[arrLength];@b@ for (int i = 0; i