下面列出了java.net.Inet4Address#getByAddress ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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");
}
}
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();
}
}
/**
* 从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);
}
}
}
/**
* 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);
}
@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);
}
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();
}
}
@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)));
}
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();
}
}
@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
}
}
/**
*
*/
@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]);
}
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();
}
}
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;
}
}
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();
}
}
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;
}
}
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];
}
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;
}
}
/**
* 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);
}
@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);
}
/**
* 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;
}
/**
* Convert to @{java.net.InetAddress}.
*
* @return The IPv4 address as a @{java.net.InetAddress}.
*/
public Inet4Address toInetAddress() throws UnknownHostException {
return (Inet4Address) Inet4Address.getByAddress(getBytes());
}