httputil.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 601 行 · 第 1/2 页
JAVA
601 行
* * @param source the String to encode * @param dest a StringBuffer that receives the encoded result */ static public void encode(String source, StringBuffer dest) { encodeUri(source, 0, source.length(), dest); } /** * Extract and encode a portion of a String. * * @param source the String to encode * @param beginIndex the begin index, inclusive * @param endIndex the end index, exclusive * @param dest a StringBuffer that receives the encoded result */ static public void encode(String source, int beginIndex, int endIndex, StringBuffer dest) { encodeUri(source, beginIndex, endIndex, dest); } /** * Extract and encode a portion of a StringBuffer. * * @param source the StringBuffer to encode * @param beginIndex the begin index, inclusive * @param endIndex the end index, exclusive * @param dest a StringBuffer that receives the encoded result */ static public void encode(StringBuffer source, int beginIndex, int endIndex, StringBuffer dest) { encodeUri(source, beginIndex, endIndex, dest); } static public void encodeUri(CharSequence source, int beginIndex, int endIndex, StringBuffer dest) { for (int i = beginIndex; i < endIndex; i++) { char ch = source.charAt(i); if ( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'z') || (ch >= '0' && ch <= '9') || ch == '-' || ch == '_' || ch == '.' || ch == '*') { dest.append(ch); } else if (ch == ' ') { dest.append('+'); } else if (ch <= 0xff) { // 8 byte (utf-8) dest.append('%'); dest.append(encodeHex(ch >> 4)); dest.append(encodeHex(ch)); } else { // 16 byte (utf-16) dest.append('%'); dest.append('u'); dest.append(encodeHex(ch >> 12)); dest.append(encodeHex(ch >> 8)); dest.append(encodeHex(ch >> 4)); dest.append(encodeHex(ch)); } } } /** * Encode a string. * * @param source the String to encode * @param dest a Writer that receives the encoded result * * @return the encoded String */ static public void encode(String source, Writer dest) throws IOException { encodeUri(source, 0, source.length(), dest); } /** * Extract and encode a portion of a String. * * @param source the String to encode * @param beginIndex the begin index, inclusive * @param endIndex the end index, exclusive * @param dest a Writer that receives the encoded result */ static public void encode(String source, int beginIndex, int endIndex, Writer dest) throws IOException { encodeUri(source, beginIndex, endIndex, dest); } /** * Extract and encode a portion of a StringBuffer. * * @param source the StringBuffer to encode * @param beginIndex the begin index, inclusive * @param endIndex the end index, exclusive * @param dest a Writer that receives the encoded result */ static public void encode(StringBuffer source, int beginIndex, int endIndex, Writer dest) throws IOException { encodeUri(source, beginIndex, endIndex, dest); } static public void encodeUri(CharSequence source, int beginIndex, int endIndex, Writer dest) throws IOException { for (int i = beginIndex; i < endIndex; i++) { char ch = source.charAt(i); if ( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'z') || (ch >= '0' && ch <= '9') || ch == '-' || ch == '_' || ch == '.' || ch == '*') { dest.write(ch); } else if (ch == ' ') { dest.write('+'); } else if (ch <= 0xff) { // 8 byte (utf-8) dest.write('%'); dest.write(encodeHex(ch >> 4)); dest.write(encodeHex(ch)); } else { // 16 byte (utf-16) dest.write('%'); dest.write('u'); dest.write(encodeHex(ch >> 12)); dest.write(encodeHex(ch >> 8)); dest.write(encodeHex(ch >> 4)); dest.write(encodeHex(ch)); } } } /** * Decode a string. * * @param source the String to decode * * @return the decoded String */ static public String decode(String source) { StringBuffer dest = getStringBuffer(); decodeUri(source, 0, source.length(), dest); String result = dest.toString(); releaseStringBuffer(dest); return result; } /** * Decode a string. * * @param source the String to decode * @param dest a StringBuffer that receives the decoded result */ static public void decode(String source, StringBuffer dest) { decodeUri(source, 0, source.length(), dest); } /** * Extract and decode an encoded portion of a string. * * @param source the String to extract from * @param beginIndex the begin index, inclusive * @param endIndex the end index, exclusive * @param dest a StringBuffer that receives the decoded result */ static public void decode(String source, int beginIndex, int endIndex, StringBuffer dest) { decodeUri(source, beginIndex, endIndex, dest); } /** * Extract and decode an encoded portion of a StringBuffer. * * @param source the StringBuffer to extract from * @param beginIndex the begin index, inclusive * @param endIndex the end index, exclusive * @param dest a StringBuffer that receives the decoded result */ static public void decode(StringBuffer source, int beginIndex, int endIndex, StringBuffer dest) { decodeUri(source, beginIndex, endIndex, dest); } static private void decodeUri(CharSequence source, int beginIndex, int endIndex, StringBuffer dest) { int i = beginIndex; while (i < endIndex) { char ch = source.charAt(i); if (ch == '%') i = scanUriEscape(source, i + 1, endIndex, dest); else if (ch == '+') { dest.append(' '); i++; } else { dest.append(ch); i++; } } } private static int scanUriEscape(CharSequence source, int i, int len, StringBuffer dest ) { int ch1 = i < len ? ( ((int)source.charAt(i++)) & 0xff) : -1; if (ch1 == 'u') { ch1 = i < len ? ( ((int)source.charAt(i++)) & 0xff) : -1; int ch2 = i < len ? ( ((int)source.charAt(i++)) & 0xff) : -1; int ch3 = i < len ? ( ((int)source.charAt(i++)) & 0xff) : -1; int ch4 = i < len ? ( ((int)source.charAt(i++)) & 0xff) : -1; dest.append((char) ((decodeHex(ch1) << 12) + (decodeHex(ch2) << 8) + (decodeHex(ch3) << 4) + (decodeHex(ch4)))); } else { int ch2 = i < len ? ( ((int)source.charAt(i++)) & 0xff) : -1; int b = (decodeHex(ch1) << 4) + decodeHex(ch2);; dest.append( (char) b); } return i; } static char encodeHex(int ch) { ch &= 0xf; if (ch < 10) return (char) (ch + '0'); else return (char) (ch + 'a' - 10); } private static int decodeHex(int ch) { if (ch >= '0' && ch <= '9') return ch - '0'; else if (ch >= 'a' && ch <= 'f') return ch - 'a' + 10; else if (ch >= 'A' && ch <= 'F') return ch - 'A' + 10; else return -1; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?