diff options
author | Warren Levy <warrenl@cygnus.com> | 1999-04-23 16:36:22 +0000 |
---|---|---|
committer | Warren Levy <warrenl@gcc.gnu.org> | 1999-04-23 16:36:22 +0000 |
commit | 12571b1f964b2fcbae5f66b6307c31854ed8c0d5 (patch) | |
tree | 824800dc6994edffba9ebed90251cddc6b816ef0 /libjava/java/net/URLEncoder.java | |
parent | d2692ef86e8ea0191975319469e8b1b3b2a846d4 (diff) | |
download | gcc-12571b1f964b2fcbae5f66b6307c31854ed8c0d5.zip gcc-12571b1f964b2fcbae5f66b6307c31854ed8c0d5.tar.gz gcc-12571b1f964b2fcbae5f66b6307c31854ed8c0d5.tar.bz2 |
Makefile.am: Added URLDecoder and URLEncoder.
* Makefile.am: Added URLDecoder and URLEncoder.
* Makefile.in: Rebuilt.
* java/net/ServerSocket.java (setSocketFactory): Renamed from
setSocketImplFactory to match spec.
* java/net/Socket.java (getSoLinger): Changed return type to
match spec.
* java/net/URLDecoder.java: New file.
* java/net/URLEncoder.java: New file.
From-SVN: r26605
Diffstat (limited to 'libjava/java/net/URLEncoder.java')
-rw-r--r-- | libjava/java/net/URLEncoder.java | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/libjava/java/net/URLEncoder.java b/libjava/java/net/URLEncoder.java new file mode 100644 index 0000000..83295ea --- /dev/null +++ b/libjava/java/net/URLEncoder.java @@ -0,0 +1,71 @@ +// URLEncoder.java - Provides a method for encoding strings according to +// application/x-www-form-urlencoded MIME type. + +/* Copyright (C) 1999 Cygnus Solutions + + This file is part of libgcj. + +This software is copyrighted work licensed under the terms of the +Libgcj License. Please consult the file "LIBGCJ_LICENSE" for +details. */ + +package java.net; +import java.io.UnsupportedEncodingException; + +/** + * @author Warren Levy <warrenl@cygnus.com> + * @date April 22, 1999. + */ + +/** + * Written using on-line Java Platform 1.2 API Specification, as well + * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998). + * Status: Believed complete and correct. + */ + +public class URLEncoder +{ + // This method, per the JCL, is conservative in that it encodes + // some "allowable" characters as % triplets. + public static String encode(String s) + { + // Get the bytes in ISO-Latin-1 (i.e. 8859_1) per the JCL. + // Even though it is the default in most cases, it's specified here + // just in case System.getProperty("file.encoding") is not "8859_1". + String result = ""; + try + { + byte[] buf = s.getBytes("8859_1"); + int start = 0; + for (int i = 0; i < buf.length; i++) + // For efficiency, check the byte in order of most likely + // possibility so as to minimize the number of comparisons. + // Hence, exclude all the alphanumeric & allowed special chars first. + if ((buf[i] >= 'a' && buf[i] <= 'z') || + (buf[i] >= 'A' && buf[i] <= 'Z') || + (buf[i] >= '0' && buf[i] <= '9') || + buf[i] == '-' || buf[i] == '_' || buf[i] == '.' || buf[i] == '*') + ; // This is the most likely case so exclude first for efficiency. + else if (buf[i] == ' ') + buf[i] = (byte) '+'; // Replace space char with plus symbol. + else + { + result = result + new String(buf, start, i - start, "8859_1") + + "%" + Integer.toHexString(((int) buf[i]) & 0xFF); + start = i + 1; + } + + // Append remainder of allowable chars from the string, if any. + if (start < buf.length) + result = result + + new String(buf, start, buf.length - start, "8859_1"); + } + catch (UnsupportedEncodingException ex) + { + // This should never happen as "8859_1" is the default encoding. + return s; + } + + return result; + } +} |