home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / java / net / InetAddress.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  4.1 KB  |  157 lines

  1. package java.net;
  2.  
  3. import java.util.Hashtable;
  4.  
  5. public final class InetAddress {
  6.    String hostName;
  7.    int address;
  8.    int family;
  9.    static Hashtable addressCache;
  10.    static InetAddress unknownAddress;
  11.    static InetAddress anyLocalAddress;
  12.    static InetAddress localHost;
  13.  
  14.    InetAddress() {
  15.    }
  16.  
  17.    InetAddress(String hostName, byte[] addr) {
  18.       this.hostName = hostName;
  19.       this.family = getInetFamily();
  20.       this.address = addr[3] & 255;
  21.       this.address |= addr[2] << 8 & '\uff00';
  22.       this.address |= addr[1] << 16 & 16711680;
  23.       this.address |= addr[0] << 24 & -16777216;
  24.    }
  25.  
  26.    public String getHostName() {
  27.       if (this.hostName == null) {
  28.          try {
  29.             this.hostName = getHostByAddr(this.address);
  30.          } catch (UnknownHostException var1) {
  31.             this.hostName = this.getHostAddress();
  32.          }
  33.       }
  34.  
  35.       return this.hostName;
  36.    }
  37.  
  38.    public byte[] getAddress() {
  39.       byte[] addr = new byte[4];
  40.       addr[0] = (byte)(this.address >>> 24 & 255);
  41.       addr[1] = (byte)(this.address >>> 16 & 255);
  42.       addr[2] = (byte)(this.address >>> 8 & 255);
  43.       addr[3] = (byte)(this.address & 255);
  44.       return addr;
  45.    }
  46.  
  47.    public String getHostAddress() {
  48.       return (this.address >>> 24 & 255) + "." + (this.address >>> 16 & 255) + "." + (this.address >>> 8 & 255) + "." + (this.address & 255);
  49.    }
  50.  
  51.    public int hashCode() {
  52.       return this.address;
  53.    }
  54.  
  55.    public boolean equals(Object obj) {
  56.       return obj != null && obj instanceof InetAddress && ((InetAddress)obj).address == this.address;
  57.    }
  58.  
  59.    public String toString() {
  60.       return (this.hostName != null ? this.hostName + "/" : "") + this.getHostAddress();
  61.    }
  62.  
  63.    public static InetAddress getByName(String host) throws UnknownHostException {
  64.       return host != null && host.length() != 0 ? getAllByName(host)[0] : getLocalHost();
  65.    }
  66.  
  67.    public static InetAddress[] getAllByName(String host) throws UnknownHostException {
  68.       if (host == null) {
  69.          throw new UnknownHostException(host);
  70.       } else {
  71.          SecurityManager security = System.getSecurityManager();
  72.          if (security != null && !security.getInCheck()) {
  73.             security.checkConnect(host, -1);
  74.          }
  75.  
  76.          Hashtable var3 = addressCache;
  77.          synchronized(var3){}
  78.  
  79.          Object obj;
  80.          try {
  81.             obj = addressCache.get(host);
  82.             if (obj == null) {
  83.                try {
  84.                   byte[][] byte_array = lookupAllHostAddr(host);
  85.                   if (byte_array.length == 0) {
  86.                      throw new UnknownHostException(host);
  87.                   }
  88.  
  89.                   InetAddress[] addr_array = new InetAddress[byte_array.length];
  90.  
  91.                   for(int i = 0; i < byte_array.length; ++i) {
  92.                      byte[] addr = byte_array[i];
  93.                      addr_array[i] = new InetAddress(host, addr);
  94.                   }
  95.  
  96.                   obj = addr_array;
  97.                } catch (UnknownHostException var13) {
  98.                   obj = unknownAddress;
  99.                }
  100.  
  101.                addressCache.put(host, obj);
  102.             }
  103.          } catch (Throwable var14) {
  104.             throw var14;
  105.          }
  106.  
  107.          if (obj == unknownAddress) {
  108.             throw new UnknownHostException(host);
  109.          } else {
  110.             try {
  111.                obj = ((InetAddress[])obj).clone();
  112.             } catch (CloneNotSupportedException cnse) {
  113.                ((Throwable)cnse).printStackTrace();
  114.                obj = null;
  115.             }
  116.  
  117.             return (InetAddress[])obj;
  118.          }
  119.       }
  120.    }
  121.  
  122.    public static InetAddress getLocalHost() throws UnknownHostException {
  123.       if (localHost.equals(unknownAddress)) {
  124.          throw new UnknownHostException();
  125.       } else {
  126.          return localHost;
  127.       }
  128.    }
  129.  
  130.    private static native String getLocalHostName() throws UnknownHostException;
  131.  
  132.    private static native void makeAnyLocalAddress(InetAddress var0);
  133.  
  134.    private static native byte[] lookupHostAddr(String var0) throws UnknownHostException;
  135.  
  136.    private static native byte[][] lookupAllHostAddr(String var0) throws UnknownHostException;
  137.  
  138.    private static native String getHostByAddr(int var0) throws UnknownHostException;
  139.  
  140.    private static native int getInetFamily();
  141.  
  142.    static {
  143.       SecurityManager.setScopePermission();
  144.       System.loadLibrary("net");
  145.       addressCache = new Hashtable();
  146.       unknownAddress = new InetAddress();
  147.       anyLocalAddress = new InetAddress();
  148.       makeAnyLocalAddress(anyLocalAddress);
  149.  
  150.       try {
  151.          localHost = getByName(getLocalHostName());
  152.       } catch (Exception var0) {
  153.          localHost = unknownAddress;
  154.       }
  155.    }
  156. }
  157.