home *** CD-ROM | disk | FTP | other *** search
/ Print Shop Ensemble 3 / the-print-shop-ensemble-iii.iso / worldnet / disk2 / java.z / MOZ2_01.ZIP / java / net / InetAddress.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-03-08  |  4.5 KB  |  179 lines

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