Java InetAddress newInterfaceAddress(InetAddress inetAddress, short maskLength)

Here you can find the source of newInterfaceAddress(InetAddress inetAddress, short maskLength)

Description

new Interface Address

License

Apache License

Declaration

private static InterfaceAddress newInterfaceAddress(InetAddress inetAddress, short maskLength) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.net.InterfaceAddress;

public class Main {
    private static InterfaceAddress newInterfaceAddress(InetAddress inetAddress, short maskLength) {
        try {//w w  w.ja  v  a2 s .c o  m
            final Constructor<InterfaceAddress> ctor = InterfaceAddress.class.getDeclaredConstructor();
            ctor.setAccessible(true);
            final Field addressField = InterfaceAddress.class.getDeclaredField("address");
            addressField.setAccessible(true);
            final Field maskLengthField = InterfaceAddress.class.getDeclaredField("maskLength");
            maskLengthField.setAccessible(true);

            final InterfaceAddress interfaceAddress = ctor.newInstance();
            addressField.set(interfaceAddress, inetAddress);
            maskLengthField.set(interfaceAddress, maskLength);

            return interfaceAddress;
        } catch (final Exception e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. inetAddressesComparator(final boolean sameHost)
  2. inetAddressesCompare(final InetAddress[] addresses1, final InetAddress[] addresses2)
  3. inetAddressFromThriftString(String ipAddress)
  4. inetAddressGt(InetAddress a, InetAddress b)
  5. inetAddrToByteArray(InetAddress a)
  6. normalizeHostAddress(final InetAddress localHost)
  7. parseInetAddressFromString(String input)
  8. readInetAddress(final DataInput in, final boolean fixedLength)
  9. readInetAddress(final String stringInetAddress)