Java Utililty Methods InetAddress Create

List of utility methods to do InetAddress Create

Description

The list of methods to do InetAddress Create are organized into topic(s).

Method

InetAddressgetSafeInetAddress(String name, JSONObject json)
get Safe Inet Address
String value = getSafeString(name, json);
if (value == null)
    return null;
try {
    return InetAddress.getByName(value);
} catch (Exception e) {
    return null;
InetAddressgetSourceAddressForDestination(InetAddress destination)
get Source Address For Destination
throw new UnsupportedOperationException("Not implemented yet.");
IterablegetSpecificAddresses(@Nonnull InetAddress in)
get Specific Addresses
if (!in.isAnyLocalAddress())
    return Collections.singleton(in);
List<InetAddress> out = new ArrayList<InetAddress>();
for (NetworkInterface iface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
    if (iface.isLoopback())
        continue;
    if (!iface.isUp())
        continue;
...
InetAddressgetSubnetPrefix(InetAddress ip, int maskLen)
Given an IP address and a prefix network mask length, it returns the equivalent subnet prefix IP address Example: for ip = "172.28.30.254" and maskLen = 25 it will return "172.28.30.128"
int bytes = maskLen / 8;
int bits = maskLen % 8;
byte modifiedByte;
byte[] sn = ip.getAddress();
if (bits > 0) {
    modifiedByte = (byte) (sn[bytes] >> (8 - bits));
    sn[bytes] = (byte) (modifiedByte << (8 - bits));
    bytes++;
...
StringgetURI(String protocol, InetAddress host, int port)
get URI
String hostAddr = host.getHostAddress();
if (host instanceof Inet6Address) {
    if (hostAddr.lastIndexOf('%') >= 0) {
        hostAddr = hostAddr.substring(0, hostAddr.lastIndexOf('%'));
    hostAddr = "[" + hostAddr + "]";
return protocol + "://" + hostAddr + ":" + port;
...