home *** CD-ROM | disk | FTP | other *** search
- package java.net;
-
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Hashtable;
-
- public final class URL {
- private String protocol;
- private String host;
- private int port;
- private String file;
- private String ref;
- URLStreamHandler handler;
- static URLStreamHandlerFactory factory;
- static Hashtable handlers = new Hashtable();
-
- public URL(String protocol, String host, int port, String file) throws MalformedURLException {
- this.port = -1;
- this.protocol = protocol;
- this.host = host;
- this.file = file;
- this.port = port;
- if ((this.handler = getURLStreamHandler(protocol)) == null) {
- throw new MalformedURLException("unknown protocol: " + protocol);
- }
- }
-
- public URL(String protocol, String host, String file) throws MalformedURLException {
- this(protocol, host, -1, file);
- }
-
- public URL(String spec) throws MalformedURLException {
- this((URL)null, spec);
- }
-
- public URL(URL context, String spec) throws MalformedURLException {
- this.port = -1;
- String original = spec;
- int start = 0;
- String newProtocol = null;
-
- try {
- int limit;
- for(limit = spec.length(); limit > 0 && spec.charAt(limit - 1) <= ' '; --limit) {
- }
-
- while(start < limit && spec.charAt(start) <= ' ') {
- ++start;
- }
-
- if (spec.regionMatches(true, start, "url:", 0, 4)) {
- start += 4;
- }
-
- int c;
- for(int i = start; i < limit && (c = spec.charAt(i)) != 47; ++i) {
- if (c == 58) {
- newProtocol = spec.substring(start, i).toLowerCase();
- start = i + 1;
- break;
- }
- }
-
- if (context == null || newProtocol != null && !newProtocol.equals(context.protocol)) {
- this.protocol = newProtocol;
- } else {
- this.protocol = context.protocol;
- this.host = context.host;
- this.port = context.port;
- this.file = context.file;
- }
-
- if (this.protocol == null) {
- throw new MalformedURLException("no protocol: " + original);
- } else if ((this.handler = getURLStreamHandler(this.protocol)) == null) {
- throw new MalformedURLException("unknown protocol: " + this.protocol);
- } else {
- int var12 = spec.indexOf(35, start);
- if (var12 >= 0) {
- this.ref = spec.substring(var12 + 1, limit);
- limit = var12;
- }
-
- this.handler.parseURL(this, spec, start, limit);
- }
- } catch (MalformedURLException e) {
- throw e;
- } catch (Exception e) {
- throw new MalformedURLException(spec + ": " + e);
- }
- }
-
- protected void set(String protocol, String host, int port, String file, String ref) {
- this.protocol = protocol;
- this.host = host;
- this.port = port;
- this.file = file;
- this.ref = ref;
- }
-
- public int getPort() {
- return this.port;
- }
-
- public String getProtocol() {
- return this.protocol;
- }
-
- public String getHost() {
- return this.host;
- }
-
- public String getHostAddress() throws UnknownHostException {
- return InetAddress.getByName(this.host).getHostAddress();
- }
-
- public String getFile() {
- return this.file;
- }
-
- public String getRef() {
- return this.ref;
- }
-
- public boolean equals(Object obj) {
- return obj instanceof URL && this.sameFile((URL)obj);
- }
-
- public int hashCode() {
- return this.protocol.hashCode() ^ this.host.hashCode() ^ this.file.hashCode();
- }
-
- boolean hostsEqual(String h1, String h2) {
- if (h1.equals(h2)) {
- return true;
- } else {
- try {
- InetAddress a1 = InetAddress.getByName(h1);
- InetAddress a2 = InetAddress.getByName(h2);
- return a1.equals(a2);
- } catch (UnknownHostException var5) {
- } catch (SecurityException var6) {
- }
-
- return false;
- }
- }
-
- public boolean sameFile(URL other) {
- return this.protocol.equals(other.protocol) && this.hostsEqual(this.host, other.host) && this.port == other.port && this.file.equals(other.file);
- }
-
- public String toString() {
- return this.toExternalForm();
- }
-
- public String toExternalForm() {
- return this.handler.toExternalForm(this);
- }
-
- public URLConnection openConnection() throws IOException {
- return this.handler.openConnection(this);
- }
-
- public final InputStream openStream() throws IOException {
- return this.openConnection().getInputStream();
- }
-
- public final Object getContent() throws IOException {
- return this.openConnection().getContent();
- }
-
- public static synchronized void setURLStreamHandlerFactory(URLStreamHandlerFactory fac) {
- if (factory != null) {
- throw new Error("factory already defined");
- } else {
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
- security.checkSetFactory();
- }
-
- factory = fac;
- }
- }
-
- static synchronized URLStreamHandler getURLStreamHandler(String protocol) {
- URLStreamHandler handler = (URLStreamHandler)handlers.get(protocol);
- if (handler == null) {
- if (factory != null) {
- handler = factory.createURLStreamHandler(protocol);
- }
-
- if (handler == null) {
- try {
- String clname = "sun.net.www.protocol." + protocol + ".Handler";
- handler = (URLStreamHandler)Class.forName(clname).newInstance();
- } catch (Exception var3) {
- }
- }
-
- if (handler != null) {
- handlers.put(protocol, handler);
- }
- }
-
- return handler;
- }
- }
-