1 28 29 package com.caucho.util; 30 31 34 public class URLUtil { 35 38 public static String encodeURL(String uri) 39 { 40 CharBuffer cb = CharBuffer.allocate(); 41 42 for (int i = 0; i < uri.length(); i++) { 43 char ch = uri.charAt(i); 44 45 switch (ch) { 46 case '<': 47 case '>': 48 case ' ': 49 case '%': 50 case '\'': 51 case '\"': 52 cb.append('%'); 53 cb.append(encodeHex(ch >> 4)); 54 cb.append(encodeHex(ch)); 55 break; 56 57 default: 58 cb.append(ch); 59 } 60 } 61 62 return cb.close(); 63 } 64 65 public static String byteToHex(int b) 66 { 67 CharBuffer cb = CharBuffer.allocate(); 68 69 cb.append(encodeHex((b & 0xf0) >> 4)); 70 cb.append(encodeHex(b & 0xf)); 71 72 return cb.close(); 73 } 74 75 public static char encodeHex(int ch) 76 { 77 ch &= 0xf; 78 79 if (ch < 10) 80 return (char) (ch + '0'); 81 else 82 return (char) (ch + 'a' - 10); 83 } 84 } 85 | Popular Tags |