home *** CD-ROM | disk | FTP | other *** search
/ Sky at Night 2007 June / SAN CD 6-2007 CD-ROM 25.iso / pc / Software / AstroGrav_Win / Java / jre1.6.0 / lib / rt.jar / java / net / Inet6Address.class (.txt) < prev    next >
Encoding:
Java Class File  |  2006-11-29  |  5.9 KB  |  335 lines

  1. package java.net;
  2.  
  3. import java.io.IOException;
  4. import java.io.InvalidObjectException;
  5. import java.io.ObjectInputStream;
  6. import java.io.ObjectOutputStream;
  7. import java.util.Enumeration;
  8.  
  9. public final class Inet6Address extends InetAddress {
  10.    static final int INADDRSZ = 16;
  11.    private transient int cached_scope_id = 0;
  12.    byte[] ipaddress;
  13.    private int scope_id = 0;
  14.    private boolean scope_id_set = false;
  15.    private transient NetworkInterface scope_ifname = null;
  16.    private boolean scope_ifname_set = false;
  17.    private static final long serialVersionUID = 6880410070516793377L;
  18.    private static final int INT16SZ = 2;
  19.    private String ifname;
  20.  
  21.    Inet6Address() {
  22.       this.hostName = null;
  23.       this.ipaddress = new byte[16];
  24.       this.family = 2;
  25.    }
  26.  
  27.    Inet6Address(String var1, byte[] var2, int var3) {
  28.       this.hostName = var1;
  29.       if (var2.length == 16) {
  30.          this.family = 2;
  31.          this.ipaddress = (byte[])(([B)var2).clone();
  32.       }
  33.  
  34.       if (var3 >= 0) {
  35.          this.scope_id = var3;
  36.          this.scope_id_set = true;
  37.       }
  38.  
  39.    }
  40.  
  41.    Inet6Address(String var1, byte[] var2) {
  42.       try {
  43.          this.initif(var1, var2, (NetworkInterface)null);
  44.       } catch (UnknownHostException var4) {
  45.       }
  46.  
  47.    }
  48.  
  49.    Inet6Address(String var1, byte[] var2, NetworkInterface var3) throws UnknownHostException {
  50.       this.initif(var1, var2, var3);
  51.    }
  52.  
  53.    Inet6Address(String var1, byte[] var2, String var3) throws UnknownHostException {
  54.       this.initstr(var1, var2, var3);
  55.    }
  56.  
  57.    public static Inet6Address getByAddress(String var0, byte[] var1, NetworkInterface var2) throws UnknownHostException {
  58.       if (var0 != null && var0.length() > 0 && var0.charAt(0) == '[' && var0.charAt(var0.length() - 1) == ']') {
  59.          var0 = var0.substring(1, var0.length() - 1);
  60.       }
  61.  
  62.       if (var1 != null && var1.length == 16) {
  63.          return new Inet6Address(var0, var1, var2);
  64.       } else {
  65.          throw new UnknownHostException("addr is of illegal length");
  66.       }
  67.    }
  68.  
  69.    public static Inet6Address getByAddress(String var0, byte[] var1, int var2) throws UnknownHostException {
  70.       if (var0 != null && var0.length() > 0 && var0.charAt(0) == '[' && var0.charAt(var0.length() - 1) == ']') {
  71.          var0 = var0.substring(1, var0.length() - 1);
  72.       }
  73.  
  74.       if (var1 != null && var1.length == 16) {
  75.          return new Inet6Address(var0, var1, var2);
  76.       } else {
  77.          throw new UnknownHostException("addr is of illegal length");
  78.       }
  79.    }
  80.  
  81.    private void initstr(String var1, byte[] var2, String var3) throws UnknownHostException {
  82.       try {
  83.          NetworkInterface var4 = NetworkInterface.getByName(var3);
  84.          if (var4 == null) {
  85.             throw new UnknownHostException("no such interface " + var3);
  86.          } else {
  87.             this.initif(var1, var2, var4);
  88.          }
  89.       } catch (SocketException var5) {
  90.          throw new UnknownHostException("SocketException thrown" + var3);
  91.       }
  92.    }
  93.  
  94.    private void initif(String var1, byte[] var2, NetworkInterface var3) throws UnknownHostException {
  95.       this.hostName = var1;
  96.       if (var2.length == 16) {
  97.          this.family = 2;
  98.          this.ipaddress = (byte[])(([B)var2).clone();
  99.       }
  100.  
  101.       if (var3 != null) {
  102.          this.scope_ifname = var3;
  103.          this.scope_ifname_set = true;
  104.          this.scope_id = this.deriveNumericScope(var3);
  105.          this.scope_id_set = true;
  106.       }
  107.  
  108.    }
  109.  
  110.    private boolean differentLocalAddressTypes(Inet6Address var1) {
  111.       if (this.isLinkLocalAddress() && !var1.isLinkLocalAddress()) {
  112.          return false;
  113.       } else {
  114.          return !this.isSiteLocalAddress() || var1.isSiteLocalAddress();
  115.       }
  116.    }
  117.  
  118.    private int deriveNumericScope(NetworkInterface var1) throws UnknownHostException {
  119.       Enumeration var2 = var1.getInetAddresses();
  120.  
  121.       while(var2.hasMoreElements()) {
  122.          InetAddress var3 = (InetAddress)var2.nextElement();
  123.          if (var3 instanceof Inet6Address) {
  124.             Inet6Address var4 = (Inet6Address)var3;
  125.             if (this.differentLocalAddressTypes(var4)) {
  126.                return var4.scope_id;
  127.             }
  128.          }
  129.       }
  130.  
  131.       throw new UnknownHostException("no scope_id found");
  132.    }
  133.  
  134.    private int deriveNumericScope(String var1) throws UnknownHostException {
  135.       Enumeration var2;
  136.       try {
  137.          var2 = NetworkInterface.getNetworkInterfaces();
  138.       } catch (SocketException var7) {
  139.          throw new UnknownHostException("could not enumerate local network interfaces");
  140.       }
  141.  
  142.       while(var2.hasMoreElements()) {
  143.          NetworkInterface var3 = (NetworkInterface)var2.nextElement();
  144.          if (var3.getName().equals(var1)) {
  145.             Enumeration var4 = var3.getInetAddresses();
  146.  
  147.             while(var4.hasMoreElements()) {
  148.                InetAddress var5 = (InetAddress)var4.nextElement();
  149.                if (var5 instanceof Inet6Address) {
  150.                   Inet6Address var6 = (Inet6Address)var5;
  151.                   if (this.differentLocalAddressTypes(var6)) {
  152.                      return var6.scope_id;
  153.                   }
  154.                }
  155.             }
  156.          }
  157.       }
  158.  
  159.       throw new UnknownHostException("No matching address found for interface : " + var1);
  160.    }
  161.  
  162.    private void readObject(ObjectInputStream var1) throws IOException, ClassNotFoundException {
  163.       this.scope_ifname = null;
  164.       this.scope_ifname_set = false;
  165.       var1.defaultReadObject();
  166.       if (this.ifname != null && !"".equals(this.ifname)) {
  167.          try {
  168.             this.scope_ifname = NetworkInterface.getByName(this.ifname);
  169.  
  170.             try {
  171.                this.scope_id = this.deriveNumericScope(this.scope_ifname);
  172.             } catch (UnknownHostException var3) {
  173.                assert false;
  174.             }
  175.          } catch (SocketException var4) {
  176.          }
  177.  
  178.          if (this.scope_ifname == null) {
  179.             this.scope_id_set = false;
  180.             this.scope_ifname_set = false;
  181.             this.scope_id = 0;
  182.          }
  183.       }
  184.  
  185.       this.ipaddress = (byte[])this.ipaddress.clone();
  186.       if (this.ipaddress.length != 16) {
  187.          throw new InvalidObjectException("invalid address length: " + this.ipaddress.length);
  188.       } else if (this.family != 2) {
  189.          throw new InvalidObjectException("invalid address family type");
  190.       }
  191.    }
  192.  
  193.    public boolean isMulticastAddress() {
  194.       return (this.ipaddress[0] & 255) == 255;
  195.    }
  196.  
  197.    public boolean isAnyLocalAddress() {
  198.       byte var1 = 0;
  199.  
  200.       for(int var2 = 0; var2 < 16; ++var2) {
  201.          var1 |= this.ipaddress[var2];
  202.       }
  203.  
  204.       return var1 == 0;
  205.    }
  206.  
  207.    public boolean isLoopbackAddress() {
  208.       byte var1 = 0;
  209.  
  210.       for(int var2 = 0; var2 < 15; ++var2) {
  211.          var1 |= this.ipaddress[var2];
  212.       }
  213.  
  214.       return var1 == 0 && this.ipaddress[15] == 1;
  215.    }
  216.  
  217.    public boolean isLinkLocalAddress() {
  218.       return (this.ipaddress[0] & 255) == 254 && (this.ipaddress[1] & 192) == 128;
  219.    }
  220.  
  221.    public boolean isSiteLocalAddress() {
  222.       return (this.ipaddress[0] & 255) == 254 && (this.ipaddress[1] & 192) == 192;
  223.    }
  224.  
  225.    public boolean isMCGlobal() {
  226.       return (this.ipaddress[0] & 255) == 255 && (this.ipaddress[1] & 15) == 14;
  227.    }
  228.  
  229.    public boolean isMCNodeLocal() {
  230.       return (this.ipaddress[0] & 255) == 255 && (this.ipaddress[1] & 15) == 1;
  231.    }
  232.  
  233.    public boolean isMCLinkLocal() {
  234.       return (this.ipaddress[0] & 255) == 255 && (this.ipaddress[1] & 15) == 2;
  235.    }
  236.  
  237.    public boolean isMCSiteLocal() {
  238.       return (this.ipaddress[0] & 255) == 255 && (this.ipaddress[1] & 15) == 5;
  239.    }
  240.  
  241.    public boolean isMCOrgLocal() {
  242.       return (this.ipaddress[0] & 255) == 255 && (this.ipaddress[1] & 15) == 8;
  243.    }
  244.  
  245.    public byte[] getAddress() {
  246.       return (byte[])this.ipaddress.clone();
  247.    }
  248.  
  249.    public int getScopeId() {
  250.       return this.scope_id;
  251.    }
  252.  
  253.    public NetworkInterface getScopedInterface() {
  254.       return this.scope_ifname;
  255.    }
  256.  
  257.    public String getHostAddress() {
  258.       String var1 = numericToTextFormat(this.ipaddress);
  259.       if (this.scope_ifname_set) {
  260.          var1 = var1 + "%" + this.scope_ifname.getName();
  261.       } else if (this.scope_id_set) {
  262.          var1 = var1 + "%" + this.scope_id;
  263.       }
  264.  
  265.       return var1;
  266.    }
  267.  
  268.    public int hashCode() {
  269.       if (this.ipaddress == null) {
  270.          return 0;
  271.       } else {
  272.          int var1 = 0;
  273.  
  274.          int var4;
  275.          for(int var2 = 0; var2 < 16; var1 += var4) {
  276.             int var3 = 0;
  277.  
  278.             for(var4 = 0; var3 < 4 && var2 < 16; ++var2) {
  279.                var4 = (var4 << 8) + this.ipaddress[var2];
  280.                ++var3;
  281.             }
  282.          }
  283.  
  284.          return var1;
  285.       }
  286.    }
  287.  
  288.    public boolean equals(Object var1) {
  289.       if (var1 != null && var1 instanceof Inet6Address) {
  290.          Inet6Address var2 = (Inet6Address)var1;
  291.  
  292.          for(int var3 = 0; var3 < 16; ++var3) {
  293.             if (this.ipaddress[var3] != var2.ipaddress[var3]) {
  294.                return false;
  295.             }
  296.          }
  297.  
  298.          return true;
  299.       } else {
  300.          return false;
  301.       }
  302.    }
  303.  
  304.    public boolean isIPv4CompatibleAddress() {
  305.       return this.ipaddress[0] == 0 && this.ipaddress[1] == 0 && this.ipaddress[2] == 0 && this.ipaddress[3] == 0 && this.ipaddress[4] == 0 && this.ipaddress[5] == 0 && this.ipaddress[6] == 0 && this.ipaddress[7] == 0 && this.ipaddress[8] == 0 && this.ipaddress[9] == 0 && this.ipaddress[10] == 0 && this.ipaddress[11] == 0;
  306.    }
  307.  
  308.    static String numericToTextFormat(byte[] var0) {
  309.       StringBuffer var1 = new StringBuffer(39);
  310.  
  311.       for(int var2 = 0; var2 < 8; ++var2) {
  312.          var1.append(Integer.toHexString(var0[var2 << 1] << 8 & '\uff00' | var0[(var2 << 1) + 1] & 255));
  313.          if (var2 < 7) {
  314.             var1.append(":");
  315.          }
  316.       }
  317.  
  318.       return var1.toString();
  319.    }
  320.  
  321.    private static native void init();
  322.  
  323.    private synchronized void writeObject(ObjectOutputStream var1) throws IOException {
  324.       if (this.scope_ifname_set) {
  325.          this.ifname = this.scope_ifname.getName();
  326.       }
  327.  
  328.       var1.defaultWriteObject();
  329.    }
  330.  
  331.    static {
  332.       init();
  333.    }
  334. }
  335.