1 /*
2 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 package java.net;
26
27 import java.io.InputStream;
28 import java.io.IOException;
29 import java.security.AccessController;
30 import java.security.PrivilegedAction;
31
32 import sun.net.idn.StringPrep;
33 import sun.net.idn.Punycode;
34 import sun.text.normalizer.UCharacterIterator;
35
36 /**
37 * Provides methods to convert internationalized domain names (IDNs) between
38 * a normal Unicode representation and an ASCII Compatible Encoding (ACE) representation.
39 * Internationalized domain names can use characters from the entire range of
40 * Unicode, while traditional domain names are restricted to ASCII characters.
41 * ACE is an encoding of Unicode strings that uses only ASCII characters and
42 * can be used with software (such as the Domain Name System) that only
43 * understands traditional domain names.
44 *
45 * <p>Internationalized domain names are defined in <a href="http://www.ietf.org/rfc/rfc3490.txt">RFC 3490</a>.
46 * RFC 3490 defines two operations: ToASCII and ToUnicode. These 2 operations employ
47 * <a href="http://www.ietf.org/rfc/rfc3491.txt">Nameprep</a> algorithm, which is a
48 * profile of <a href="http://www.ietf.org/rfc/rfc3454.txt">Stringprep</a>, and
49 * <a href="http://www.ietf.org/rfc/rfc3492.txt">Punycode</a> algorithm to convert
50 * domain name string back and forth.
51 *
52 * <p>The behavior of aforementioned conversion process can be adjusted by various flags:
53 * <ul>
54 * <li>If the ALLOW_UNASSIGNED flag is used, the domain name string to be converted
209 public static String toUnicode(String input) {
210 return toUnicode(input, 0);
211 }
212
213
214 /* ---------------- Private members -------------- */
215
216 // ACE Prefix is "xn--"
217 private static final String ACE_PREFIX = "xn--";
218 private static final int ACE_PREFIX_LENGTH = ACE_PREFIX.length();
219
220 private static final int MAX_LABEL_LENGTH = 63;
221
222 // single instance of nameprep
223 private static StringPrep namePrep = null;
224
225 static {
226 InputStream stream = null;
227
228 try {
229 final String IDN_PROFILE = "uidna.spp";
230 if (System.getSecurityManager() != null) {
231 stream = AccessController.doPrivileged(new PrivilegedAction<>() {
232 public InputStream run() {
233 return StringPrep.class.getResourceAsStream(IDN_PROFILE);
234 }
235 });
236 } else {
237 stream = StringPrep.class.getResourceAsStream(IDN_PROFILE);
238 }
239
240 namePrep = new StringPrep(stream);
241 stream.close();
242 } catch (IOException e) {
243 // should never reach here
244 assert false;
245 }
246 }
247
248
249 /* ---------------- Private operations -------------- */
|
1 /*
2 * Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 package java.net;
26
27 import java.io.InputStream;
28 import java.io.IOException;
29 import java.security.AccessController;
30 import java.security.PrivilegedAction;
31
32 import jdk.internal.icu.impl.Punycode;
33 import jdk.internal.icu.text.StringPrep;
34 import jdk.internal.icu.text.UCharacterIterator;
35
36 /**
37 * Provides methods to convert internationalized domain names (IDNs) between
38 * a normal Unicode representation and an ASCII Compatible Encoding (ACE) representation.
39 * Internationalized domain names can use characters from the entire range of
40 * Unicode, while traditional domain names are restricted to ASCII characters.
41 * ACE is an encoding of Unicode strings that uses only ASCII characters and
42 * can be used with software (such as the Domain Name System) that only
43 * understands traditional domain names.
44 *
45 * <p>Internationalized domain names are defined in <a href="http://www.ietf.org/rfc/rfc3490.txt">RFC 3490</a>.
46 * RFC 3490 defines two operations: ToASCII and ToUnicode. These 2 operations employ
47 * <a href="http://www.ietf.org/rfc/rfc3491.txt">Nameprep</a> algorithm, which is a
48 * profile of <a href="http://www.ietf.org/rfc/rfc3454.txt">Stringprep</a>, and
49 * <a href="http://www.ietf.org/rfc/rfc3492.txt">Punycode</a> algorithm to convert
50 * domain name string back and forth.
51 *
52 * <p>The behavior of aforementioned conversion process can be adjusted by various flags:
53 * <ul>
54 * <li>If the ALLOW_UNASSIGNED flag is used, the domain name string to be converted
209 public static String toUnicode(String input) {
210 return toUnicode(input, 0);
211 }
212
213
214 /* ---------------- Private members -------------- */
215
216 // ACE Prefix is "xn--"
217 private static final String ACE_PREFIX = "xn--";
218 private static final int ACE_PREFIX_LENGTH = ACE_PREFIX.length();
219
220 private static final int MAX_LABEL_LENGTH = 63;
221
222 // single instance of nameprep
223 private static StringPrep namePrep = null;
224
225 static {
226 InputStream stream = null;
227
228 try {
229 final String IDN_PROFILE = "/sun/net/idn/uidna.spp";
230 if (System.getSecurityManager() != null) {
231 stream = AccessController.doPrivileged(new PrivilegedAction<>() {
232 public InputStream run() {
233 return StringPrep.class.getResourceAsStream(IDN_PROFILE);
234 }
235 });
236 } else {
237 stream = StringPrep.class.getResourceAsStream(IDN_PROFILE);
238 }
239
240 namePrep = new StringPrep(stream);
241 stream.close();
242 } catch (IOException e) {
243 // should never reach here
244 assert false;
245 }
246 }
247
248
249 /* ---------------- Private operations -------------- */
|