Java ME平台中的URLEncoder实现类 - 编程入门网
- ''A'');
static {
dontNeedEncoding = new boolean[256];
int i;
for (i = ''a''; i <= ''z''; i++) {
dontNeedEncoding[i] = true;
}
for (i = ''A''; i <= ''Z''; i++) {
dontNeedEncoding[i] = true;
}
for (i = ''0''; i <= ''9''; i++) {
dontNeedEncoding[i] = true;
}
dontNeedEncoding['' ''] = true; // encoding a space to a + is done in the encode() method
dontNeedEncoding[''-''] = true;
dontNeedEncoding[''_''] = true;
dontNeedEncoding[''.''] = true;
dontNeedEncoding[''*''] = true;
defaultEncName = System.getProperty("microedition.encoding");
if(defaultEncName == null || defaultEncName.trim().length() == 0){
defaultEncName = "UTF-8";
}
}
public static final int MIN_RADIX = 2; /** * The maximum radix available for conversion to and from strings. */ public static final int MAX_RADIX = 36; /** * The class is not meant to be instantiated. */ private URLEncoder() { } Java ME平台中的URLEncoder实现类(2)时间:2007-05-30 j2medev/** * Translates a string into "<CODE>x-www-form-urlencoded</CODE>" * format.This method uses the platform''s default encoding * as the encoding scheme to obtain the bytes for unsafe characters. * * @param s the string to be translated. * * @return The resulting string. */ public static String encode(String s) { String str = null; str = encode(s, defaultEncName); return str; } /** * Translates a string into <code>application/x-www-form-urlencoded</code> * format using a specific encoding scheme. This method uses the * supplied encoding scheme to obtain the bytes for unsafe * characters. * <p> * <em><strong>Note:</strong> The <a href= * "http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars"> * World Wide Web Consortium Recommendation</a> states that * UTF-8 should be used. Not doing so may introduce * incompatibilites.</em> * * @param s <code>String</code> to be translated. * @param enc The name of a supported character encoding such as UTF-8 * @return the translated <code>String</code>. */ public static String encode(String s, String enc) { boolean needToChange = false; boolean wroteUnencodedChar = false; int maxBytesPerChar = 10; // rather arbitrary limit, but safe for now StringBuffer out = new StringBuffer(s.length()); ByteArrayOutputStream buf = new ByteArrayOutputStream(maxBytesPerChar); OutputStreamWriter writer = null; try { writer = new OutputStreamWriter(buf, enc); } catch (UnsupportedEncodingException ex) { try { writer = new OutputStreamWriter(buf,defaultEncName); } catch (UnsupportedEncodingException e) { //never reach } } for (int i = 0; i < s.length(); i++) { int c = (int) s.charAt(i); //System.out.println("Examining character: " + c); if (c <256 && dontNeedEncoding[c]) { if (c == '' '') { c = ''+''; needToChange = true; } //System.out.println("Storing: " + c); out.append((char)c); wroteUnencodedChar = true; } else { // convert to e |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |