From 216cbd9e315524d11fd1cc2a1172d5d37b810a92 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Sat, 6 Dec 2014 22:31:44 +0900 Subject: [PATCH] Fix Java 6 compatibility issue in DnsNameResolver Related: #3173 Motivation: DnsNameResolver was using InetSocketAddress.getHostString() which is only available since Java 7. Modifications: Use InetSocketAddress.getHostName() in lieu of getHostString() when the current Java version is less than 7. Result: DnsNameResolver runs fine on Java 6. --- .../java/io/netty/resolver/dns/DnsNameResolver.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java b/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java index 030b3716b0..1bf048e7b6 100644 --- a/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java +++ b/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java @@ -642,7 +642,7 @@ public class DnsNameResolver extends SimpleNameResolver { @Override protected void doResolve(InetSocketAddress unresolvedAddress, Promise promise) throws Exception { - final String hostname = IDN.toASCII(unresolvedAddress.getHostString()); + final String hostname = IDN.toASCII(hostname(unresolvedAddress)); final int port = unresolvedAddress.getPort(); final DnsNameResolverContext ctx = new DnsNameResolverContext(this, hostname, port, promise); @@ -650,6 +650,15 @@ public class DnsNameResolver extends SimpleNameResolver { ctx.resolve(); } + private static String hostname(InetSocketAddress addr) { + // InetSocketAddress.getHostString() is available since Java 7. + if (PlatformDependent.javaVersion() < 7) { + return addr.getHostName(); + } else { + return addr.getHostString(); + } + } + /** * Sends a DNS query with the specified question. */