Java Local Address Check isLocalOrLoopback(String address)

Here you can find the source of isLocalOrLoopback(String address)

Description

This will check if a url or address is a loopback or local address

License

Open Source License

Parameter

Parameter Description
address the address you which to check

Return

true if it is a loopback or local address

Declaration

public static boolean isLocalOrLoopback(String address) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;

public class Main {
    /**// ww  w.  j  av  a  2s. c  om
     * This will check if a url or address is a loopback or local address
     * @param address the address you which to check
     * @return true if it is a loopback or local address
     */
    public static boolean isLocalOrLoopback(String address) {
        try {
            try {
                URL url = new URL(address);
                address = url.getHost();
            } catch (MalformedURLException ignored) {
            }
            InetAddress inetAddress = InetAddress.getByName(address);
            return inetAddress.isAnyLocalAddress() || inetAddress.isLinkLocalAddress()
                    || inetAddress.isLoopbackAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return false;
    }
}

Related

  1. isLocalAddress(final String address)
  2. isLocalAddress(String address)
  3. isLocalAddress(String address)
  4. isLocalAddress(String address)
  5. isLocalAddress(String host)
  6. isLoopbackAddress(String address)
  7. isMyAddress(String host)