package java.net;
import java.io.*;
import java.util.Hashtable;
import java.util.StringTokenizer;
public final class URL implements Serializable
{
private String protocol;
private String host;
private int port = -1; private String file;
private String ref;
private int hashCode = 0;
transient private URLStreamHandler handler;
private static Hashtable handlers = new Hashtable();
private static URLStreamHandlerFactory factory;
private static final long serialVersionUID = -7627629688361524110L;
public URL(String protocol, String host, int port, String file)
throws MalformedURLException
{
this(protocol, host, port, file, null);
}
public URL(String protocol, String host, String file)
throws MalformedURLException
{
this(protocol, host, -1, file, null);
}
public URL(String protocol, String host, int port, String file,
URLStreamHandler handler) throws MalformedURLException
{
if (protocol == null)
throw new MalformedURLException("null protocol");
this.protocol = protocol;
if (handler != null)
{
this.handler = handler;
}
else
this.handler = setURLStreamHandler(protocol);
if (this.handler == null)
throw new MalformedURLException("Protocol handler not found: " + protocol);
this.host = host;
this.port = port;
int hashAt = file.indexOf('#');
if (hashAt < 0)
{
this.file = file;
this.ref = null;
}
else
{
this.file = file.substring(0, hashAt);
this.ref = file.substring(hashAt + 1);
}
hashCode = hashCode(); }
public URL(String spec) throws MalformedURLException
{
this((URL) null, spec, (URLStreamHandler) null);
}
public URL(URL context, String spec) throws MalformedURLException
{
this(context, spec, (URLStreamHandler) null);
}
public URL(URL context, String spec, URLStreamHandler handler)
throws MalformedURLException
{
int colon;
if ((colon = spec.indexOf("://", 1)) > 0 &&
! spec.regionMatches(colon, "://:", 0, 4))
context = null;
int slash;
if ((colon = spec.indexOf(':')) > 0 &&
(colon < (slash = spec.indexOf('/')) || slash < 0))
{
protocol = spec.substring(0, colon);
if (context != null && context.protocol.equals(protocol))
{
host = context.host;
port = context.port;
file = context.file;
}
}
else if (context != null)
{
colon = -1;
protocol = context.protocol;
host = context.host;
port = context.port;
file = context.file;
}
else throw new
MalformedURLException("Absolute URL required with null context");
if (handler != null)
{
this.handler = handler;
}
else
this.handler = setURLStreamHandler(protocol);
if (this.handler == null)
throw new MalformedURLException("Protocol handler not found: " + protocol);
int hashAt = spec.indexOf('#', colon + 1);
this.handler.parseURL(this, spec, colon + 1,
hashAt < 0 ? spec.length() : hashAt);
if (hashAt >= 0)
ref = spec.substring(hashAt + 1);
hashCode = hashCode(); }
public boolean equals(Object obj)
{
if (obj == null || ! (obj instanceof URL))
return false;
URL uObj = (URL) obj;
return (port == uObj.port
&& ((protocol == null && uObj.protocol == null)
|| (protocol != null && protocol.equals(uObj.protocol)))
&& ((host == null && uObj.host == null)
|| (host != null && host.equals(uObj.host)))
&& ((file == null && uObj.file == null)
|| (file != null && file.equals(uObj.file)))
&& ((ref == null && uObj.ref == null)
|| (ref != null && ref.equals(uObj.ref))));
}
public final Object getContent() throws IOException
{
return openConnection().getContent();
}
public String getFile()
{
return file;
}
public String getPath()
{
int quest = file.indexOf('?');
return quest < 0 ? file : file.substring(0, quest);
}
public String getHost()
{
return host;
}
public int getPort()
{
return port;
}
public String getProtocol()
{
return protocol;
}
public String getRef()
{
return ref;
}
public int hashCode()
{
if (hashCode != 0)
return hashCode; else
return (protocol.hashCode() + ((host == null) ? 0 : host.hashCode()) +
port + file.hashCode());
}
public URLConnection openConnection() throws IOException
{
return handler.openConnection(this);
}
public final InputStream openStream() throws IOException
{
return openConnection().getInputStream();
}
public boolean sameFile(URL other)
{
return handler.sameFile(this, other);
}
protected void set(String protocol, String host, int port, String file,
String ref)
{
this.handler = setURLStreamHandler(protocol);
this.protocol = protocol;
this.port = port;
this.host = host;
this.file = file;
this.ref = ref;
hashCode = hashCode(); }
public static synchronized void
setURLStreamHandlerFactory(URLStreamHandlerFactory fac)
{
if (factory != null)
throw new Error("URLStreamHandlerFactory already set");
SecurityManager s = System.getSecurityManager();
if (s != null)
s.checkSetFactory();
factory = fac;
}
public String toExternalForm()
{
return handler.toExternalForm(this);
}
public String toString()
{
return handler.toExternalForm(this);
}
private URLStreamHandler setURLStreamHandler(String protocol)
{
URLStreamHandler handler;
if ((handler = (URLStreamHandler) handlers.get(protocol)) != null)
return handler;
if (factory != null)
handler = factory.createURLStreamHandler(protocol);
else if (protocol.equals ("core"))
{
handler = new gnu.gcj.protocol.core.Handler ();
}
else if (protocol.equals ("file"))
{
handler = new gnu.gcj.protocol.file.Handler ();
}
if (handler == null)
{
String propVal = System.getProperty("java.protocol.handler.pkgs");
propVal = (propVal == null) ? "" : (propVal + "|");
propVal = propVal + "gnu.gcj.protocol|sun.net.www.protocol";
StringTokenizer pkgPrefix = new StringTokenizer(propVal, "|");
do
{
String facName = pkgPrefix.nextToken() + "." + protocol +
".Handler";
try
{
handler =
(URLStreamHandler) Class.forName(facName).newInstance();
}
catch (Exception e)
{
}
} while ((handler == null ||
! (handler instanceof URLStreamHandler)) &&
pkgPrefix.hasMoreTokens());
}
if (handler != null)
if (handler instanceof URLStreamHandler)
handlers.put(protocol, handler);
else
handler = null;
return handler;
}
private void readObject(ObjectInputStream ois)
throws IOException, ClassNotFoundException
{
ois.defaultReadObject();
this.handler = setURLStreamHandler(protocol);
if (this.handler == null)
throw new IOException("Handler for protocol " + protocol + " not found");
}
private void writeObject(ObjectOutputStream oos) throws IOException
{
oos.defaultWriteObject();
}
}