aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/net/URL.java
diff options
context:
space:
mode:
Diffstat (limited to 'libjava/java/net/URL.java')
-rw-r--r--libjava/java/net/URL.java248
1 files changed, 213 insertions, 35 deletions
diff --git a/libjava/java/net/URL.java b/libjava/java/net/URL.java
index a3e9d784cef..b485c51231d 100644
--- a/libjava/java/net/URL.java
+++ b/libjava/java/net/URL.java
@@ -28,9 +28,12 @@ import java.util.StringTokenizer;
public final class URL implements Serializable
{
private String protocol;
+ private String authority;
+ private String userInfo;
private String host;
private int port = -1; // Initialize for constructor using context.
private String file;
+ private String query;
private String ref;
private int hashCode = 0;
transient private URLStreamHandler handler;
@@ -39,19 +42,53 @@ public final class URL implements Serializable
private static final long serialVersionUID = -7627629688361524110L;
+ /**
+ * Creates an URL object from the given arguments
+ *
+ * @param protocol The protocol of the URL
+ * @param host The host of the URL
+ * @param port The port of the URL
+ * @param file The file of the URL
+ *
+ * @exception MalformedURLException If an error occurs
+ */
public URL(String protocol, String host, int port, String file)
throws MalformedURLException
{
this(protocol, host, port, file, null);
}
+ /**
+ * Creates an URL object from the given arguments
+ *
+ * @param protocol The protocol of the URL
+ * @param host The host of the URL
+ * @param file The file of the URL
+ *
+ * @exception MalformedURLException If an error occurs
+ */
public URL(String protocol, String host, String file)
throws MalformedURLException
{
this(protocol, host, -1, file, null);
}
- // JDK1.2
+ /**
+ * Creates an URL object from the given arguments
+ *
+ * @param protocol The protocol of the URL
+ * @param host The host of the URL
+ * @param port The port of the URL
+ * @param file The file of the URL
+ * @param handler The stream handler for the URL
+ *
+ * @exception MalformedURLException If an error occurs
+ * @exception SecurityException If a security manager exists and its
+ * checkPermission method doesn't allow specifying a stream handler
+ * explicitly
+ *
+ * @since 1.2
+ */
public URL(String protocol, String host, int port, String file,
URLStreamHandler handler) throws MalformedURLException
{
@@ -61,14 +98,9 @@ public final class URL implements Serializable
if (handler != null)
{
- // TODO12: Need SecurityManager.checkPermission and
- // TODO12: java.net.NetPermission from JDK 1.2 to be implemented.
- // Throw an exception if an extant security mgr precludes
- // specifying a StreamHandler.
- //
- // SecurityManager s = System.getSecurityManager();
- // if (s != null)
- // s.checkPermission(NetPermission("specifyStreamHandler"));
+ SecurityManager s = System.getSecurityManager ();
+ if (s != null)
+ s.checkPermission (new NetPermission ("specifyStreamHandler"));
this.handler = handler;
}
@@ -76,11 +108,14 @@ public final class URL implements Serializable
this.handler = setURLStreamHandler(protocol);
if (this.handler == null)
- throw new MalformedURLException("Protocol handler not found: " + protocol);
+ throw new MalformedURLException (
+ "Protocol handler not found: " + protocol);
this.host = host;
-
this.port = port;
+ this.userInfo = null;
+ this.authority = null;
+ this.query = null;
int hashAt = file.indexOf('#');
if (hashAt < 0)
@@ -96,17 +131,45 @@ public final class URL implements Serializable
hashCode = hashCode(); // Used for serialization.
}
+ /**
+ * Creates an URL object from the given arguments
+ *
+ * @param spec The string to parse an URL
+ *
+ * @exception MalformedURLException If an error occurs
+ */
public URL(String spec) throws MalformedURLException
{
this((URL) null, spec, (URLStreamHandler) null);
}
+ /**
+ * Creates an URL object from the given arguments
+ *
+ * @param context The context on which to parse the specification
+ * @param spec The string to parse an URL
+ *
+ * @exception MalformedURLException If an error occurs
+ */
public URL(URL context, String spec) throws MalformedURLException
{
this(context, spec, (URLStreamHandler) null);
}
- // JDK1.2
+ /**
+ * Creates an URL from given arguments
+ *
+ * @param context The context in which to parse the specification
+ * @param spec The string to parse as an URL
+ * @param handler The stream handler for the URL
+ *
+ * @exception MalformedURLException If an error occurs
+ * @exception SecurityException If a security manager exists and its
+ * checkPermission method doesn't allow specifying a stream handler
+ * explicitly
+ *
+ * @since 1.2
+ */
public URL(URL context, String spec, URLStreamHandler handler)
throws MalformedURLException
{
@@ -142,6 +205,9 @@ public final class URL implements Serializable
host = context.host;
port = context.port;
file = context.file;
+ userInfo = context.userInfo;
+ authority = context.authority;
+ query = context.query;
}
}
else if (context != null)
@@ -153,6 +219,9 @@ public final class URL implements Serializable
host = context.host;
port = context.port;
file = context.file;
+ userInfo = context.userInfo;
+ authority = context.authority;
+ query = context.query;
}
else // Protocol NOT specified in spec. and no context available.
throw new
@@ -160,14 +229,9 @@ public final class URL implements Serializable
if (handler != null)
{
- // TODO12: Need SecurityManager.checkPermission and
- // TODO12: java.net.NetPermission from JDK 1.2 to be implemented.
- // Throw an exception if an extant security mgr precludes
- // specifying a StreamHandler.
- //
- // SecurityManager s = System.getSecurityManager();
- // if (s != null)
- // s.checkPermission(NetPermission("specifyStreamHandler"));
+ SecurityManager s = System.getSecurityManager ();
+ if (s != null)
+ s.checkPermission (new NetPermission ("specifyStreamHandler"));
this.handler = handler;
}
@@ -175,7 +239,8 @@ public final class URL implements Serializable
this.handler = setURLStreamHandler(protocol);
if (this.handler == null)
- throw new MalformedURLException("Protocol handler not found: " + protocol);
+ throw new MalformedURLException("Protocol handler not found: "
+ + protocol);
// JDK 1.2 doc for parseURL specifically states that any '#' ref
// is to be excluded by passing the 'limit' as the indexOf the '#'
@@ -195,46 +260,86 @@ public final class URL implements Serializable
return false;
URL uObj = (URL) obj;
-
- // This comparison is very conservative. It assumes that any
- // field can be null.
- 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))));
+
+ return handler.equals (this, uObj);
}
+ /**
+ * Gets the contents of this URL
+ *
+ * @exception IOException If an error occurs
+ *
+ * @since 1.3
+ */
public final Object getContent() throws IOException
{
return openConnection().getContent();
}
+ /**
+ * Gets the contents of this URL
+ *
+ * @exception IOException If an error occurs
+ */
+ public final Object getContent (Class[] classes) throws IOException
+ {
+ // FIXME: implement this
+ return getContent();
+ }
+
public String getFile()
{
return file;
}
+ /**
+ * Returns the path of the URL
+ *
+ * @since 1.3
+ */
public String getPath()
{
int quest = file.indexOf('?');
return quest < 0 ? file : file.substring(0, quest);
}
+ /**
+ * Returns the authority of the URL
+ *
+ * @since 1.3
+ */
+ public String getAuthority()
+ {
+ return authority;
+ }
+
+ /**
+ * Returns the host of the URL
+ */
public String getHost()
{
return host;
}
+ /**
+ * Returns of port of the URL
+ */
public int getPort()
{
return port;
}
+ /**
+ * Returns the default port of the URL
+ */
+ public int getDefaultPort()
+ {
+ return 0;
+ }
+
+ /**
+ * Returns the protocol of the URL
+ */
public String getProtocol()
{
return protocol;
@@ -245,6 +350,23 @@ public final class URL implements Serializable
return ref;
}
+ /**
+ * Returns the user information of the URL
+ */
+ public String getUserInfo ()
+ {
+ int at = host.indexOf('@');
+ return at < 0 ? null : host.substring(0, at);
+ }
+
+ /**
+ * Returns the query of the URL
+ */
+ public String getQuery ()
+ {
+ return query;
+ }
+
public int hashCode()
{
// JCL book says this is computed using (only) the hashcodes of the
@@ -264,15 +386,26 @@ public final class URL implements Serializable
if (hashCode != 0)
return hashCode; // Use cached value if available.
else
- return (protocol.hashCode() + ((host == null) ? 0 : host.hashCode()) +
- port + file.hashCode());
+ return handler.hashCode (this);
}
+ /**
+ * Returns a URLConnection object that represents a connection to the remote
+ * object referred to by the URL
+ *
+ * @exception IOException If an error occurs
+ */
public URLConnection openConnection() throws IOException
{
return handler.openConnection(this);
}
+ /**
+ * Opens a connection to this URL and returns an InputStream for reading
+ * from that connection
+ *
+ * @exception IOException If an error occurs
+ */
public final InputStream openStream() throws IOException
{
return openConnection().getInputStream();
@@ -283,6 +416,11 @@ public final class URL implements Serializable
return handler.sameFile(this, other);
}
+ /**
+ * Sets the specified fields of the URL. This is not a public method so
+ * that only URLStreamHandlers can modify URL fields. URLs are otherwise
+ * constant
+ */
protected void set(String protocol, String host, int port, String file,
String ref)
{
@@ -292,13 +430,53 @@ public final class URL implements Serializable
// be aware of this.
this.handler = setURLStreamHandler(protocol);
this.protocol = protocol;
+ this.authority = null;
+ this.userInfo = null;
this.port = port;
this.host = host;
this.file = file;
+ this.query = null;
+ this.ref = ref;
+ hashCode = hashCode(); // Used for serialization.
+ }
+
+ /**
+ * Sets the specified fields of the URL. This is not a public method so
+ * that only URLStreamHandlers can modify URL fields. URLs are otherwise
+ * constant
+ *
+ * @since 1.3
+ */
+ protected void set(String protocol, String host, int port,
+ String authority, String userInfo,
+ String path, String query, String ref)
+ {
+ // TBD: Theoretically, a poorly written StreamHandler could pass an
+ // invalid protocol. It will cause the handler to be set to null
+ // thus overriding a valid handler. Callers of this method should
+ // be aware of this.
+ this.handler = setURLStreamHandler(protocol);
+ this.protocol = protocol;
+ if (userInfo == null)
+ this.host = host;
+ else
+ this.host = userInfo + "@" + host;
+ this.port = port;
+ if (query == null)
+ this.file = path;
+ else
+ this.file = path + "?" + query;
this.ref = ref;
hashCode = hashCode(); // Used for serialization.
}
+ /**
+ * Sets an application's URLStreamHandlerFactory
+ *
+ * @exception Error If the application has already set a factory
+ * @exception SecurityException If a security manager exists and its
+ * checkSetFactory method doesn't allow the operation
+ */
public static synchronized void
setURLStreamHandlerFactory(URLStreamHandlerFactory fac)
{