home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1999 April / DPPCPRO0499.ISO / April / Notes / 50b2wic.exe / DATA1.CAB / NotesProgramFilesJavaSupport / rt.jar / java / net / InetAddress.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-04-23  |  6.0 KB  |  274 lines

  1. package java.net;
  2.  
  3. import java.io.Serializable;
  4. import java.util.Hashtable;
  5.  
  6. public final class InetAddress implements Serializable {
  7.    String hostName;
  8.    int address;
  9.    int family;
  10.    private static final long serialVersionUID = 3286316764910316507L;
  11.    static Hashtable addressCache;
  12.    static InetAddress unknownAddress;
  13.    static InetAddress anyLocalAddress;
  14.    static InetAddress localHost;
  15.    static InetAddress[] unknown_array;
  16.    static InetAddressImpl impl;
  17.    private static InetAddress loopbackHost;
  18.  
  19.    InetAddress() {
  20.       this.family = impl.getInetFamily();
  21.    }
  22.  
  23.    InetAddress(String var1, byte[] var2) {
  24.       this.hostName = new String(var1);
  25.       this.family = impl.getInetFamily();
  26.       this.address = var2[3] & 255;
  27.       this.address |= var2[2] << 8 & '\uff00';
  28.       this.address |= var2[1] << 16 & 16711680;
  29.       this.address |= var2[0] << 24 & -16777216;
  30.    }
  31.  
  32.    public boolean isMulticastAddress() {
  33.       return (this.address & -268435456) == -536870912;
  34.    }
  35.  
  36.    public String getHostName() {
  37.       if (this.hostName == null) {
  38.          try {
  39.             this.hostName = new String(impl.getHostByAddr(this.address));
  40.             InetAddress[] var1 = (InetAddress[])addressCache.get(this.hostName);
  41.             if (var1 != null) {
  42.                for(int var2 = 0; var2 < var1.length; ++var2) {
  43.                   if (this.hostName.equalsIgnoreCase(var1[var2].hostName) && this.address != var1[var2].address) {
  44.                      this.hostName = this.getHostAddress();
  45.                      break;
  46.                   }
  47.                }
  48.             } else {
  49.                var1 = new InetAddress[]{this};
  50.                addressCache.put(this.hostName, var1);
  51.             }
  52.  
  53.             SecurityManager var6 = System.getSecurityManager();
  54.             if (var6 != null && !var6.getInCheck()) {
  55.                var6.checkConnect(this.hostName, -1);
  56.             }
  57.          } catch (SecurityException var3) {
  58.             this.hostName = this.getHostAddress();
  59.          } catch (UnknownHostException var4) {
  60.             this.hostName = this.getHostAddress();
  61.          }
  62.       }
  63.  
  64.       return this.hostName;
  65.    }
  66.  
  67.    public byte[] getAddress() {
  68.       byte[] var1 = new byte[]{(byte)(this.address >>> 24 & 255), (byte)(this.address >>> 16 & 255), (byte)(this.address >>> 8 & 255), (byte)(this.address & 255)};
  69.       return var1;
  70.    }
  71.  
  72.    public String getHostAddress() {
  73.       return (this.address >>> 24 & 255) + "." + (this.address >>> 16 & 255) + "." + (this.address >>> 8 & 255) + "." + (this.address & 255);
  74.    }
  75.  
  76.    public int hashCode() {
  77.       return this.address;
  78.    }
  79.  
  80.    public boolean equals(Object var1) {
  81.       return var1 != null && var1 instanceof InetAddress && ((InetAddress)var1).address == this.address;
  82.    }
  83.  
  84.    public String toString() {
  85.       return this.getHostName() + "/" + this.getHostAddress();
  86.    }
  87.  
  88.    public static InetAddress getByName(String var0) throws UnknownHostException {
  89.       if (var0 != null && var0.length() != 0) {
  90.          if (!Character.isDigit(var0.charAt(0))) {
  91.             return getAllByName0(var0)[0];
  92.          } else {
  93.             int var1 = 0;
  94.             int var2 = 0;
  95.             char[] var3 = var0.toCharArray();
  96.             int var4 = 0;
  97.  
  98.             label53:
  99.             while(var4 < var3.length) {
  100.                char var5 = var3[var4];
  101.                if (var5 >= '0' && var5 <= '9') {
  102.                   int var6 = 0;
  103.  
  104.                   while(true) {
  105.                      if (var5 != '.') {
  106.                         if (var5 < '0' || var5 > '9') {
  107.                            return getAllByName0(var0)[0];
  108.                         }
  109.  
  110.                         var6 = var6 * 10 + var5 - 48;
  111.                         ++var4;
  112.                         if (var4 < var3.length) {
  113.                            var5 = var3[var4];
  114.                            continue;
  115.                         }
  116.                      }
  117.  
  118.                      if (var6 > 255) {
  119.                         return getAllByName0(var0)[0];
  120.                      }
  121.  
  122.                      var1 = (var1 << 8) + var6;
  123.                      ++var2;
  124.                      ++var4;
  125.                      continue label53;
  126.                   }
  127.                }
  128.  
  129.                return getAllByName0(var0)[0];
  130.             }
  131.  
  132.             if (var2 == 4 && !var0.endsWith(".")) {
  133.                InetAddress var7 = new InetAddress();
  134.                var7.address = var1;
  135.                var7.hostName = null;
  136.                return var7;
  137.             } else {
  138.                return getAllByName0(var0)[0];
  139.             }
  140.          }
  141.       } else {
  142.          return loopbackHost;
  143.       }
  144.    }
  145.  
  146.    public static InetAddress[] getAllByName(String var0) throws UnknownHostException {
  147.       if (var0 != null && var0.length() != 0) {
  148.          if (Character.isDigit(var0.charAt(0))) {
  149.             InetAddress[] var1 = new InetAddress[]{getByName(var0)};
  150.             return var1;
  151.          } else {
  152.             return getAllByName0(var0);
  153.          }
  154.       } else {
  155.          throw new UnknownHostException("empty string");
  156.       }
  157.    }
  158.  
  159.    private static InetAddress[] getAllByName0(String var0) throws UnknownHostException {
  160.       Object var1 = null;
  161.       Object var2 = null;
  162.       SecurityManager var3 = System.getSecurityManager();
  163.       if (var3 != null && !var3.getInCheck()) {
  164.          var3.checkConnect(var0, -1);
  165.       }
  166.  
  167.       Hashtable var4 = addressCache;
  168.       synchronized(var4){}
  169.  
  170.       try {
  171.          var1 = addressCache.get(var0);
  172.          if (var1 == null) {
  173.             try {
  174.                byte[][] var6 = impl.lookupAllHostAddr(var0);
  175.                InetAddress[] var7 = new InetAddress[var6.length];
  176.  
  177.                for(int var8 = 0; var8 < var6.length; ++var8) {
  178.                   byte[] var9 = var6[var8];
  179.                   var7[var8] = new InetAddress(var0, var9);
  180.                }
  181.  
  182.                var1 = var7;
  183.             } catch (UnknownHostException var14) {
  184.                var1 = unknown_array;
  185.             }
  186.  
  187.             addressCache.put(var0, var1);
  188.          }
  189.       } catch (Throwable var15) {
  190.          throw var15;
  191.       }
  192.  
  193.       if (var1 == unknown_array) {
  194.          throw new UnknownHostException(var0);
  195.       } else {
  196.          try {
  197.             var2 = ((InetAddress[])var1).clone();
  198.             if (var2 == null) {
  199.                throw new CloneNotSupportedException();
  200.             }
  201.          } catch (CloneNotSupportedException var13) {
  202.             ((Throwable)var13).printStackTrace();
  203.          }
  204.  
  205.          return (InetAddress[])var2;
  206.       }
  207.    }
  208.  
  209.    public static InetAddress getLocalHost() throws UnknownHostException {
  210.       if (localHost.equals(unknownAddress)) {
  211.          throw new UnknownHostException();
  212.       } else {
  213.          try {
  214.             if (localHost.address == -1) {
  215.                localHost = getAllByName(localHost.hostName)[0];
  216.             }
  217.  
  218.             SecurityManager var0 = System.getSecurityManager();
  219.             if (var0 != null && !var0.getInCheck()) {
  220.                var0.checkConnect(localHost.getHostName(), -1);
  221.             }
  222.  
  223.             return localHost;
  224.          } catch (SecurityException var1) {
  225.             return loopbackHost;
  226.          }
  227.       }
  228.    }
  229.  
  230.    static {
  231.       System.loadLibrary("net");
  232.       addressCache = new Hashtable();
  233.       String var0 = System.getProperty("impl.prefix", "");
  234.  
  235.       try {
  236.          impl = null;
  237.          impl = (InetAddressImpl)Class.forName("java.net." + var0 + "InetAddressImpl").newInstance();
  238.       } catch (ClassNotFoundException var5) {
  239.          System.err.println("Class not found: java.net." + var0 + "InetAddressImpl:\ncheck impl.prefix property " + "in your properties file.");
  240.       } catch (InstantiationException var6) {
  241.          System.err.println("Could not instantiate: java.net." + var0 + "InetAddressImpl:\ncheck impl.prefix property " + "in your properties file.");
  242.       } catch (IllegalAccessException var7) {
  243.          System.err.println("Cannot access class: java.net." + var0 + "InetAddressImpl:\ncheck impl.prefix property " + "in your properties file.");
  244.       }
  245.  
  246.       if (impl == null) {
  247.          try {
  248.             impl = (InetAddressImpl)Class.forName("java.net.InetAddressImpl").newInstance();
  249.          } catch (Exception var4) {
  250.             throw new Error("System property impl.prefix incorrect");
  251.          }
  252.       }
  253.  
  254.       unknownAddress = new InetAddress();
  255.       anyLocalAddress = new InetAddress();
  256.       impl.makeAnyLocalAddress(anyLocalAddress);
  257.       byte[] var1 = new byte[]{127, 0, 0, 1};
  258.       loopbackHost = new InetAddress("localhost", var1);
  259.  
  260.       try {
  261.          localHost = new InetAddress();
  262.          localHost.hostName = impl.getLocalHostName();
  263.          localHost.address = -1;
  264.       } catch (Exception var3) {
  265.          localHost = unknownAddress;
  266.       }
  267.  
  268.       String var2 = new String("0.0.0.0");
  269.       unknown_array = new InetAddress[1];
  270.       unknown_array[0] = new InetAddress(var2, unknownAddress.getAddress());
  271.       addressCache.put(var2, unknown_array);
  272.    }
  273. }
  274.