fork download
  1. package myapp.util;
  2.  
  3. import java.io.IOException;
  4.  
  5. public final class UrlUtil {
  6. public static final String ENC_SJIS = "SJIS";
  7. public static final String ENC_UTF8 = "UTF-8";
  8.  
  9. private UrlUtil() {
  10. }
  11.  
  12. public static final String urlencode(String src) throws IOException {
  13. return UrlUtil.urlencode(src, UrlUtil.ENC_SJIS);
  14. }
  15.  
  16. public static final String urlencode(String src, String enc) throws IOException {
  17. byte[] data = src.getBytes(enc);
  18. int n = data.length;
  19. StringBuffer buf = new StringBuffer(n);
  20. for (int i = 0; i < n; i++) {
  21. int b = 0xFF & (int)data[i];
  22. if ((b < 0x21) || (b > 0x7E)) {
  23. buf.append('%');
  24. if (b < 0x10) {
  25. buf.append('0');
  26. }
  27. buf.append(Integer.toHexString(b).toUpperCase());
  28. } else {
  29. char c = (char)b;
  30. if ((Character.digit(c, 36)) == -1) {
  31. switch (c) {
  32. case '_':
  33. case '~':
  34. case '.':
  35. case '-':
  36. buf.append(c);
  37. break;
  38. default:
  39. buf.append('%');
  40. buf.append(Integer.toHexString(b).toUpperCase());
  41. break;
  42. }
  43. } else {
  44. buf.append(c);
  45. }
  46. }
  47. }
  48. return buf.toString();
  49. }
  50. }
  51.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty