Java Code Examples for java.net.Inet4Address#getByAddress()

The following examples show how to use java.net.Inet4Address#getByAddress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IPReachabilityInformationPrefixDescriptorSubTLV.java    From netphony-network-protocols with Apache License 2.0 6 votes vote down vote up
public void decode(){
	int offset = 4;
	address=new byte[4];
	prefix_length = this.tlv_bytes[offset]&(0xFF);
	offset+=1;
		for(int i = 0;i<address.length;i++){
			address[i]=0;
		}
		System.arraycopy(this.tlv_bytes,offset, address, 0, prefix_length);
	try {
			ipv4Address= (Inet4Address) Inet4Address.getByAddress(address);
	} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
 
Example 2
Source File: DomainIDTLV.java    From netphony-network-protocols with Apache License 2.0 6 votes vote down vote up
public void decode() throws MalformedPCEPObjectException{
	if (this.TLVValueLength!=8){
		throw new MalformedPCEPObjectException("Bad DomainIDTLV lenght: "+this.TLVValueLength);
	}
	 
	int offset=4;
	domainType=((this.tlv_bytes[offset]<<8)& 0xFF00) |  (this.tlv_bytes[offset+1] & 0xFF);
	offset = 8;
	byte[] ip=new byte[4];
	System.arraycopy(this.tlv_bytes,offset, ip, 0, 4);
	try {		
		domainId=(Inet4Address)Inet4Address.getByAddress(ip);
	} catch (UnknownHostException e) {			
		e.printStackTrace();
		throw new MalformedPCEPObjectException("Bad DomainIDTLV address");
	}
	

	
}
 
Example 3
Source File: ResourceIDSubTLV.java    From netphony-network-protocols with Apache License 2.0 5 votes vote down vote up
public void decode() {
	byte[] resourceID=new byte[4];
	System.arraycopy(this.subtlv_bytes, 4, resourceID, 0, 4);
	try {
		ResourceID= (Inet4Address)Inet4Address.getByAddress(resourceID);
	} catch (UnknownHostException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
 
Example 4
Source File: SourceAddressChannelHandlerTest.java    From zuul with Apache License 2.0 5 votes vote down vote up
@Test
public void ipv4AddressString() throws Exception {
    InetAddress address = Inet4Address.getByAddress("localhost", new byte[] {127, 0, 0, 1});

    String addressString = SourceAddressChannelHandler.getHostAddress(new InetSocketAddress(address, 8080));

    assertEquals("127.0.0.1", addressString);
}
 
Example 5
Source File: TestInetAddressServlet.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
/**
 * Get instances of InetAddress by calling InetAddress.getByAddress(byte[]),
 * Inet4Address.getByAddress(byte[]),  and Inet6Address.getByAddress(byte[]),
 * passing in a 4-byte address,
 * and then test their properties.
 * @param response HttpServletResponse used to return failure message
 * @throws IOException If method being tested throws this.
 * @throws AssertionFailedException If an assertion fails
 */
private static void testGetByAddress4(HttpServletResponse response)
    throws IOException, AssertionFailedException {
  byte[] addressBytes = new byte[] {74, 125, (byte) 224, 19};
  String addressString = "74.125.224.19";
  String name = null;
  InetAddress googleDotCom = InetAddress.getByAddress(addressBytes);
  testNameAndAddress4(name, addressString, addressBytes, googleDotCom, response);
  googleDotCom = Inet4Address.getByAddress(addressBytes);
  testNameAndAddress4(name, addressString, addressBytes, googleDotCom, response);
  googleDotCom = Inet6Address.getByAddress(addressBytes);
  testNameAndAddress4(name, addressString, addressBytes, googleDotCom, response);
}
 
Example 6
Source File: CommonMethods.java    From SmartProxy with GNU General Public License v3.0 5 votes vote down vote up
public static InetAddress ipIntToInet4Address(int ip){
	byte[] ipAddress=new byte[4];
	writeInt(ipAddress, 0, ip);
       try {
		return  Inet4Address.getByAddress(ipAddress);
	} catch (UnknownHostException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		return null;
	}
}
 
Example 7
Source File: IPv4AddressRROSubobject.java    From netphony-network-protocols with Apache License 2.0 5 votes vote down vote up
public void decode(){
	byte[] ipadd=new byte[4]; 
	System.arraycopy(this.subobject_bytes,2, ipadd, 0, 4);
	try {
		ipv4address=(Inet4Address)Inet4Address.getByAddress(ipadd);
	} catch (UnknownHostException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}		
	prefix=(int)this.subobject_bytes[6];
}
 
Example 8
Source File: ProxyUtils.java    From SmartZPN with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static InetAddress ipIntToInet4Address(int ip) {
    byte[] ipAddress = new byte[4];
    writeInt(ipAddress, 0, ip);
    try {
        return Inet4Address.getByAddress(ipAddress);
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
}
 
Example 9
Source File: BGPLSIdentifierNodeDescriptorSubTLV.java    From netphony-network-protocols with Apache License 2.0 5 votes vote down vote up
private void decode() {
	//Decoding BGPLS_ID_TLV	
	byte[] ip=new byte[4]; 
	System.arraycopy(this.subtlv_bytes,4, ip, 0, 4);
	try {
		BGPLS_ID=(Inet4Address)Inet4Address.getByAddress(ip);
	} catch (UnknownHostException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} 	
}
 
Example 10
Source File: CommonMethods.java    From BaoLianDeng with GNU General Public License v3.0 5 votes vote down vote up
public static InetAddress ipIntToInet4Address(int ip) {
    byte[] ipAddress = new byte[4];
    writeInt(ipAddress, 0, ip);
    try {
        return Inet4Address.getByAddress(ipAddress);
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
}
 
Example 11
Source File: IPUtil.java    From vjtools with Apache License 2.0 5 votes vote down vote up
/**
 * 从IPv4String转换为InetAddress.
 * 
 * IpString如果确定ipv4, 使用本方法减少字符分析消耗 .
 * 
 * 先字符串传换为byte[]再调getByAddress(byte[]),避免了调用getByName(ip)可能引起的DNS访问.
 */
public static Inet4Address fromIpv4String(String address) {
	byte[] bytes = ip4StringToBytes(address);
	if (bytes == null) {
		return null;
	} else {
		try {
			return (Inet4Address) Inet4Address.getByAddress(bytes);
		} catch (UnknownHostException e) {
			throw new AssertionError(e);
		}
	}
}
 
Example 12
Source File: RSVPHopIPv4.java    From netphony-network-protocols with Apache License 2.0 5 votes vote down vote up
/**
 * 
 */

@Override
public void decode(byte[] bytes, int offset) {

	byte[] receivedAddress = new byte[4];
	System.arraycopy(bytes,offset+4,receivedAddress,0,4);
	try{
		next_previousHopAddress = (Inet4Address) Inet4Address.getByAddress(receivedAddress);
	}catch(UnknownHostException e){
		// FIXME: Poner logs con respecto a excepcion
	}
	logicalInterfaceHandle = (double)(bytes[offset+8]|bytes[offset+9]|bytes[offset+10]|bytes[offset+11]);
		
}
 
Example 13
Source File: ResvConfirmIPv4.java    From netphony-network-protocols with Apache License 2.0 5 votes vote down vote up
@Override
public void decode(byte[] bytes, int offset) {
	
	byte[] receivedAddress = new byte[4];
	System.arraycopy(bytes,offset+4,receivedAddress,0,4);
	try{
		receiverAddress = (Inet4Address) Inet4Address.getByAddress(receivedAddress);
	}catch(UnknownHostException e){
		// FIXME: Poner logs con respecto a excepcion
	}
	
}
 
Example 14
Source File: RemoteASNumber.java    From netphony-network-protocols with Apache License 2.0 5 votes vote down vote up
public void decode()throws MalformedOSPFSubTLVException{
	if (this.getTLVValueLength()!=4){
		throw new MalformedOSPFSubTLVException();
	}
		
	byte[] ip=new byte[4];
	System.arraycopy(this.tlv_bytes,4, ip, 0, 4);
	try {
		this.remoteASNumber=(Inet4Address)Inet4Address.getByAddress(ip);
	} catch (UnknownHostException e) {
		e.printStackTrace();
		throw new MalformedOSPFSubTLVException();
	}	
}
 
Example 15
Source File: GetDestinationIPAddressTest.java    From xbee-java with Mozilla Public License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
	DESTINATION_ADDRESS = (Inet4Address) Inet4Address.getByAddress(RESPONSE);

	// Instantiate a local Wi-Fi device object.
	xbeeDevice = PowerMockito.spy(new WiFiDevice(Mockito.mock(SerialPortRxTx.class)));
}
 
Example 16
Source File: IPv4InterfaceAddressLinkDescriptorsSubTLV.java    From netphony-network-protocols with Apache License 2.0 5 votes vote down vote up
public void decode(){
	if (this.getTLVValueLength()!=4){
		//throw new MalformedPCEPObjectException();
		//FIXME: esta mal formado Que hacer
	}
	byte[] ip=new byte[4]; 
	System.arraycopy(this.tlv_bytes,4, ip, 0, 4);
	try {
		ipv4Address=(Inet4Address)Inet4Address.getByAddress(ip);
	} catch (UnknownHostException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
 
Example 17
Source File: IPDeviceTest.java    From xbee-java with Mozilla Public License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
	// Suppress the 'readDeviceInfo' method of the parent class so that it is not
	// called from the child (IPDevice) class.
	PowerMockito.suppress(PowerMockito.method(AbstractXBeeDevice.class, "readDeviceInfo"));
	
	// Spy the IPDevice class.
	SerialPortRxTx mockPort = Mockito.mock(SerialPortRxTx.class);
	ipDevice = PowerMockito.spy(new IPDevice(mockPort));
	
	// Mock an IP address object.
	ipAddress = (Inet4Address) Inet4Address.getByAddress(RESPONSE_MY);
}
 
Example 18
Source File: TestInetAddressServlet.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
/**
 * Get instances of InetAddress by calling InetAddress.getByAddress(String, byte[]),
 * Ine4tAddress.getByAddress(String, byte[]),  and Inet6Address.getByAddress(String, byte[]),
 * passing in a 4-byte address,
 * and then test their properties.
 * @param response HttpServletResponse used to return failure message
 * @throws IOException If method being tested throws this.
 * @throws AssertionFailedException If an assertion fails
 */
private static void testGetByNameAndAddress4(HttpServletResponse response)
    throws IOException, AssertionFailedException {
  byte[] addressBytes = new byte[] {74, 125, (byte) 224, 19};
  String addressString = "74.125.224.19";
  String name = "www.google.com";
  InetAddress googleDotCom = InetAddress.getByAddress(name, addressBytes);
  testNameAndAddress4(name, addressString, addressBytes, googleDotCom, response);
  googleDotCom = Inet4Address.getByAddress(name, addressBytes);
  testNameAndAddress4(name, addressString, addressBytes, googleDotCom, response);
  googleDotCom = Inet6Address.getByAddress(name, addressBytes);
  testNameAndAddress4(name, addressString, addressBytes, googleDotCom, response);
}
 
Example 19
Source File: Inet4Network.java    From james-project with Apache License 2.0 3 votes vote down vote up
/**
 * Return InetAddress which represent the given byte[]
 * 
 * @param ip
 *            the byte[] represent the ip
 * @return ip the InetAddress generated of the given byte[]
 * @throws java.net.UnknownHostException
 */
private static InetAddress getByAddress(byte[] ip) throws UnknownHostException {

    InetAddress addr;

    addr = Inet4Address.getByAddress(ip);

    if (addr == null) {
        addr = InetAddress.getByName(Integer.toString(ip[0] & 0xFF, 10) + "." + Integer.toString(ip[1] & 0xFF, 10) + "." + Integer.toString(ip[2] & 0xFF, 10) + "." + Integer.toString(ip[3] & 0xFF, 10));
    }

    return addr;
}
 
Example 20
Source File: IPv4Address.java    From galimatias with MIT License 2 votes vote down vote up
/**
 * Convert to @{java.net.InetAddress}.
 *
 * @return The IPv4 address as a @{java.net.InetAddress}.
 */
public Inet4Address toInetAddress() throws UnknownHostException {
    return (Inet4Address) Inet4Address.getByAddress(getBytes());
}