1 16 17 package org.apache.commons.codec.net; 18 19 import java.io.ByteArrayOutputStream ; 20 import java.io.UnsupportedEncodingException ; 21 import java.util.BitSet ; 22 23 import org.apache.commons.codec.BinaryDecoder; 24 import org.apache.commons.codec.BinaryEncoder; 25 import org.apache.commons.codec.DecoderException; 26 import org.apache.commons.codec.EncoderException; 27 import org.apache.commons.codec.StringDecoder; 28 import org.apache.commons.codec.StringEncoder; 29 30 50 public class URLCodec implements BinaryEncoder, BinaryDecoder, StringEncoder, StringDecoder { 51 52 55 protected String charset = StringEncodings.UTF8; 56 57 protected static byte ESCAPE_CHAR = '%'; 58 61 protected static final BitSet WWW_FORM_URL = new BitSet (256); 62 63 static { 65 for (int i = 'a'; i <= 'z'; i++) { 67 WWW_FORM_URL.set(i); 68 } 69 for (int i = 'A'; i <= 'Z'; i++) { 70 WWW_FORM_URL.set(i); 71 } 72 for (int i = '0'; i <= '9'; i++) { 74 WWW_FORM_URL.set(i); 75 } 76 WWW_FORM_URL.set('-'); 78 WWW_FORM_URL.set('_'); 79 WWW_FORM_URL.set('.'); 80 WWW_FORM_URL.set('*'); 81 WWW_FORM_URL.set(' '); 83 } 84 85 86 89 public URLCodec() { 90 super(); 91 } 92 93 98 public URLCodec(String charset) { 99 super(); 100 this.charset = charset; 101 } 102 103 111 public static final byte[] encodeUrl(BitSet urlsafe, byte[] bytes) 112 { 113 if (bytes == null) { 114 return null; 115 } 116 if (urlsafe == null) { 117 urlsafe = WWW_FORM_URL; 118 } 119 120 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 121 for (int i = 0; i < bytes.length; i++) { 122 int b = bytes[i]; 123 if (b < 0) { 124 b = 256 + b; 125 } 126 if (urlsafe.get(b)) { 127 if (b == ' ') { 128 b = '+'; 129 } 130 buffer.write(b); 131 } else { 132 buffer.write('%'); 133 char hex1 = Character.toUpperCase( 134 Character.forDigit((b >> 4) & 0xF, 16)); 135 char hex2 = Character.toUpperCase( 136 Character.forDigit(b & 0xF, 16)); 137 buffer.write(hex1); 138 buffer.write(hex2); 139 } 140 } 141 return buffer.toByteArray(); 142 } 143 144 145 154 public static final byte[] decodeUrl(byte[] bytes) 155 throws DecoderException 156 { 157 if (bytes == null) { 158 return null; 159 } 160 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 161 for (int i = 0; i < bytes.length; i++) { 162 int b = bytes[i]; 163 if (b == '+') { 164 buffer.write(' '); 165 } else if (b == '%') { 166 try { 167 int u = Character.digit((char)bytes[++i], 16); 168 int l = Character.digit((char)bytes[++i], 16); 169 if (u == -1 || l == -1) { 170 throw new DecoderException("Invalid URL encoding"); 171 } 172 buffer.write((char)((u << 4) + l)); 173 } catch(ArrayIndexOutOfBoundsException e) { 174 throw new DecoderException("Invalid URL encoding"); 175 } 176 } else { 177 buffer.write(b); 178 } 179 } 180 return buffer.toByteArray(); 181 } 182 183 184 191 public byte[] encode(byte[] bytes) { 192 return encodeUrl(WWW_FORM_URL, bytes); 193 } 194 195 196 205 public byte[] decode(byte[] bytes) throws DecoderException { 206 return decodeUrl(bytes); 207 } 208 209 210 220 public String encode(String pString, String charset) 221 throws UnsupportedEncodingException 222 { 223 if (pString == null) { 224 return null; 225 } 226 return new String (encode(pString.getBytes(charset)), StringEncodings.US_ASCII); 227 } 228 229 230 240 public String encode(String pString) throws EncoderException { 241 if (pString == null) { 242 return null; 243 } 244 try { 245 return encode(pString, getDefaultCharset()); 246 } catch(UnsupportedEncodingException e) { 247 throw new EncoderException(e.getMessage()); 248 } 249 } 250 251 252 264 public String decode(String pString, String charset) 265 throws DecoderException, UnsupportedEncodingException 266 { 267 if (pString == null) { 268 return null; 269 } 270 return new String (decode(pString.getBytes(StringEncodings.US_ASCII)), charset); 271 } 272 273 274 285 public String decode(String pString) throws DecoderException { 286 if (pString == null) { 287 return null; 288 } 289 try { 290 return decode(pString, getDefaultCharset()); 291 } catch(UnsupportedEncodingException e) { 292 throw new DecoderException(e.getMessage()); 293 } 294 } 295 296 306 public Object encode(Object pObject) throws EncoderException { 307 if (pObject == null) { 308 return null; 309 } else if (pObject instanceof byte[]) { 310 return encode((byte[])pObject); 311 } else if (pObject instanceof String ) { 312 return encode((String )pObject); 313 } else { 314 throw new EncoderException("Objects of type " + 315 pObject.getClass().getName() + " cannot be URL encoded"); 316 317 } 318 } 319 320 330 public Object decode(Object pObject) throws DecoderException { 331 if (pObject == null) { 332 return null; 333 } else if (pObject instanceof byte[]) { 334 return decode((byte[])pObject); 335 } else if (pObject instanceof String ) { 336 return decode((String )pObject); 337 } else { 338 throw new DecoderException("Objects of type " + 339 pObject.getClass().getName() + " cannot be URL decoded"); 340 341 } 342 } 343 344 351 public String getEncoding() { 352 return this.charset; 353 } 354 355 360 public String getDefaultCharset() { 361 return this.charset; 362 } 363 364 } 365 | Popular Tags |