Java InetAddress Check isPrivateNetworkAddress(final InetAddress inetAddress)

Here you can find the source of isPrivateNetworkAddress(final InetAddress inetAddress)

Description

Returns whether the given internet address is reserved for private networks, such as those behind a NAT router.

License

Open Source License

Parameter

Parameter Description
inetAddress the given internet address

Return

whether the given internet address is reserved for private networks

Declaration

public static boolean isPrivateNetworkAddress(final InetAddress inetAddress) 

Method Source Code

//package com.java2s;
/*//from w ww  . jav  a 2  s.c  om
 * NetworkUtils.java
 *
 * Created on Jan 12, 2010, 9:17:04 AM
 *
 * Description: NetworkUtils utilities.
 *
 * Copyright (C) Jan 12, 2010 reed.
 *
 * This program is free software; you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

import java.net.InetAddress;

public class Main {
    /** Returns whether the given internet address is reserved for private networks, such as those behind a NAT router.
     *
     * @param inetAddress the given internet address
     * @return whether the given internet address is reserved for private networks
     */
    public static boolean isPrivateNetworkAddress(final InetAddress inetAddress) {
        //Preconditions
        assert inetAddress != null : "inetAddress must not be null";

        final String inetAddressString = inetAddress.getHostAddress();
        return inetAddressString.startsWith("192.168.") || inetAddressString.startsWith("10.")
                || inetAddressString.startsWith("172.");
    }
}

Related

  1. isPrivate(InetAddress addr)
  2. isPrivate(InetAddress adr)
  3. isPrivateIp(final InetAddress address)
  4. isPrivateIp(InetAddress addr)
  5. isPrivateIP(InetAddress ip)
  6. isPrivateSubnet(InetAddress address)
  7. isPublic(InetAddress inetAddress)
  8. isPublicIp(InetAddress addr)
  9. isPublicIP4Address(InetAddress localAddress)