1 25 package org.archive.net; 26 27 import java.io.ByteArrayOutputStream ; 28 import java.io.UnsupportedEncodingException ; 29 import java.util.BitSet ; 30 31 import org.apache.commons.codec.net.URLCodec; 32 33 36 public class LaxURLCodec extends URLCodec { 37 public static LaxURLCodec DEFAULT = new LaxURLCodec("UTF-8"); 38 39 public LaxURLCodec(String encoding) { 41 super(encoding); 42 } 43 44 58 public static final byte[] decodeUrlLoose(byte[] bytes) 59 { 60 if (bytes == null) { 61 return null; 62 } 63 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 64 for (int i = 0; i < bytes.length; i++) { 65 int b = bytes[i]; 66 if (b == '+') { 67 buffer.write(' '); 68 continue; 69 } 70 if (b == '%') { 71 if(i+2<bytes.length) { 72 int u = Character.digit((char)bytes[i+1], 16); 73 int l = Character.digit((char)bytes[i+2], 16); 74 if (u > -1 && l > -1) { 75 int c = ((u << 4) + l); 77 buffer.write((char)c); 78 i += 2; 79 continue; 80 } } } 83 buffer.write(b); 84 } 85 return buffer.toByteArray(); 86 } 87 88 92 public static BitSet EXPANDED_URI_SAFE = new BitSet (256); 93 static { 94 for (int i = 'a'; i <= 'z'; i++) { 96 EXPANDED_URI_SAFE.set(i); 97 } 98 for (int i = 'A'; i <= 'Z'; i++) { 99 EXPANDED_URI_SAFE.set(i); 100 } 101 for (int i = '0'; i <= '9'; i++) { 103 EXPANDED_URI_SAFE.set(i); 104 } 105 EXPANDED_URI_SAFE.set('-'); 107 EXPANDED_URI_SAFE.set('~'); 108 EXPANDED_URI_SAFE.set('_'); 109 EXPANDED_URI_SAFE.set('.'); 110 EXPANDED_URI_SAFE.set('*'); 111 EXPANDED_URI_SAFE.set('/'); 112 EXPANDED_URI_SAFE.set('='); 113 EXPANDED_URI_SAFE.set('&'); 114 EXPANDED_URI_SAFE.set('+'); 115 EXPANDED_URI_SAFE.set(','); 116 EXPANDED_URI_SAFE.set(':'); 117 EXPANDED_URI_SAFE.set(';'); 118 EXPANDED_URI_SAFE.set('@'); 119 EXPANDED_URI_SAFE.set('$'); 120 EXPANDED_URI_SAFE.set('!'); 121 EXPANDED_URI_SAFE.set(')'); 122 EXPANDED_URI_SAFE.set('('); 123 EXPANDED_URI_SAFE.set('%'); 125 EXPANDED_URI_SAFE.set('|'); 127 EXPANDED_URI_SAFE.set('\''); 128 } 129 130 public static BitSet QUERY_SAFE = new BitSet (256); 131 static { 132 QUERY_SAFE.or(EXPANDED_URI_SAFE); 133 QUERY_SAFE.set('{'); 135 QUERY_SAFE.set('}'); 136 QUERY_SAFE.set('['); 138 QUERY_SAFE.set(']'); 139 QUERY_SAFE.set('^'); 140 QUERY_SAFE.set('?'); 141 } 142 143 157 public String encode(BitSet safe, String pString, String cs) 158 throws UnsupportedEncodingException { 159 if (pString == null) { 160 return null; 161 } 162 return new String (encodeUrl(safe,pString.getBytes(cs)), "US-ASCII"); 163 } 164 } 165 | Popular Tags |