1 11 package org.eclipse.update.internal.ui; 12 13 import java.io.ByteArrayOutputStream ; 14 import java.io.UnsupportedEncodingException ; 15 16 public class URLCoder { 17 18 public static String encode(String s) throws UnsupportedEncodingException { 19 return urlEncode(s.getBytes("UTF8")); } 21 22 public static String decode(String s) throws UnsupportedEncodingException { 23 return new String (urlDecode(s), "UTF8"); } 25 26 private static String urlEncode(byte[] data) { 27 StringBuffer buf = new StringBuffer (data.length); 28 for (int i = 0; i < data.length; i++) { 29 buf.append('%'); 30 buf.append(Character.forDigit((data[i] & 240) >>> 4, 16)); 31 buf.append(Character.forDigit(data[i] & 15, 16)); 32 } 33 return buf.toString(); 34 } 35 36 private static byte[] urlDecode(String encodedURL) { 37 int len = encodedURL.length(); 38 ByteArrayOutputStream os = new ByteArrayOutputStream (len); 39 for (int i = 0; i < len;) { 40 switch (encodedURL.charAt(i)) { 41 case '%': 42 if (len >= i + 3) { 43 os.write(Integer.parseInt(encodedURL.substring(i + 1, i + 3), 16)); 44 } 45 i += 3; 46 break; 47 case '+': os.write(' '); 49 i++; 50 break; 51 default: 52 os.write(encodedURL.charAt(i++)); 53 break; 54 } 55 56 } 57 return os.toByteArray(); 58 } 59 } 60 | Popular Tags |