⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 urlencoder.java

📁 struts spring ibatis
💻 JAVA
字号:
package com.struts2.framework.util;

import java.io.*;
import java.security.AccessController;
import java.util.BitSet;
import sun.security.action.GetPropertyAction;

public class URLEncoder {

	/**
	 * @deprecated Method encode is deprecated
	 */

	public static String encode(String s) {
		String str = null;
		try {
			str = encode(s, dfltEncName);
		} catch (UnsupportedEncodingException unsupportedencodingexception) {
		}
		return str;
	}

	public static String encode(String s, String enc)
			throws UnsupportedEncodingException {
		boolean needToChange = false;
		boolean wroteUnencodedChar = false;
		int maxBytesPerChar = 10;
		StringBuffer out = new StringBuffer(s.length());
		ByteArrayOutputStream buf = new ByteArrayOutputStream(maxBytesPerChar);
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(buf,
				enc));
		for (int i = 0; i < s.length(); i++) {
			int c = s.charAt(i);
			if (dontNeedEncoding.get(c)) {
				if (c == 32) {
					c = 43;
					needToChange = true;
				}
				out.append((char) c);
				wroteUnencodedChar = true;
				continue;
			}
			try {
				if (wroteUnencodedChar) {
					writer = new BufferedWriter(
							new OutputStreamWriter(buf, enc));
					wroteUnencodedChar = false;
				}
				writer.write(c);
				if (c >= 55296 && c <= 56319 && i + 1 < s.length()) {
					int d = s.charAt(i + 1);
					if (d >= 56320 && d <= 57343) {
						writer.write(d);
						i++;
					}
				}
				writer.flush();
			} catch (IOException ioexception) {
				buf.reset();
				continue;
			}
			byte ba[] = buf.toByteArray();
			for (int j = 0; j < ba.length; j++) {
				out.append('%');
				char ch = Character.forDigit(ba[j] >> 4 & 0xf, 16);
				if (Character.isLetter(ch))
					ch -= ' ';
				out.append(ch);
				ch = Character.forDigit(ba[j] & 0xf, 16);
				if (Character.isLetter(ch))
					ch -= ' ';
				out.append(ch);
			}

			buf.reset();
			needToChange = true;
		}

		return needToChange ? out.toString() : s;
	}

	private URLEncoder() {
	}

	static BitSet dontNeedEncoding;

	static final int caseDiff = 32;

	static String dfltEncName = null;

	static {
		dontNeedEncoding = new BitSet(256);
		for (int i = 97; i <= 122; i++)
			dontNeedEncoding.set(i);

		for (int i = 65; i <= 90; i++)
			dontNeedEncoding.set(i);

		for (int i = 48; i <= 57; i++)
			dontNeedEncoding.set(i);

		dontNeedEncoding.set(32);
		dontNeedEncoding.set(45);
		dontNeedEncoding.set(95);
		dontNeedEncoding.set(46);
		dontNeedEncoding.set(42);
		dfltEncName = (String) AccessController
				.doPrivileged(new GetPropertyAction("file.encoding"));
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -