home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 14 / CDACTUAL.iso / ie4 / IE4 / JAVI386.CAB / classr.exe / java / net / URL.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-01-31  |  5.5 KB  |  227 lines

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