1 7 8 package java.net; 9 10 import java.io.ByteArrayOutputStream ; 11 import java.io.BufferedWriter ; 12 import java.io.OutputStreamWriter ; 13 import java.io.IOException ; 14 import java.io.UnsupportedEncodingException ; 15 import java.io.CharArrayWriter ; 16 import java.nio.charset.Charset ; 17 import java.nio.charset.IllegalCharsetNameException ; 18 import java.nio.charset.UnsupportedCharsetException ; 19 import java.util.BitSet ; 20 import java.security.AccessController ; 21 import java.security.PrivilegedAction ; 22 import sun.security.action.GetBooleanAction; 23 import sun.security.action.GetPropertyAction; 24 25 66 public class URLEncoder { 67 static BitSet dontNeedEncoding; 68 static final int caseDiff = ('a' - 'A'); 69 static String dfltEncName = null; 70 71 static { 72 73 108 109 dontNeedEncoding = new BitSet (256); 110 int i; 111 for (i = 'a'; i <= 'z'; i++) { 112 dontNeedEncoding.set(i); 113 } 114 for (i = 'A'; i <= 'Z'; i++) { 115 dontNeedEncoding.set(i); 116 } 117 for (i = '0'; i <= '9'; i++) { 118 dontNeedEncoding.set(i); 119 } 120 dontNeedEncoding.set(' '); 122 dontNeedEncoding.set('-'); 123 dontNeedEncoding.set('_'); 124 dontNeedEncoding.set('.'); 125 dontNeedEncoding.set('*'); 126 127 dfltEncName = (String )AccessController.doPrivileged ( 128 new GetPropertyAction("file.encoding") 129 ); 130 } 131 132 135 private URLEncoder() { } 136 137 148 @Deprecated 149 public static String encode(String s) { 150 151 String str = null; 152 153 try { 154 str = encode(s, dfltEncName); 155 } catch (UnsupportedEncodingException e) { 156 } 158 159 return str; 160 } 161 162 184 public static String encode(String s, String enc) 185 throws UnsupportedEncodingException { 186 187 boolean needToChange = false; 188 StringBuffer out = new StringBuffer (s.length()); 189 Charset charset; 190 CharArrayWriter charArrayWriter = new CharArrayWriter (); 191 192 if (enc == null) 193 throw new NullPointerException ("charsetName"); 194 195 try { 196 charset = Charset.forName(enc); 197 } catch (IllegalCharsetNameException e) { 198 throw new UnsupportedEncodingException (enc); 199 } catch (UnsupportedCharsetException e) { 200 throw new UnsupportedEncodingException (enc); 201 } 202 203 for (int i = 0; i < s.length();) { 204 int c = (int) s.charAt(i); 205 if (dontNeedEncoding.get(c)) { 207 if (c == ' ') { 208 c = '+'; 209 needToChange = true; 210 } 211 out.append((char)c); 213 i++; 214 } else { 215 do { 217 charArrayWriter.write(c); 218 226 if (c >= 0xD800 && c <= 0xDBFF) { 227 231 if ( (i+1) < s.length()) { 232 int d = (int) s.charAt(i+1); 233 237 if (d >= 0xDC00 && d <= 0xDFFF) { 238 243 charArrayWriter.write(d); 244 i++; 245 } 246 } 247 } 248 i++; 249 } while (i < s.length() && !dontNeedEncoding.get((c = (int) s.charAt(i)))); 250 251 charArrayWriter.flush(); 252 String str = new String (charArrayWriter.toCharArray()); 253 byte[] ba = str.getBytes(charset.name()); 254 for (int j = 0; j < ba.length; j++) { 255 out.append('%'); 256 char ch = Character.forDigit((ba[j] >> 4) & 0xF, 16); 257 if (Character.isLetter(ch)) { 260 ch -= caseDiff; 261 } 262 out.append(ch); 263 ch = Character.forDigit(ba[j] & 0xF, 16); 264 if (Character.isLetter(ch)) { 265 ch -= caseDiff; 266 } 267 out.append(ch); 268 } 269 charArrayWriter.reset(); 270 needToChange = true; 271 } 272 } 273 274 return (needToChange? out.toString() : s); 275 } 276 } 277 | Popular Tags |