-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IPv4-mapped IPv6 addresses cause 'ClassCastException' #73
Comments
Yes, it seems InetAddress.getByAddress returns an Inet4Address for IPv4-mapped IPv6 addresses. So this is a bug in IPAddress which will be fixed in all future releases. In the meantime, here are a couple of work-arounds. They both do the same thing. The first is a little more explicit about what is going on, and the second is shorter. public static void main(String[] args) throws HostNameException, UnknownHostException {
IPAddress address = new HostName("[::FFFF:192.168.8.165]").toAddress();
try {
error(address);
} catch(ClassCastException e) {
System.out.println("oops! " + e);
}
System.out.println(workaroundOne(address));
System.out.println(workaroundTwo(address));
}
public static void error(IPAddress address) throws HostNameException, UnknownHostException {
address.toInetAddress();
} Option 1 public static InetAddress workaroundOne(IPAddress address) {
if(address.isIPv6()) {
IPv6Address addr = address.toIPv6();
if(addr.isIPv4Mapped()) {
address = addr.toIPv4();
}
}
return address.toInetAddress();
} Option 2 public static InetAddress workaroundTwo(IPAddress address) {
if(address.isIPv4Convertible()) {
address = address.toIPv4();
}
return address.toInetAddress();
} Output:
|
Thank you for quick response, I've find another workaround.
this works fine |
This has been fixed in the newly released version 5.3.4 |
Hi,
I'm having a problems with IPv4-mapped IPv6 addresses. Code below throws ClassCastException.
`java.lang.ClassCastException: class java.net.Inet4Address cannot be cast to class java.net.Inet6Address (java.net.Inet4Address and java.net.Inet6Address are in module java.base of loader 'bootstrap')
`
The text was updated successfully, but these errors were encountered: