java.net.IDN
Provides methods to convert internationalized domain names (IDNs) between a normal Unicode representation and an ASCII Compatible Encoding (ACE) representation. Internationalized domain names can use characters from the entire range of Unicode. In contrast, traditional domain names are restricted to ASCII characters. ACE is an encoding of Unicode strings that uses only ASCII characters and can be used with software (such as the Domain Name System) that only understands traditional domain names.
Internationalized domain names are defined in RFC 3490.RFC 3490 defines two operations: ToASCII and ToUnicode. These 2 operations employ the Name prep algorithm, a profile of String prep, and the Puny code algorithm to convert domain name string back and forth.
The behavior of the aforementioned conversion process can be adjusted by various flags:
1. If the ALLOW_UNASSIGNED flag is used, the domain name string to be converted can contain code points that are unassigned in Unicode 3.2, which is the Unicode version on which IDN conversion is based. If the flag is not used, the presence of such unassigned code points is treated as an error.
2. If the USE_STD3_ASCII_RULES flag is used, ASCII strings are checked against RFC 1122 and RFC 1123.It is an error if they don't meet the requirements.
These flags can be logically OR'ed together.
Some methods of IDN class
1. IDN.ALLOW_UNASSIGNED
int java.net.IDN.ALLOW_UNASSIGNED: 1 [0x1]
Flag to allow processing of unassigned code points.
Approach
Java
package com.IDN;import java.net.IDN;public class IDNALLOW_UNASSIGNED {public static void main(String[] args) {System.out.println(IDN.ALLOW_UNASSIGNED);}}
Output:
1
2. IDN.toASCII(String input)
String java.net.IDN.toASCII(String input)
Translates a string from Unicode to ASCII Compatible Encoding (ACE),as defined by the ToASCII operation of RFC 3490.
Parameters: One parameter is required for this method.
input: the string to be processed.
Returns: the translated String.
Throws:
1. IllegalArgumentException - if the input string doesn't conform to RFC 3490 specification.
Approach
Java
package com.IDN;import java.net.IDN;public class IDNtoASCII {public static void main(String[] args) {String input = "hello";System.out.println(IDN.toASCII(input));}}
Output:
hello
3. IDN.toASCII(String input, int flag)
String java.net.IDN.toASCII(String input, int flag)
Translates a string from Unicode to ASCII Compatible Encoding (ACE), as defined by the ToASCII operation of RFC 3490.
ToASCII operation can fail. ToASCII fails if any step of it fails. If the ToASCII operation fails, an IllegalArgumentException will be thrown. In this case, the input string should not be used in an internationalized domain name.
Parameters: Two parameters are required for this method.
input: the string to be processed.
flag: process flag; can be 0 or any logical OR of possible flags.
Returns: the translated String.
Throws:
1. IllegalArgumentException - if the input string doesn't conform to RFC 3490 specification.
Approach
Java
package com.IDN;import java.net.IDN;public class IDNtoASCII2 {public static void main(String[] args) {String input = "hello";int flag = 1;System.out.println(IDN.toASCII(input, flag));}}
Output:
hello
4. IDN.toUnicode(String input)
String java.net.IDN.toUnicode(String input)
Translates a string from ASCII Compatible Encoding (ACE) to Unicode, as defined by the ToUnicode operation of RFC 3490.
Parameters: One parameter is required for this method.
input: the string to be processed.
Returns: the translated String
Approach
Java
package com.IDN;import java.net.IDN;public class IDNtoUnicode {public static void main(String[] args) {String input = "hello";System.out.println(IDN.toUnicode(input));}}
Output:
hello
5.
String java.net.IDN.toUnicode(String input, int flag)
Translates a string from ASCII Compatible Encoding (ACE) to Unicode, as defined by the ToUnicode operation of RFC 3490.
ToUnicode never fails. In case of any error, the input string is returned unmodified.
Parameters: Two parameters are required for this method.
input: the string to be processed.
flag: process flag; can be 0 or any logical OR of possible flags.
Returns: the translated String
Approach
Java
package com.IDN;import java.net.IDN;public class IDNtoUnicode2 {public static void main(String[] args) {String input = "hello";int flag = 1;System.out.println(IDN.toUnicode(input, flag));}}
Output:
hello
6. IDN.USE_STD3_ASCII_RULES
int java.net.IDN.USE_STD3_ASCII_RULES: 2 [0x2]
Flag to turn on the check against STD-3 ASCII rules.
Approach
Java
package com.IDN;import java.net.IDN;public class IDNUSE_STD3_ASCII_RULES {public static void main(String[] args) {System.out.println(IDN.USE_STD3_ASCII_RULES);}}
Output:
2
No comments:
Post a Comment