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