package java.net;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
public final class InetAddress implements java.io.Serializable
{
private int address;
transient byte[] addr;
String hostName;
int family;
private static final long serialVersionUID = 3286316764910316507L;
private void readObject(ObjectInputStream ois)
throws IOException, ClassNotFoundException
{
ois.defaultReadObject();
addr = new byte[4];
addr[3] = (byte) address;
for (int i = 2; i >= 0; --i)
addr[i] = (byte) (address >>= 8);
family = getFamily (addr);
}
private void writeObject(ObjectOutputStream oos) throws IOException
{
int len = addr.length;
int i = len - 4;
for (; i < len; i++)
address = address << 8 | (((int) addr[i]) & 0xFF);
oos.defaultWriteObject();
}
private static native int getFamily (byte[] address);
InetAddress (byte[] address, String hostname)
{
addr = address;
hostName = hostname;
if (address != null)
family = getFamily (address);
}
public boolean isMulticastAddress ()
{
int len = addr.length;
if (len == 4)
return (addr[0] & 0xF0) == 0xE0;
if (len == 16)
return addr[0] == (byte) 0xFF;
return false;
}
public String getHostName ()
{
if (hostName == null)
lookup (null, this, false);
return hostName;
}
public byte[] getAddress ()
{
return (byte[]) addr.clone();
}
private static InetAddress[] allocArray (int count)
{
return new InetAddress[count];
}
private static SecurityException checkConnect (String hostname)
{
SecurityManager s = System.getSecurityManager();
if (s == null)
return null;
try
{
s.checkConnect(hostname, -1);
return null;
}
catch (SecurityException ex)
{
return ex;
}
}
public String getHostAddress ()
{
StringBuffer sbuf = new StringBuffer(40);
int len = addr.length;
int i = 0;
if (len == 16)
{ for (; ; i += 2)
{
if (i >= 16)
return sbuf.toString();
int x = ((addr[i] & 0xFF) << 8) | (addr[i+1] & 0xFF);
boolean empty = sbuf.length() == 0;
if (empty)
{
if (i == 10 && x == 0xFFFF)
{ sbuf.append(":FFFF:");
break; }
else if (i == 12)
{ sbuf.append(':');
break; }
else if (i > 0)
sbuf.append("::");
}
else
sbuf.append(':');
if (x != 0 || i >= 14)
sbuf.append(Integer.toHexString(x).toUpperCase());
}
}
for ( ; ; )
{
sbuf.append(addr[i] & 0xFF);
i++;
if (i == len)
break;
sbuf.append('.');
}
return sbuf.toString();
}
public int hashCode()
{
int hash = 0;
int len = addr.length;
int i = len > 4 ? len - 4 : 0;
for ( ; i < len; i++)
hash = (hash << 8) | (addr[i] & 0xFF);
return hash;
}
public boolean equals (Object obj)
{
if (obj == null || ! (obj instanceof InetAddress))
return false;
byte[] addr1 = addr;
byte[] addr2 = ((InetAddress) obj).addr;
if (addr1.length != addr2.length)
return false;
for (int i = addr1.length; --i >= 0; )
if (addr1[i] != addr2[i])
return false;
return true;
}
public String toString()
{
return getHostName()+'/'+getHostAddress();
}
private static native byte[] aton (String host);
private static native InetAddress[] lookup
(String hostname, InetAddress addr, boolean all);
public static InetAddress getByName (String host)
throws UnknownHostException
{
if (host == null)
return getLocalHost();
byte[] address = aton(host);
if (address != null)
return new InetAddress(address, null);
InetAddress iaddr = new InetAddress(null, host);
lookup(host, iaddr, false);
return iaddr;
}
public static InetAddress[] getAllByName (String host)
throws UnknownHostException
{
byte[] address = aton(host);
if (address != null)
{
InetAddress[] result = new InetAddress[1];
result[0] = new InetAddress(address, null);
return result;
}
return lookup(host, null, true);
}
static final byte[] zeros = {0,0,0,0};
static final InetAddress ANY_IF = new InetAddress(zeros, null);
private static final byte[] localhostAddress = { 127, 0, 0, 1 };
private static native String getLocalHostname ();
private static InetAddress localhost = null;
public static InetAddress getLocalHost() throws UnknownHostException
{
SecurityManager s = System.getSecurityManager();
if (localhost == null
|| (s != null && localhost.addr != localhostAddress))
getLocalHost(s);
return localhost;
}
private static synchronized void getLocalHost(SecurityManager s)
throws UnknownHostException
{
if (s == null && localhost != null)
return;
String hostname = getLocalHostname();
if (s != null)
{
try
{
s.checkConnect(hostname, -1);
}
catch (SecurityException ex)
{
hostname = null;
}
}
if (hostname != null)
{
try
{
localhost = new InetAddress(null, null);
lookup(hostname, localhost, false);
}
catch (Exception ex)
{
}
}
if (localhost == null)
localhost = new InetAddress (localhostAddress, "localhost");
}
}