URLEncoder.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* URLEncoder.java -- Class to convert strings to a properly encoded URL
  2. Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.net;
  32. import java.io.UnsupportedEncodingException;
  33. /*
  34. * Written using on-line Java Platform 1.2/1.4 API Specification, as well
  35. * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
  36. * Status: Believed complete and correct.
  37. */
  38. /**
  39. * This utility class contains static methods that converts a
  40. * string into a fully encoded URL string in x-www-form-urlencoded
  41. * format. This format replaces certain disallowed characters with
  42. * encoded equivalents. All upper case and lower case letters in the
  43. * US alphabet remain as is, the space character (' ') is replaced with
  44. * '+' sign, and all other characters are converted to a "%XX" format
  45. * where XX is the hexadecimal representation of that character in a
  46. * certain encoding (by default "UTF-8").
  47. * <p>
  48. * This method is very useful for encoding strings to be sent to CGI scripts
  49. *
  50. * @author Aaron M. Renn (arenn@urbanophile.com)
  51. * @author Warren Levy <warrenl@cygnus.com>
  52. * @author Mark Wielaard (mark@klomp.org)
  53. */
  54. public class URLEncoder
  55. {
  56. /**
  57. * This method translates the passed in string into x-www-form-urlencoded
  58. * format using the standard "UTF-8" character encoding to hex-encode the
  59. * unsafe characters.
  60. *
  61. * @param s The String to convert
  62. *
  63. * @return The converted String
  64. */
  65. public static String encode(String s)
  66. {
  67. try
  68. {
  69. return encode(s, "UTF-8");
  70. }
  71. catch (UnsupportedEncodingException uee)
  72. {
  73. // Should never happen since UTF-8 should always be supported
  74. return s;
  75. }
  76. }
  77. /**
  78. * This method translates the passed in string into x-www-form-urlencoded
  79. * format using the character encoding to hex-encode the unsafe characters.
  80. *
  81. * @param s The String to convert
  82. * @param encoding The encoding to use for unsafe characters
  83. *
  84. * @return The converted String
  85. *
  86. * @exception UnsupportedEncodingException If the named encoding is not
  87. * supported
  88. *
  89. * @since 1.4
  90. */
  91. public static String encode(String s, String encoding)
  92. throws UnsupportedEncodingException
  93. {
  94. int length = s.length();
  95. int start = 0;
  96. int i = 0;
  97. StringBuffer result = new StringBuffer(length);
  98. while (true)
  99. {
  100. while ( i < length && isSafe(s.charAt(i)) )
  101. i++;
  102. // Safe character can just be added
  103. result.append(s.substring(start, i));
  104. // Are we done?
  105. if (i >= length)
  106. return result.toString();
  107. else if (s.charAt(i) == ' ')
  108. {
  109. result.append('+'); // Replace space char with plus symbol.
  110. i++;
  111. }
  112. else
  113. {
  114. // Get all unsafe characters
  115. start = i;
  116. char c;
  117. while ( i < length && (c = s.charAt(i)) != ' ' && !isSafe(c) )
  118. i++;
  119. // Convert them to %XY encoded strings
  120. String unsafe = s.substring(start,i);
  121. byte bytes[] = unsafe.getBytes(encoding);
  122. for (int j = 0; j < bytes.length; j++)
  123. {
  124. result.append('%');
  125. result.append(Integer.toHexString(((int) bytes[j]) & 0xFF));
  126. }
  127. }
  128. start = i;
  129. }
  130. }
  131. /**
  132. * Private static method that returns true if the given char is either
  133. * a uppercase or lowercase letter from 'a' till 'z', or a digit froim
  134. * '0' till '9', or one of the characters '-', '_', '.' or '*'. Such
  135. * 'safe' character don't have to be url encoded.
  136. */
  137. private static boolean isSafe(char c)
  138. {
  139. return ((c >= 'a' && c <= 'z') ||
  140. (c >= 'A' && c <= 'Z') ||
  141. (c >= '0' && c <= '9') ||
  142. c == '-' || c == '_' || c == '.' || c == '*');
  143. }
  144. /**
  145. * Private constructor that does nothing. Included to avoid a default
  146. * public constructor being created by the compiler.
  147. */
  148. private URLEncoder() { }
  149. } // class URLEncoder