Java Local Address Check isLocalAddress(String address)

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

Description

is Local Address

License

Apache License

Declaration

public static boolean isLocalAddress(String address) throws IOException 

Method Source Code

//package com.java2s;
/**/*  www. j  a v  a 2s .c  o  m*/
*      Copyright (C) 2008 10gen Inc.
*  
*    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.io.*;
import java.net.*;
import java.util.*;

public class Main {
    public static boolean isLocalAddress(String address) throws IOException {
        if (address == null)
            return false;
        return isLocalAddress(getByName(address));
    }

    public static boolean isLocalAddress(InetAddress ia) throws IOException {

        for (NetworkInterface nic : getNetworkInterfaces()) {
            Enumeration<InetAddress> e = nic.getInetAddresses();
            while (e.hasMoreElements()) {
                InetAddress mine = e.nextElement();
                if (mine.getHostAddress().equals(ia.getHostAddress()))
                    return true;
            }
        }

        return false;
    }

    public static InetAddress getByName(String host) throws UnknownHostException {

        if (host == null)
            throw new NullPointerException("can't lookup null host");

        return InetAddress.getByName(host);
    }

    public static List<NetworkInterface> getNetworkInterfaces() throws java.io.IOException {
        List<NetworkInterface> lst = new ArrayList<NetworkInterface>();

        Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
        while (e.hasMoreElements())
            lst.add(e.nextElement());

        return lst;
    }
}

Related

  1. isLocal(String brokerAddress)
  2. isLocalAddress(final String addr)
  3. isLocalAddress(final String address)
  4. isLocalAddress(String address)
  5. isLocalAddress(String address)
  6. isLocalAddress(String host)
  7. isLocalOrLoopback(String address)
  8. isLoopbackAddress(String address)