I need to determine if an InetSocketAddress is IPv6 or IPv4 efficiently. The only two ways I can see of doing this are either using the instanceof operator, or checking the length of getAddress() (which should return a byte[]). Both of these are less than ideal (instanceof is slow, and getAddress would have to make a copy of the array).
Is there a better alternative?
instanceofmay not be as slow as you think. Personally, unless you've profiled your app and it's reporting instanceof as taking > 5% of your run time, then I would put off optimizing awayinstanceoffor later.