Java InetAddress Check isLoopbackAddress(InetAddress address)

Here you can find the source of isLoopbackAddress(InetAddress address)

Description

is Loopback Address

License

Apache License

Declaration

public static boolean isLoopbackAddress(InetAddress address) 

Method Source Code


//package com.java2s;
/*/*www  . j ava  2s. c o m*/
 * Copyright 2010-2012 Roger Kapsi
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

import java.net.Inet4Address;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;

public class Main {
    /**
     * @see InetAddress#isLoopbackAddress()
     */
    public static boolean isLoopbackAddress(SocketAddress address) {
        return isLoopbackAddress(getAddress(address));
    }

    /**
     * @see InetAddress#isLoopbackAddress()
     */
    public static boolean isLoopbackAddress(InetAddress address) {
        return isLoopbackAddress(address.getAddress());
    }

    /**
     * @see InetAddress#isLoopbackAddress()
     */
    public static boolean isLoopbackAddress(byte[] address) {
        // 127.x.x.x
        if (isClassicAddress(address)) {
            return (address[0] & 0xFF) == 127;
        }

        // ::01
        if (address[15] != 0x01) {
            return false;
        }

        for (int i = 0; i < 15; i++) {
            if (address[i] != 0x00) {
                return false;
            }
        }

        return true;
    }

    /**
     * An utility method to get the {@link InetAddress} from the
     * {@link SocketAddress}.
     */
    public static InetAddress getAddress(SocketAddress address) {
        return getResolved(address).getAddress();
    }

    /**
     * Returns true if the given {@link SocketAddress} is an IPv4 address
     */
    public static boolean isClassicAddress(SocketAddress address) {
        return isClassicAddress(getAddress(address));
    }

    /**
     * Returns true if the given {@link InetAddress} is an IPv4 address
     */
    public static boolean isClassicAddress(InetAddress address) {
        return address instanceof Inet4Address;
    }

    /**
     * Returns true if the given address is an IPv4 address
     */
    public static boolean isClassicAddress(byte[] address) {
        return address.length == 4;
    }

    /**
     * Resolves (if necessary) the given {@link SocketAddress} and returns it.
     */
    public static InetSocketAddress getResolved(SocketAddress address) {
        InetSocketAddress isa = (InetSocketAddress) address;
        if (isa.isUnresolved()) {
            return new InetSocketAddress(isa.getHostName(), isa.getPort());
        }

        return isa;
    }

    /**
     * Returns {@code true} if the given {@link SocketAddress} is unresolved.
     */
    public static boolean isUnresolved(SocketAddress address) {
        return ((InetSocketAddress) address).isUnresolved();
    }

    /**
     * An utility method to get the {@link InetSocketAddress#getHostName()}.
     */
    public static String getHostName(SocketAddress address) {
        return ((InetSocketAddress) address).getHostName();
    }

    /**
     * Returns the port number.
     */
    public static int getPort(SocketAddress address) {
        return ((InetSocketAddress) address).getPort();
    }
}

Related

  1. isLocalAddress(InetAddress addr)
  2. isLocalAddress(InetAddress address)
  3. isLocalAddress(InetAddress address)
  4. isLocalIP(final InetAddress ip1, final InetAddress ip2, final int mask)
  5. isLocalIpAddress(final InetAddress ipAddress)
  6. isLoopbackIp(InetAddress addr)
  7. isMulticastAddress(InetAddress ipAddr)
  8. isOnNetwork(InetAddress host, InetAddress network, byte[] mask)
  9. isPortFree(int port, InetAddress addr)