home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / java / net / URL.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  6.1 KB  |  209 lines

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