10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.jndi.toolkit.url;
27
28 import java.net.MalformedURLException;
29 import java.io.UnsupportedEncodingException;
30
31 /**
32 * Utilities for dealing with URLs.
33 * @author Vincent Ryan
34 */
35
36 final public class UrlUtil {
37
38 // To prevent creation of this static class
39 private UrlUtil() {
40 }
41
42 /**
43 * Decode a URI string (according to RFC 2396).
44 */
45 public static final String decode(String s) throws MalformedURLException {
46 try {
47 return decode(s, "8859_1");
48 } catch (UnsupportedEncodingException e) {
49 // ISO-Latin-1 should always be available?
50 throw new MalformedURLException("ISO-Latin-1 decoder unavailable");
51 }
52 }
53
54 /**
55 * Decode a URI string (according to RFC 2396).
56 *
57 * Three-character sequences '%xy', where 'xy' is the two-digit
58 * hexadecimal representation of the lower 8-bits of a character,
59 * are decoded into the character itself.
60 *
61 * The string is subsequently converted using the specified encoding
62 */
63 public static final String decode(String s, String enc)
64 throws MalformedURLException, UnsupportedEncodingException {
65
66 int length = s.length();
67 byte[] bytes = new byte[length];
68 int j = 0;
69
70 for (int i = 0; i < length; i++) {
71 if (s.charAt(i) == '%') {
72 i++; // skip %
73 try {
74 bytes[j++] = (byte)
75 Integer.parseInt(s.substring(i, i + 2), 16);
76
77 } catch (Exception e) {
78 throw new MalformedURLException("Invalid URI encoding: " + s);
79 }
80 i++; // skip first hex char; for loop will skip second one
81 } else {
82 bytes[j++] = (byte) s.charAt(i);
83 }
84 }
85
86 return new String(bytes, 0, j, enc);
87 }
88
89 /**
90 * Encode a string for inclusion in a URI (according to RFC 2396).
91 *
92 * Unsafe characters are escaped by encoding them in three-character
93 * sequences '%xy', where 'xy' is the two-digit hexadecimal representation
94 * of the lower 8-bits of the character.
95 *
96 * The question mark '?' character is also escaped, as required by RFC 2255.
97 *
98 * The string is first converted to the specified encoding.
99 * For LDAP (2255), the encoding must be UTF-8.
100 */
101 public static final String encode(String s, String enc)
102 throws UnsupportedEncodingException {
103
104 byte[] bytes = s.getBytes(enc);
105 int count = bytes.length;
106
|
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.jndi.toolkit.url;
27
28 import java.net.MalformedURLException;
29 import java.io.UnsupportedEncodingException;
30 import java.net.URLDecoder;
31
32 /**
33 * Utilities for dealing with URLs.
34 * @author Vincent Ryan
35 */
36
37 final public class UrlUtil {
38
39 // To prevent creation of this static class
40 private UrlUtil() {
41 }
42
43 /**
44 * Decode a URI string (according to RFC 2396).
45 */
46 public static final String decode(String s) throws MalformedURLException {
47 try {
48 return decode(s, "8859_1");
49 } catch (UnsupportedEncodingException e) {
50 // ISO-Latin-1 should always be available?
51 throw new MalformedURLException("ISO-Latin-1 decoder unavailable");
52 }
53 }
54
55 /**
56 * Decode a URI string (according to RFC 2396).
57 *
58 * Three-character sequences '%xy', where 'xy' is the two-digit
59 * hexadecimal representation of the lower 8-bits of a character,
60 * are decoded into the character itself.
61 *
62 * The string is subsequently converted using the specified encoding
63 */
64 public static final String decode(String s, String enc)
65 throws MalformedURLException, UnsupportedEncodingException {
66 try {
67 return URLDecoder.decode(s, enc);
68 } catch (IllegalArgumentException iae) {
69 MalformedURLException mue = new MalformedURLException("Invalid URI encoding: " + s);
70 mue.initCause(iae);
71 throw mue;
72 }
73 }
74
75 /**
76 * Encode a string for inclusion in a URI (according to RFC 2396).
77 *
78 * Unsafe characters are escaped by encoding them in three-character
79 * sequences '%xy', where 'xy' is the two-digit hexadecimal representation
80 * of the lower 8-bits of the character.
81 *
82 * The question mark '?' character is also escaped, as required by RFC 2255.
83 *
84 * The string is first converted to the specified encoding.
85 * For LDAP (2255), the encoding must be UTF-8.
86 */
87 public static final String encode(String s, String enc)
88 throws UnsupportedEncodingException {
89
90 byte[] bytes = s.getBytes(enc);
91 int count = bytes.length;
92
|