IDN Punycode ASCII And Unicode Domain Forms

Convert internationalized domain names between Unicode and ASCII Punycode via java.net.IDN, enforcing STD3 rules to reject unsafe host characters.

JavaSecurity Boundaries

Code

snippet
import java.net.IDN;

public class PunycodeDomains {

    public static void main(String[] args) {
        String[] hosts = { "bücher.example", "例え.jp", "api.example.com" };

        for (String host : hosts) {
            // toASCII encodes non-ASCII labels into RFC 3490 Punycode (xn--...)
            String ascii = IDN.toASCII(host, IDN.USE_STD3_ASCII_RULES);
            String roundtrip = IDN.toUnicode(ascii);
            System.out.println(host + " -> " + ascii + " -> " + roundtrip);
        }

        // Default mode permits underscores; USE_STD3_ASCII_RULES restricts to LDH host chars
        String permissive = IDN.toASCII("bad_host.example");
        System.out.println("permissive underscore: " + permissive);

        try {
            IDN.toASCII("bad_host.example", IDN.USE_STD3_ASCII_RULES);
        } catch (IllegalArgumentException ex) {
            System.out.println("STD3 rejected underscore: " + ex.getClass().getSimpleName());
        }
    }
}

Step-by-step analysis — IDN Punycode ASCII And Unicode Domain Forms

IDN Punycode ASCII And Unicode Domain Forms reduces to the class PunycodeDomains and the function main.

for (String host : hosts) performs sequential per-record iteration neatly across entries.

Caveats

  • Completely standalone — avoids non-bundled packages beyond core runtime standard modules.

Using it

Signature: public static void main(String[] args)

Logged lines display descriptive keys — permissive underscore:, STD3 rejected underscore: — when the file runs.

Key statements

String[] hosts = { "bücher.example", "例え.jp", "api.example.com" };
String ascii = IDN.toASCII(host, IDN.USE_STD3_ASCII_RULES);
IDN.toASCII("bad_host.example", IDN.USE_STD3_ASCII_RULES);

Interactive Tools for This Topic

FAQ

What does IDN Punycode ASCII And Unicode Domain Forms print when run?
Execute the whole file; each printed line is prefixed with a label like `permissive underscore:`, `STD3 rejected underscore:`.