Java Utililty Methods InetAddress

List of utility methods to do InetAddress

Description

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

Method

booleaninetAddressGt(InetAddress a, InetAddress b)
inet Address Gt
return !inetAddressLeq(a, b);
byte[]inetAddrToByteArray(InetAddress a)
inet Addr To Byte Array
return a.getAddress();
InterfaceAddressnewInterfaceAddress(InetAddress inetAddress, short maskLength)
new Interface Address
try {
    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();
...
StringnormalizeHostAddress(final InetAddress localHost)
normalize Host Address
if (localHost instanceof Inet6Address) {
    return "[" + localHost.getHostAddress() + "]";
} else {
    return localHost.getHostAddress();
InetAddressparseInetAddressFromString(String input)
parse Inet Address From String
try {
    InetAddress address = InetAddress.getByName(input);
    return address;
} catch (Exception ex) {
    return null;
InetAddressreadInetAddress(final DataInput in, final boolean fixedLength)
read Inet Address
if (in.readBoolean()) {
    if (fixedLength) {
        in.readByte();
        in.readLong();
        in.readLong();
    return null;
final byte addrLength = in.readByte();
switch (addrLength) {
case 4:
    final int address = in.readInt();
    if (fixedLength) {
        in.readInt();
        in.readLong();
    final byte[] addr = new byte[4];
    addr[0] = (byte) (address >>> 24 & 0xFF);
    addr[1] = (byte) (address >>> 16 & 0xFF);
    addr[2] = (byte) (address >>> 8 & 0xFF);
    addr[3] = (byte) (address & 0xFF);
    return InetAddress.getByAddress(addr);
case 16:
    final byte[] result = new byte[16];
    in.readFully(result);
    return InetAddress.getByAddress(result);
default:
    throw new IOException("Unknown address format, length: " + addrLength);
InetAddressreadInetAddress(final String stringInetAddress)
read Inet Address
try {
    return InetAddress.getByName(stringInetAddress);
} catch (final UnknownHostException e) {
    throw new IllegalArgumentException(e);
InetAddress[]remove(InetAddress[] a_, InetAddress b)
remove
InetAddress[] a = (InetAddress[]) a_.clone();
for (int i = 0; i < a.length; i++)
    if (b.equals(a[i])) {
        a[i] = null;
        break;
return removeNull(a);
InetAddress[]removeNull(InetAddress addrs[])
remove Null
int count = 0;
for (int i = 0; i < addrs.length; i++)
    if (addrs[i] != null)
        count++;
InetAddress[] result = new InetAddress[count];
count = 0;
for (int i = 0; i < addrs.length; i++)
    if (addrs[i] != null)
...
voidserialize(InetAddress endpoint, DataOutput out)
serialize
byte[] buf = endpoint.getAddress();
out.writeByte(buf.length);
out.write(buf);