Here you can find the source of isLoopbackIp(InetAddress addr)
Parameter | Description |
---|---|
addr | target address |
public static boolean isLoopbackIp(InetAddress addr)
//package com.java2s; /*// www . j av a 2s . c o m License: blueprint-sdk is licensed under the terms of Eclipse Public License(EPL) v1.0 (http://www.eclipse.org/legal/epl-v10.html) Distribution: Repository - https://github.com/lempel/blueprint-sdk.git Blog - http://lempel.egloos.com */ import java.net.*; public class Main { /** * See if given address is loopback or not * * @param addr target address * @return true : private network */ public static boolean isLoopbackIp(InetAddress addr) { boolean result = false; if (addr instanceof Inet4Address) { result = isLoopbackIp((Inet4Address) addr); } else if (addr instanceof Inet6Address) { result = isLoopbackIp((Inet6Address) addr); } return result; } /** * See if given address is loopback or not * * @param addr IPv4 address * @return true : private network */ public static boolean isLoopbackIp(Inet4Address addr) { boolean result = false; byte[] address = addr.getAddress(); if ((address[0] & 0x000000ff) == 0x7f) { // 127.*.*.* result = true; } return result; } /** * See if given address is loopback or not * * @param addr IPv6 address * @return true : private network */ public static boolean isLoopbackIp(Inet6Address addr) { boolean result = false; byte[] address = addr.getAddress(); boolean allZero = true; for (byte abyte : address) { if (abyte != 0) { allZero = false; break; } } if (allZero && address[address.length - 1] == 1) { result = true; } return result; } }