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

  1. package java.net;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.ObjectInputStream;
  6. import java.io.ObjectOutputStream;
  7. import java.io.Serializable;
  8. import java.util.Hashtable;
  9. import java.util.StringTokenizer;
  10.  
  11. public final class URL implements Serializable {
  12.    private static final String protocolPathProp = "java.protocol.handler.pkgs";
  13.    private String protocol;
  14.    private String host;
  15.    private int port;
  16.    private String file;
  17.    private String ref;
  18.    transient URLStreamHandler handler;
  19.    private int hashCode;
  20.    static URLStreamHandlerFactory factory;
  21.    static Hashtable handlers = new Hashtable();
  22.  
  23.    public URL(String var1, String var2, int var3, String var4) throws MalformedURLException {
  24.       this.port = -1;
  25.       this.hashCode = -1;
  26.       this.protocol = var1;
  27.       this.host = var2;
  28.       this.port = var3;
  29.       int var5 = var4.indexOf(35);
  30.       this.file = var5 < 0 ? var4 : var4.substring(0, var5);
  31.       this.ref = var5 < 0 ? null : var4.substring(var5 + 1);
  32.       if ((this.handler = getURLStreamHandler(var1)) == null) {
  33.          throw new MalformedURLException("unknown protocol: " + var1);
  34.       }
  35.    }
  36.  
  37.    public URL(String var1, String var2, String var3) throws MalformedURLException {
  38.       this(var1, var2, -1, var3);
  39.    }
  40.  
  41.    public URL(String var1) throws MalformedURLException {
  42.       this((URL)null, var1);
  43.    }
  44.  
  45.    public URL(URL var1, String var2) throws MalformedURLException {
  46.       this.port = -1;
  47.       this.hashCode = -1;
  48.       String var3 = var2;
  49.       int var7 = 0;
  50.       String var8 = null;
  51.       boolean var9 = false;
  52.  
  53.       try {
  54.          int var5;
  55.          for(var5 = var2.length(); var5 > 0 && var2.charAt(var5 - 1) <= ' '; --var5) {
  56.          }
  57.  
  58.          while(var7 < var5 && var2.charAt(var7) <= ' ') {
  59.             ++var7;
  60.          }
  61.  
  62.          if (var2.regionMatches(true, var7, "url:", 0, 4)) {
  63.             var7 += 4;
  64.          }
  65.  
  66.          if (var7 < var2.length() && var2.charAt(var7) == '#') {
  67.             var9 = true;
  68.          }
  69.  
  70.          char var6;
  71.          for(int var4 = var7; !var9 && var4 < var5 && (var6 = var2.charAt(var4)) != '/'; ++var4) {
  72.             if (var6 == ':') {
  73.                var8 = var2.substring(var7, var4).toLowerCase();
  74.                var7 = var4 + 1;
  75.                break;
  76.             }
  77.          }
  78.  
  79.          if (var1 == null || var8 != null && !var8.equals(var1.protocol)) {
  80.             this.protocol = var8;
  81.          } else {
  82.             this.protocol = var1.protocol;
  83.             this.host = var1.host;
  84.             this.port = var1.port;
  85.             this.file = var1.file;
  86.          }
  87.  
  88.          if (this.protocol == null) {
  89.             throw new MalformedURLException("no protocol: " + var3);
  90.          } else if ((this.handler = getURLStreamHandler(this.protocol)) == null) {
  91.             throw new MalformedURLException("unknown protocol: " + this.protocol);
  92.          } else {
  93.             int var13 = var2.indexOf(35, var7);
  94.             if (var13 >= 0) {
  95.                this.ref = var2.substring(var13 + 1, var5);
  96.                var5 = var13;
  97.             }
  98.  
  99.             this.handler.parseURL(this, var2, var7, var5);
  100.          }
  101.       } catch (MalformedURLException var11) {
  102.          throw var11;
  103.       } catch (Exception var12) {
  104.          throw new MalformedURLException(var2 + ": " + var12);
  105.       }
  106.    }
  107.  
  108.    protected void set(String var1, String var2, int var3, String var4, String var5) {
  109.       this.protocol = var1;
  110.       this.host = var2;
  111.       this.port = var3;
  112.       this.file = var4;
  113.       this.ref = var5;
  114.    }
  115.  
  116.    public int getPort() {
  117.       return this.port;
  118.    }
  119.  
  120.    public String getProtocol() {
  121.       return this.protocol;
  122.    }
  123.  
  124.    public String getHost() {
  125.       return this.host;
  126.    }
  127.  
  128.    public String getFile() {
  129.       return this.file;
  130.    }
  131.  
  132.    public String getRef() {
  133.       return this.ref;
  134.    }
  135.  
  136.    public boolean equals(Object var1) {
  137.       if (this.ref == null) {
  138.          return var1 instanceof URL && this.sameFile((URL)var1);
  139.       } else {
  140.          return var1 instanceof URL && this.sameFile((URL)var1) && this.ref.equals(((URL)var1).ref);
  141.       }
  142.    }
  143.  
  144.    public int hashCode() {
  145.       if (this.hashCode == -1) {
  146.          this.hashCode = this.host.toLowerCase().hashCode() ^ this.file.hashCode() ^ this.protocol.hashCode();
  147.       }
  148.  
  149.       return this.hashCode;
  150.    }
  151.  
  152.    boolean hostsEqual(String var1, String var2) {
  153.       if (var1.equals(var2)) {
  154.          return true;
  155.       } else {
  156.          try {
  157.             InetAddress var3 = InetAddress.getByName(var1);
  158.             InetAddress var4 = InetAddress.getByName(var2);
  159.             return var3.equals(var4);
  160.          } catch (UnknownHostException var5) {
  161.          } catch (SecurityException var6) {
  162.          }
  163.  
  164.          return false;
  165.       }
  166.    }
  167.  
  168.    public boolean sameFile(URL var1) {
  169.       return this.protocol.equals(var1.protocol) && this.hostsEqual(this.host, var1.host) && this.port == var1.port && this.file.equals(var1.file);
  170.    }
  171.  
  172.    public String toString() {
  173.       return this.toExternalForm();
  174.    }
  175.  
  176.    public String toExternalForm() {
  177.       return this.handler.toExternalForm(this);
  178.    }
  179.  
  180.    public URLConnection openConnection() throws IOException {
  181.       return this.handler.openConnection(this);
  182.    }
  183.  
  184.    public final InputStream openStream() throws IOException {
  185.       return this.openConnection().getInputStream();
  186.    }
  187.  
  188.    public final Object getContent() throws IOException {
  189.       return this.openConnection().getContent();
  190.    }
  191.  
  192.    public static synchronized void setURLStreamHandlerFactory(URLStreamHandlerFactory var0) {
  193.       if (factory != null) {
  194.          throw new Error("factory already defined");
  195.       } else {
  196.          SecurityManager var1 = System.getSecurityManager();
  197.          if (var1 != null) {
  198.             var1.checkSetFactory();
  199.          }
  200.  
  201.          handlers.clear();
  202.          factory = var0;
  203.       }
  204.    }
  205.  
  206.    static synchronized URLStreamHandler getURLStreamHandler(String var0) {
  207.       URLStreamHandler var1 = (URLStreamHandler)handlers.get(var0);
  208.       if (var1 == null) {
  209.          if (factory != null) {
  210.             var1 = factory.createURLStreamHandler(var0);
  211.          }
  212.  
  213.          if (var1 == null) {
  214.             String var2 = System.getProperty("java.protocol.handler.pkgs", "");
  215.             if (var2 != "") {
  216.                var2 = var2 + "|";
  217.             }
  218.  
  219.             var2 = var2 + "sun.net.www.protocol";
  220.             StringTokenizer var3 = new StringTokenizer(var2, "|");
  221.  
  222.             while(var1 == null && var3.hasMoreTokens()) {
  223.                String var4 = var3.nextToken().trim();
  224.  
  225.                try {
  226.                   String var5 = var4 + "." + var0 + ".Handler";
  227.                   var1 = (URLStreamHandler)Class.forName(var5).newInstance();
  228.                } catch (Exception var6) {
  229.                }
  230.             }
  231.          }
  232.  
  233.          if (var1 != null) {
  234.             handlers.put(var0, var1);
  235.          }
  236.       }
  237.  
  238.       return var1;
  239.    }
  240.  
  241.    private synchronized void writeObject(ObjectOutputStream var1) throws IOException {
  242.       var1.defaultWriteObject();
  243.    }
  244.  
  245.    private synchronized void readObject(ObjectInputStream var1) throws IOException, ClassNotFoundException {
  246.       var1.defaultReadObject();
  247.       if ((this.handler = getURLStreamHandler(this.protocol)) == null) {
  248.          throw new IOException("unknown protocol: " + this.protocol);
  249.       }
  250.    }
  251. }
  252.