DnsUtil { public static Optional<Integer> getPort(String server) throws InterruptedException, ExecutionException { Objects.requireNonNull(server); DnsQuestion question = new DefaultDnsQuestion(server, DnsRecordType.SRV); DnsResponse response = RESOLVER.query(question).get().content(); DnsRawRecord record = response.recordAt(DnsSection.ANSWER); if (record == null) return Optional.empty(); ByteBuf buffer = record.content(); buffer.readShort(); buffer.readShort(); int port = buffer.readShort(); if (!ValidationUtil.port(port)) return Optional.empty(); return Optional.of(port); } private  DnsUtil(); static Optional<Integer> getPort(String server); static Future<InetAddress> resolve(String server);  }
@Test void testGetPort() throws InterruptedException, ExecutionException { assertTrue(DnsUtil.getPort("invalid123").isEmpty()); assertTrue(DnsUtil.getPort("test.google.com").isEmpty()); assertThrows(ExecutionException.class, () -> DnsUtil.getPort("")); }