mirror of
https://github.com/corda/corda.git
synced 2025-06-13 12:48:18 +00:00
Implement socket API
This commit is contained in:
committed by
Joshua Warner
parent
2800ffe826
commit
45ee25f68c
@ -13,41 +13,61 @@ package java.net;
|
||||
import java.io.IOException;
|
||||
|
||||
public class InetAddress {
|
||||
private final String address;
|
||||
private final String name;
|
||||
private final int ip;
|
||||
|
||||
private InetAddress(String address) {
|
||||
this.address = address;
|
||||
private InetAddress(String name) throws UnknownHostException {
|
||||
this.name = name;
|
||||
this.ip = ipv4AddressForName(name);
|
||||
if (ip == 0) {
|
||||
throw new UnknownHostException(name);
|
||||
}
|
||||
}
|
||||
|
||||
public String getHostName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getHostAddress() {
|
||||
return address;
|
||||
try {
|
||||
return new InetAddress(name).toString();
|
||||
} catch (UnknownHostException e) {
|
||||
return null; // Strange case
|
||||
}
|
||||
}
|
||||
|
||||
public static InetAddress getByName(String name)
|
||||
throws UnknownHostException
|
||||
{
|
||||
public static InetAddress getByName(String name) throws UnknownHostException {
|
||||
try {
|
||||
Socket.init();
|
||||
return new InetAddress(name);
|
||||
} catch (IOException e) {
|
||||
UnknownHostException uhe = new UnknownHostException(name);
|
||||
uhe.initCause(e);
|
||||
throw uhe;
|
||||
}
|
||||
|
||||
int address = ipv4AddressForName(name);
|
||||
if (address == 0) {
|
||||
throw new UnknownHostException(name);
|
||||
} else {
|
||||
return new InetAddress(ipv4AddressToString(address));
|
||||
}
|
||||
}
|
||||
|
||||
private static String ipv4AddressToString(int address) {
|
||||
return (((address >>> 24) ) + "." +
|
||||
((address >>> 16) & 0xFF) + "." +
|
||||
((address >>> 8 ) & 0xFF) + "." +
|
||||
((address ) & 0xFF));
|
||||
public byte[] getAddress() {
|
||||
byte[] res = new byte[4];
|
||||
res[0] = (byte) ( ip >>> 24);
|
||||
res[1] = (byte) ((ip >>> 16) & 0xFF);
|
||||
res[2] = (byte) ((ip >>> 8 ) & 0xFF);
|
||||
res[3] = (byte) ((ip ) & 0xFF);
|
||||
return res;
|
||||
}
|
||||
|
||||
private static native int ipv4AddressForName(String name);
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
byte[] addr = getAddress();
|
||||
return (int)((addr[0] + 256) % 256) + "." +
|
||||
(int)((addr[1] + 256) % 256) + "." +
|
||||
(int)((addr[2] + 256) % 256) + "." +
|
||||
(int)((addr[3] + 256) % 256);
|
||||
}
|
||||
|
||||
int getRawAddress() {
|
||||
return ip;
|
||||
}
|
||||
|
||||
static native int ipv4AddressForName(String name);
|
||||
}
|
||||
|
Reference in New Issue
Block a user