guava
guava copied to clipboard
Reverse toString parsing for InetAddresses.
InetAddress.toString() outputs a string in the format <hostname>/<address>. I have use cases where I need to convert this string back to an Inet4Address or Inet6Address, which I think would be useful to include in Guava's InetAddresses class.
The code I'm using currently looks like this:
public static InetAddress forHostnameAddressString(String hostAndAddr) throws UnknownHostException {
checkNotNull(hostAndAddr);
if (hostAndAddr.contains("/")) {
String[] parts = hostAndAddr.split("/", 2);
String host = parts[0];
String addr = parts[1];
InetAddress inetAddress = InetAddresses.forString(addr);
if (inetAddress instanceof Inet4Address) {
return Inet4Address.getByAddress(host, inetAddress.getAddress());
} else if (inetAddress instanceof Inet6Address) {
return Inet6Address.getByAddress(host, inetAddress.getAddress(), ((Inet6Address) inetAddress).getScopeId());
} else {
throw new IllegalStateException("InetAddress must be an instance of Inet4Address or Inet6Address");
}
} else {
return InetAddresses.forString(hostAndAddr);
}
}
Are these cases in which it's not guaranteed that the string was generated from InetAddress.toString()? Because those always have the "/".
Hi netdpb! May I take this issue?
Hi @Shin0017. Thanks for asking! Generally we don't want to accept new APIs until we have some more discussion of the requirements, the use cases, and specifically the API. I haven't heard back from @rhuffy about my question yet. Do you have any thoughts here?
Thanks for your response!
If the string does not contain '/', then this string was not generated from InetAddress.toString(). Otherwise, individually check the legality of each part generated by split() to ensure that this string can be generated by some InetAddress.toString(). If the verification confirms its legitimacy, return the corresponding Inet4Address or Inet6Address.
Thanks.
We still have questions about the usefulness of this feature. How many people need such an API? (We have seen no need for it within Google.) If you're serializing InetAddress. why is the toString() the right serialization format?
Another question for @rhuffy: The code snippet you provided passes the addr part to InetAddresses.forString(String) and uses the parsed scope ID portion—but that method is documented to drop any such scope ID from the string. Is the scope ID part important? If not, why not? If so, I think it would be the first example of an API in InetAddresses that cares about the scope ID.
Hi @netdpb, I'd like to work on this issue, too.
I think we can create a new static method that does the exact opposite of toString(), and just ignore the other cases(let's throw an exception).
How do you think?
Hi @blackdurumi. Until we have a better sense of why such a feature would be actually useful to people, we won't accept it as an API addition. Additionally, there are questions about whether it's even possible (or desirable) to round-trip the scope ID.