java.net.Inet4Address#getAddress ( )源码实例Demo

下面列出了java.net.Inet4Address#getAddress ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: ki4a   文件: RouteInfo.java
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;
}
 
源代码2 项目: netphony-topology   文件: ReachabilityManager.java
/**
 * 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;		
}
 
源代码3 项目: reef   文件: Utils.java
@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;
}
 
源代码4 项目: android_9.0.0_r45   文件: NetworkUtils.java
/**
 * 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);
}
 
源代码5 项目: android_9.0.0_r45   文件: NetworkUtils.java
/**
 * 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.
    }
}
 
源代码6 项目: netty-4.1.22   文件: DnsQueryContextManager.java
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);
    }
}
 
源代码7 项目: PgBulkInsert   文件: Inet4AddressValueHandler.java
@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);
}
 
源代码8 项目: RipplePower   文件: IP46Utils.java
@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);
}
 
源代码10 项目: MakeLobbiesGreatAgain   文件: IOPeer.java
/**
 * 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;
}
 
源代码11 项目: IPAddress   文件: IPv4Address.java
/**
 * Constructs an IPv4 address.
 *
 * @param inet4Address the java.net address object
 */
public IPv4Address(Inet4Address inet4Address, Integer networkPrefixLength) {
	this(inet4Address.getAddress(), networkPrefixLength);
}
 
源代码12 项目: IPAddress   文件: IPv4Address.java
/**
 * Constructs an IPv4 address.
 *
 * @param inet4Address the java.net address object
 */
public IPv4Address(Inet4Address inet4Address) {
	this(inet4Address.getAddress());
}
 
源代码13 项目: galimatias   文件: IPv4Address.java
/**
 * 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());
}
 
源代码14 项目: galimatias   文件: IPv4Address.java
/**
 * 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());
}