* Makefile.in (dbxout.o): Depend on ggc.h.
[official-gcc.git] / libjava / java / net / URLEncoder.java
blob83295ea57de27ba15a534f7900144126bffa76f6
1 // URLEncoder.java - Provides a method for encoding strings according to
2 // application/x-www-form-urlencoded MIME type.
4 /* Copyright (C) 1999 Cygnus Solutions
6 This file is part of libgcj.
8 This software is copyrighted work licensed under the terms of the
9 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
10 details. */
12 package java.net;
13 import java.io.UnsupportedEncodingException;
15 /**
16 * @author Warren Levy <warrenl@cygnus.com>
17 * @date April 22, 1999.
20 /**
21 * Written using on-line Java Platform 1.2 API Specification, as well
22 * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
23 * Status: Believed complete and correct.
26 public class URLEncoder
28 // This method, per the JCL, is conservative in that it encodes
29 // some "allowable" characters as % triplets.
30 public static String encode(String s)
32 // Get the bytes in ISO-Latin-1 (i.e. 8859_1) per the JCL.
33 // Even though it is the default in most cases, it's specified here
34 // just in case System.getProperty("file.encoding") is not "8859_1".
35 String result = "";
36 try
38 byte[] buf = s.getBytes("8859_1");
39 int start = 0;
40 for (int i = 0; i < buf.length; i++)
41 // For efficiency, check the byte in order of most likely
42 // possibility so as to minimize the number of comparisons.
43 // Hence, exclude all the alphanumeric & allowed special chars first.
44 if ((buf[i] >= 'a' && buf[i] <= 'z') ||
45 (buf[i] >= 'A' && buf[i] <= 'Z') ||
46 (buf[i] >= '0' && buf[i] <= '9') ||
47 buf[i] == '-' || buf[i] == '_' || buf[i] == '.' || buf[i] == '*')
48 ; // This is the most likely case so exclude first for efficiency.
49 else if (buf[i] == ' ')
50 buf[i] = (byte) '+'; // Replace space char with plus symbol.
51 else
53 result = result + new String(buf, start, i - start, "8859_1") +
54 "%" + Integer.toHexString(((int) buf[i]) & 0xFF);
55 start = i + 1;
58 // Append remainder of allowable chars from the string, if any.
59 if (start < buf.length)
60 result = result +
61 new String(buf, start, buf.length - start, "8859_1");
63 catch (UnsupportedEncodingException ex)
65 // This should never happen as "8859_1" is the default encoding.
66 return s;
69 return result;