下面列出了java.net.Inet4Address#getAddress ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public String getRoute_address() {
try {
int prefix = Integer.parseInt(this.prefix_length);
if (prefix >= 0 && prefix <= 32) {
Inet4Address address = (Inet4Address) InetAddress.getByName(this.route_address);
byte[] bytes = address.getAddress();
int mask = 0xffffffff << (32 - prefix);
bytes[0] = (byte)(bytes[0] & (mask >> 24));
bytes[1] = (byte)(bytes[1] & (mask >> 16));
bytes[2] = (byte)(bytes[2] & (mask >> 8));
bytes[3] = (byte)(bytes[3] & (mask));
InetAddress netAddr = InetAddress.getByAddress(bytes);
this.route_address = netAddr.getHostAddress();
}
}
catch (Exception e) {}
return this.route_address;
}
/**
* Method to determine if the address 'nodeAdrress' belongs to the aggregated IP address range given by the mask and the addresss 'aggregatedIPRange'
* @param nodeAddress The address to query
* @param mask The 32 bit network mask
* @param aggregatedIPRange The aggregated IP address
* @return true if it belongs to the range, false if it does not belong to the range
*/
public boolean belongsToDomain(Inet4Address nodeAddress, byte []mask, Inet4Address aggregatedIPRange){
int i;
//byte [] networkAddress=new byte[4];
byte [] bytesAggregatedIPRange=aggregatedIPRange.getAddress();
byte [] bytesNodeAddress=nodeAddress.getAddress();
boolean found=true;
for (i=0;i<4;++i){
//networkAddress[i]= (byte) ((bytesNodeAddress[i]&mask[i])&0xFF) ;
//log.info("network Node Address[i]: "+(networkAddress[i]&0xFF));
//log.info("bytesAggregatedIPRange["+i+"]: "+((bytesAggregatedIPRange[i]&mask[i])&0xFF));
//log.info("bytesNodeAddress["+i+"]: "+((bytesNodeAddress[i]&mask[i])&0xFF));
if ((byte)((bytesAggregatedIPRange[i]&mask[i])&0xFF)!=(byte)((bytesNodeAddress[i]&mask[i])&0xFF)){
found=false;
return found;
}
}
return found;
}
@Override
public int compare(final Inet4Address aa, final Inet4Address ba) {
final byte[] a = aa.getAddress();
final byte[] b = ba.getAddress();
// local subnet comes after all else.
if (a[0] == 127 && b[0] != 127) {
return 1;
}
if (a[0] != 127 && b[0] == 127) {
return -1;
}
for (int i = 0; i < 4; i++) {
if (a[i] < b[i]) {
return -1;
}
if (a[i] > b[i]) {
return 1;
}
}
return 0;
}
/**
* Convert a IPv4 address from an InetAddress to an integer
* @param inetAddr is an InetAddress corresponding to the IPv4 address
* @return the IP address as an integer in network byte order
*/
public static int inetAddressToInt(Inet4Address inetAddr)
throws IllegalArgumentException {
byte [] addr = inetAddr.getAddress();
return ((addr[3] & 0xff) << 24) | ((addr[2] & 0xff) << 16) |
((addr[1] & 0xff) << 8) | (addr[0] & 0xff);
}
/**
* Returns the implicit netmask of an IPv4 address, as was the custom before 1993.
*/
public static int getImplicitNetmask(Inet4Address address) {
int firstByte = address.getAddress()[0] & 0xff; // Convert to an unsigned value.
if (firstByte < 128) {
return 8;
} else if (firstByte < 192) {
return 16;
} else if (firstByte < 224) {
return 24;
} else {
return 32; // Will likely not end well for other reasons.
}
}
private static Inet6Address toCompactAddress(Inet4Address a4) {
byte[] b4 = a4.getAddress();
byte[] b6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, b4[0], b4[1], b4[2], b4[3] };
try {
return (Inet6Address) InetAddress.getByAddress(b6);
} catch (UnknownHostException e) {
throw new Error(e);
}
}
@Override
protected void internalHandle(DataOutputStream buffer, final Inet4Address value) throws Exception {
buffer.writeInt(8);
buffer.writeByte(IPv4);
buffer.writeByte(MASK);
buffer.writeByte(IS_CIDR);
byte[] inet4AddressBytes = value.getAddress();
buffer.writeByte(inet4AddressBytes.length);
buffer.write(inet4AddressBytes);
}
@Override
public int compare(final Inet4Address ipOne, final Inet4Address ipTwo) {
final byte[] bytes1 = ipOne.getAddress();
final byte[] bytes2 = ipTwo.getAddress();
for (int i = 0; i < 4; i++) {
final int a = bytes1[i] & 0xff;
final int b = bytes2[i] & 0xff;
if (a != b) {
return a - b;
}
}
return 0;
}
/**
* Test properties of an instance of Inet4Address obtained via getLocalHost(). This method
* is identical to testGetLocalHost(InetAddress, HttpServletResponse), except that the
* parameter is of type Inet4Address instead of InetAddress. This will test a different
* code-path through our byte-rewriting.
* @param localHost The instance of InetAddress being tested
* @param response HttpServletResponse used to return failure message
* @throws IOException If method being tested throws this.
* @throws AssertionFailedException
*/
private static void testLocalHost4(Inet4Address localHost, HttpServletResponse response)
throws IOException, AssertionFailedException {
assertNotNull("localhost", localHost, response);
assertTrue("instanceof Inet4Addr", localHost instanceof Inet4Address, response);
//getAddress
byte[] bytes = localHost.getAddress();
assertNotNull("localhost address-bytes", bytes, response);
assertEquals("localhost address bytes.length", 4, bytes.length, response);
assertEquals("first byte of localhost address", 127, bytes[0], response);
assertEquals("last byte of localhost address", 1, bytes[3], response);
String name = localHost.getCanonicalHostName();
assertEquals("getCanonicalHostName", "localhost", name, response);
//getHostAddress should return the loopback address
String address = localHost.getHostAddress();
assertEquals("getHostAddress", "127.0.0.1", address, response);
//getHostName
name = localHost.getHostName();
assertEquals("getHostName", "localhost", name, response);
//Misc Properties
assertFalse("isAnyLocalAddress", localHost.isAnyLocalAddress(), response);
assertFalse("isLinkLocalAddress", localHost.isLinkLocalAddress(), response);
assertTrue("isLoopbackAddress", localHost.isLoopbackAddress(), response);
assertFalse("isLoopbackAddress", localHost.isMCGlobal(), response);
assertFalse("isMCLinkLoca", localHost.isMCLinkLocal(), response);
assertFalse("isMCNodeLocal", localHost.isMCNodeLocal(), response);
assertFalse("isMCOrgLocal", localHost.isMCOrgLocal(), response);
assertFalse("isMCSiteLoca", localHost.isMCSiteLocal(), response);
assertFalse("isMulticastAddress", localHost.isMulticastAddress(), response);
assertFalse("isSiteLocalAddress", localHost.isSiteLocalAddress(), response);
//toString
String s = localHost.toString();
assertEquals("toString", "localhost/127.0.0.1", s, response);
//isReachable
assertFalse("isReachable", localHost.isReachable(1000), response);
//Can't test version of isReachable() that takes a NetworkInterface
//because it is not possible ot get a network interface without trigger ptrace sandbox
//localHost.isReachable(netif, ttl, timeout);
}
/**
* Wrapper function for converting INetAddrs into hashed IPs. <br>
* Formatting is handled in this method to simplify keeping IP data the same everywhere. <br>
* To modify the way IPs are saved, simply modify this method.
*
* @param ip The IP to convert.
*
* @return
*/
private int formatIP(Inet4Address ip) {
int hash = 0;
int len = ip.getAddress().length;
int i = len > 4 ? len - 4 : 0;
for (; i < len; i++)
hash = (hash << 8) | (ip.getAddress()[i] & 0xff);
return hash;
}
/**
* Constructs an IPv4 address.
*
* @param inet4Address the java.net address object
*/
public IPv4Address(Inet4Address inet4Address, Integer networkPrefixLength) {
this(inet4Address.getAddress(), networkPrefixLength);
}
/**
* Constructs an IPv4 address.
*
* @param inet4Address the java.net address object
*/
public IPv4Address(Inet4Address inet4Address) {
this(inet4Address.getAddress());
}
/**
* Convert from @{java.net.Inet4Address}.
*
* @param inet4Address The IPv4 address as a @{java.net.Inet4Address}.
* @return The IPv4 address as a @{IPv4Address}.
*/
public static IPv4Address fromInet4Adress(final Inet4Address inet4Address) {
return new IPv4Address(inet4Address.getAddress());
}
/**
* Convert from @{java.net.Inet4Address}.
*
* @param inet4Address The IPv4 address as a @{java.net.Inet4Address}.
* @return The IPv4 address as a @{IPv4Address}.
*/
public static IPv4Address fromInet4Adress(final Inet4Address inet4Address) {
return new IPv4Address(inet4Address.getAddress());
}