1 23 24 25 package org.apache.slide.webdav.util; 26 27 import java.io.ByteArrayOutputStream ; 28 import java.io.IOException ; 29 import java.io.OutputStreamWriter ; 30 import java.io.UnsupportedEncodingException ; 31 import java.text.SimpleDateFormat ; 32 import java.util.BitSet ; 33 import java.util.TimeZone ; 34 35 36 41 42 public final class URLUtil { 43 44 45 47 48 51 private static SimpleDateFormat format = 52 new SimpleDateFormat (" EEEE, dd-MMM-yy kk:mm:ss zz"); 53 54 55 58 protected static BitSet safeCharacters; 59 60 61 protected static final char[] hexadecimal = 62 {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 63 'A', 'B', 'C', 'D', 'E', 'F'}; 64 65 66 68 69 static { 70 71 format.setTimeZone(TimeZone.getTimeZone("GMT")); 72 73 safeCharacters = new BitSet (256); 74 int i; 75 for (i = 'a'; i <= 'z'; i++) { 76 safeCharacters.set(i); 77 } 78 for (i = 'A'; i <= 'Z'; i++) { 79 safeCharacters.set(i); 80 } 81 for (i = '0'; i <= '9'; i++) { 82 safeCharacters.set(i); 83 } 84 safeCharacters.set('-'); 85 safeCharacters.set('_'); 86 safeCharacters.set('.'); 87 safeCharacters.set('*'); 88 safeCharacters.set('/'); 89 90 } 91 92 93 95 96 107 public static String URLDecode(String str) { 108 109 return URLDecode(str, null); 110 111 } 112 113 114 122 public static String URLDecode(String str, String enc) { 123 124 if (str == null) 125 return (null); 126 127 byte[] bytes; 129 if (enc==null) { 130 bytes=str.getBytes(); 131 } 132 else { 133 try { 134 bytes=str.getBytes(enc); 135 } 136 catch (UnsupportedEncodingException ex) { 137 bytes=str.getBytes(); 138 } 139 } 140 141 return URLDecode(bytes, enc); 142 } 143 144 145 152 public static String URLDecode(byte[] bytes) { 153 return URLDecode(bytes, null); 154 } 155 156 157 165 public static String URLDecode(byte[] bytes, String enc) { 166 167 if (bytes == null) 168 return (null); 169 170 int len = bytes.length; 171 int ix = 0; 172 int ox = 0; 173 while (ix < len) { 174 byte b = bytes[ix++]; if (b == '%') { 176 b = (byte) ((convertHexDigit(bytes[ix++]) << 4) 177 + convertHexDigit(bytes[ix++])); 178 } 179 if (b == '+') { 180 b = (byte)' '; 181 } 182 183 bytes[ox++] = b; 184 } 185 if (enc != null) { 186 try { 187 return new String (bytes, 0, ox, enc); 188 } catch (Exception e) { 189 e.printStackTrace(); 190 } 191 } 192 return new String (bytes, 0, ox); 193 194 } 195 196 197 202 private static byte convertHexDigit( byte b ) { 203 if ((b >= '0') && (b <= '9')) return (byte)(b - '0'); 204 if ((b >= 'a') && (b <= 'f')) return (byte)(b - 'a' + 10); 205 if ((b >= 'A') && (b <= 'F')) return (byte)(b - 'A' + 10); 206 return 0; 207 } 208 209 210 215 public static String URLEncode(String path, String enc) { 216 217 223 224 int maxBytesPerChar = 10; 225 StringBuffer rewrittenPath = new StringBuffer (path.length()); 226 ByteArrayOutputStream buf = new ByteArrayOutputStream (maxBytesPerChar); 227 OutputStreamWriter writer = null; 228 try { 229 writer = new OutputStreamWriter (buf, enc); 231 } catch (Exception e) { 232 e.printStackTrace(); 233 writer = new OutputStreamWriter (buf); 234 } 235 236 for (int i = 0; i < path.length(); i++) { 237 int c = (int) path.charAt(i); 238 if (safeCharacters.get(c)) { 239 rewrittenPath.append((char)c); 240 } else { 241 try { 243 writer.write(c); 244 writer.flush(); 245 } catch(IOException e) { 246 buf.reset(); 247 continue; 248 } 249 byte[] ba = buf.toByteArray(); 250 for (int j = 0; j < ba.length; j++) { 251 byte toEncode = ba[j]; 253 rewrittenPath.append('%'); 254 int low = (int) (toEncode & 0x0f); 255 int high = (int) ((toEncode & 0xf0) >> 4); 256 rewrittenPath.append(hexadecimal[high]); 257 rewrittenPath.append(hexadecimal[low]); 258 } 259 buf.reset(); 260 } 261 } 262 263 return rewrittenPath.toString(); 264 265 } 266 267 268 } 269 270 271 | Popular Tags |