# URLStreamHandler

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary:[Ctors](https://developer.android.com/reference/java/net/URLStreamHandler#pubctors)\|[Protected Methods](https://developer.android.com/reference/java/net/URLStreamHandler#promethods)\|[Inherited Methods](https://developer.android.com/reference/java/net/URLStreamHandler#inhmethods)  

# URLStreamHandler

`
public


abstract
class
URLStreamHandler
`  
`

extends `[Object](https://developer.android.com/reference/java/lang/Object)`


``


`

|---|---------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | java.net.URLStreamHandler |

<br />

*** ** * ** ***

The abstract class`URLStreamHandler`is the common superclass for all stream protocol handlers. A stream protocol handler knows how to make a connection for a particular protocol type, such as`http`or`https`.

In most cases, an instance of a`URLStreamHandler`subclass is not created directly by an application. Rather, the first time a protocol name is encountered when constructing a`URL`, the appropriate stream protocol handler is automatically loaded.  
**See also:**

- [URL.URL(java.lang.String, java.lang.String, int, java.lang.String)](https://developer.android.com/reference/java/net/URL#URL(java.lang.String,%20java.lang.String,%20int,%20java.lang.String))

## Summary

|                                               ### Public constructors                                               ||
|------------------------------------------------------------------------------------------------------------------|---|
| ` `[URLStreamHandler](https://developer.android.com/reference/java/net/URLStreamHandler#URLStreamHandler())`() ` |

|                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  ### Protected methods                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   ||
|---------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` boolean`                                                                                  | ` `[equals](https://developer.android.com/reference/java/net/URLStreamHandler#equals(java.net.URL,%20java.net.URL))`(`[URL](https://developer.android.com/reference/java/net/URL)` u1, `[URL](https://developer.android.com/reference/java/net/URL)` u2) ` Provides the default equals calculation.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| ` int`                                                                                      | ` `[getDefaultPort](https://developer.android.com/reference/java/net/URLStreamHandler#getDefaultPort())`() ` Returns the default port for a URL parsed by this handler.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| ` `[InetAddress](https://developer.android.com/reference/java/net/InetAddress)              | ` `[getHostAddress](https://developer.android.com/reference/java/net/URLStreamHandler#getHostAddress(java.net.URL))`(`[URL](https://developer.android.com/reference/java/net/URL)` u) ` Get the IP address of our host.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| ` int`                                                                                      | ` `[hashCode](https://developer.android.com/reference/java/net/URLStreamHandler#hashCode(java.net.URL))`(`[URL](https://developer.android.com/reference/java/net/URL)` u) ` Provides the default hash calculation.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| ` boolean`                                                                                  | ` `[hostsEqual](https://developer.android.com/reference/java/net/URLStreamHandler#hostsEqual(java.net.URL,%20java.net.URL))`(`[URL](https://developer.android.com/reference/java/net/URL)` u1, `[URL](https://developer.android.com/reference/java/net/URL)` u2) ` Compares the host components of two URLs.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| ` abstract `[URLConnection](https://developer.android.com/reference/java/net/URLConnection) | ` `[openConnection](https://developer.android.com/reference/java/net/URLStreamHandler#openConnection(java.net.URL))`(`[URL](https://developer.android.com/reference/java/net/URL)` u) ` Opens a connection to the object referenced by the`URL`argument.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| ` `[URLConnection](https://developer.android.com/reference/java/net/URLConnection)          | ` `[openConnection](https://developer.android.com/reference/java/net/URLStreamHandler#openConnection(java.net.URL,%20java.net.Proxy))`(`[URL](https://developer.android.com/reference/java/net/URL)` u, `[Proxy](https://developer.android.com/reference/java/net/Proxy)` p) ` Same as openConnection(URL), except that the connection will be made through the specified proxy; Protocol handlers that do not support proxying will ignore the proxy parameter and make a normal connection.                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| ` void`                                                                                     | ` `[parseURL](https://developer.android.com/reference/java/net/URLStreamHandler#parseURL(java.net.URL,%20java.lang.String,%20int,%20int))`(`[URL](https://developer.android.com/reference/java/net/URL)` u, `[String](https://developer.android.com/reference/java/lang/String)` spec, int start, int limit) ` Parses the string representation of a`URL`into a`URL`object.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| ` boolean`                                                                                  | ` `[sameFile](https://developer.android.com/reference/java/net/URLStreamHandler#sameFile(java.net.URL,%20java.net.URL))`(`[URL](https://developer.android.com/reference/java/net/URL)` u1, `[URL](https://developer.android.com/reference/java/net/URL)` u2) ` Compare two urls to see whether they refer to the same file, i.e., having the same protocol, host, port, and path.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| ` void`                                                                                     | ` `[setURL](https://developer.android.com/reference/java/net/URLStreamHandler#setURL(java.net.URL,%20java.lang.String,%20java.lang.String,%20int,%20java.lang.String,%20java.lang.String))`(`[URL](https://developer.android.com/reference/java/net/URL)` u, `[String](https://developer.android.com/reference/java/lang/String)` protocol, `[String](https://developer.android.com/reference/java/lang/String)` host, int port, `[String](https://developer.android.com/reference/java/lang/String)` file, `[String](https://developer.android.com/reference/java/lang/String)` ref) ` *This method was deprecated in API level 15. Use setURL(URL, String, String, int, String, String, String, String);*                                                                                                                                                                                                                                                 |
| ` void`                                                                                     | ` `[setURL](https://developer.android.com/reference/java/net/URLStreamHandler#setURL(java.net.URL,%20java.lang.String,%20java.lang.String,%20int,%20java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang.String))`(`[URL](https://developer.android.com/reference/java/net/URL)` u, `[String](https://developer.android.com/reference/java/lang/String)` protocol, `[String](https://developer.android.com/reference/java/lang/String)` host, int port, `[String](https://developer.android.com/reference/java/lang/String)` authority, `[String](https://developer.android.com/reference/java/lang/String)` userInfo, `[String](https://developer.android.com/reference/java/lang/String)` path, `[String](https://developer.android.com/reference/java/lang/String)` query, `[String](https://developer.android.com/reference/java/lang/String)` ref) ` Sets the fields of the`URL`argument to the indicated values. |
| ` `[String](https://developer.android.com/reference/java/lang/String)                       | ` `[toExternalForm](https://developer.android.com/reference/java/net/URLStreamHandler#toExternalForm(java.net.URL))`(`[URL](https://developer.android.com/reference/java/net/URL)` u) ` Converts a`URL`of a specific protocol to a`String`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |

| ### Inherited methods |
|-----------------------|---|
| From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                          | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed. | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long))`(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed.                   | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*.                                                                                             | ||

## Public constructors

### URLStreamHandler

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public URLStreamHandler ()
```

<br />

<br />

## Protected methods

### equals

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
protected boolean equals (URL u1, 
                URL u2)
```

Provides the default equals calculation. May be overidden by handlers for other protocols that have different requirements for equals(). This method requires that none of its arguments is null. This is guaranteed by the fact that it is only called by java.net.URL class.

<br />

|            Parameters            ||
|------|----------------------------|
| `u1` | `URL`: a URL object <br /> |
| `u2` | `URL`: a URL object <br /> |

|                                                       Returns                                                       ||
|-----------|----------------------------------------------------------------------------------------------------------|
| `boolean` | `true`if the two urls are considered equal, ie. they refer to the same fragment in the same file. <br /> |

### getDefaultPort

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
protected int getDefaultPort ()
```

Returns the default port for a URL parsed by this handler. This method is meant to be overidden by handlers with default port numbers.

<br />

|                             Returns                              ||
|-------|-----------------------------------------------------------|
| `int` | the default port for a`URL`parsed by this handler. <br /> |

### getHostAddress

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
protected InetAddress getHostAddress (URL u)
```

Get the IP address of our host. An empty host field or a DNS failure will result in a null return.

<br />

|           Parameters            ||
|-----|----------------------------|
| `u` | `URL`: a URL object <br /> |

|                                                               Returns                                                                ||
|-----------------------------------------------------------------------------|---------------------------------------------------------|
| [InetAddress](https://developer.android.com/reference/java/net/InetAddress) | an`InetAddress`representing the host IP address. <br /> |

### hashCode

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
protected int hashCode (URL u)
```

Provides the default hash calculation. May be overidden by handlers for other protocols that have different requirements for hashCode calculation.

<br />

|           Parameters            ||
|-----|----------------------------|
| `u` | `URL`: a URL object <br /> |

|                        Returns                        ||
|-------|------------------------------------------------|
| `int` | an`int`suitable for hash table indexing <br /> |

### hostsEqual

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
protected boolean hostsEqual (URL u1, 
                URL u2)
```

Compares the host components of two URLs.

<br />

|                        Parameters                         ||
|------|-----------------------------------------------------|
| `u1` | `URL`: the URL of the first host to compare <br />  |
| `u2` | `URL`: the URL of the second host to compare <br /> |

|                                 Returns                                 ||
|-----------|--------------------------------------------------------------|
| `boolean` | `true`if and only if they are equal,`false`otherwise. <br /> |

### openConnection

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
protected abstract URLConnection openConnection (URL u)
```

Opens a connection to the object referenced by the`URL`argument. This method should be overridden by a subclass.

If for the handler's protocol (such as HTTP or JAR), there exists a public, specialized URLConnection subclass belonging to one of the following packages or one of their subpackages: java.lang, java.io, java.util, java.net, the connection returned will be of that subclass. For example, for HTTP an HttpURLConnection will be returned, and for JAR a JarURLConnection will be returned.

<br />

|                    Parameters                     ||
|-----|----------------------------------------------|
| `u` | `URL`: the URL that this connects to. <br /> |

|                                                           Returns                                                            ||
|---------------------------------------------------------------------------------|---------------------------------------------|
| [URLConnection](https://developer.android.com/reference/java/net/URLConnection) | a`URLConnection`object for the`URL`. <br /> |

|                                                              Throws                                                              ||
|----------------------------------------------------------------------------|------------------------------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException) | if an I/O error occurs while opening the connection. |

### openConnection

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
protected URLConnection openConnection (URL u, 
                Proxy p)
```

Same as openConnection(URL), except that the connection will be made through the specified proxy; Protocol handlers that do not support proxying will ignore the proxy parameter and make a normal connection. Calling this method preempts the system's default ProxySelector settings.

<br />

|                                                                   Parameters                                                                   ||
|-----|-------------------------------------------------------------------------------------------------------------------------------------------|
| `u` | `URL`: the URL that this connects to. <br />                                                                                              |
| `p` | `Proxy`: the proxy through which the connection will be made. If direct connection is desired, Proxy.NO_PROXY should be specified. <br /> |

|                                                           Returns                                                            ||
|---------------------------------------------------------------------------------|---------------------------------------------|
| [URLConnection](https://developer.android.com/reference/java/net/URLConnection) | a`URLConnection`object for the`URL`. <br /> |

|                                                                                           Throws                                                                                            ||
|------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException)                                       | if an I/O error occurs while opening the connection.                      |
| [IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException)           | if either u or p is null, or p has the wrong type.                        |
| [UnsupportedOperationException](https://developer.android.com/reference/java/lang/UnsupportedOperationException) | if the subclass that implements the protocol doesn't support this method. |

### parseURL

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
protected void parseURL (URL u, 
                String spec, 
                int start, 
                int limit)
```

Parses the string representation of a`URL`into a`URL`object.

If there is any inherited context, then it has already been copied into the`URL`argument.

The`parseURL`method of`URLStreamHandler`parses the string representation as if it were an`http`specification. Most URL protocol families have a similar parsing. A stream protocol handler for a protocol that has a different syntax must override this routine.

<br />

|                                                                                                  Parameters                                                                                                   ||
|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `u`     | `URL`: the`URL`to receive the result of parsing the spec. <br />                                                                                                                                     |
| `spec`  | `String`: the`String`representing the URL that must be parsed. <br />                                                                                                                                |
| `start` | `int`: the character index at which to begin parsing. This is just past the '`:`' (if there is one) that specifies the determination of the protocol name. <br />                                    |
| `limit` | `int`: the character position to stop parsing at. This is the end of the string or the position of the "`#`" character, if present. All information after the sharp sign indicates an anchor. <br /> |

### sameFile

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
protected boolean sameFile (URL u1, 
                URL u2)
```

Compare two urls to see whether they refer to the same file, i.e., having the same protocol, host, port, and path. This method requires that none of its arguments is null. This is guaranteed by the fact that it is only called indirectly by java.net.URL class.

<br />

|            Parameters            ||
|------|----------------------------|
| `u1` | `URL`: a URL object <br /> |
| `u2` | `URL`: a URL object <br /> |

|                          Returns                           ||
|-----------|-------------------------------------------------|
| `boolean` | true if u1 and u2 refer to the same file <br /> |

### setURL

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Deprecated in[API level 15](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
protected void setURL (URL u, 
                String protocol, 
                String host, 
                int port, 
                String file, 
                String ref)
```

<br />

**This method was deprecated in API level 15.**   
Use setURL(URL, String, String, int, String, String, String, String);

Sets the fields of the`URL`argument to the indicated values. Only classes derived from URLStreamHandler are able to use this method to set the values of the URL fields.

<br />

|                                    Parameters                                    ||
|------------|----------------------------------------------------------------------|
| `u`        | `URL`: the URL to modify. <br />                                     |
| `protocol` | `String`: the protocol name. This value is ignored since 1.2. <br /> |
| `host`     | `String`: the remote host value for the URL. <br />                  |
| `port`     | `int`: the port on the remote machine. <br />                        |
| `file`     | `String`: the file. <br />                                           |
| `ref`      | `String`: the reference. <br />                                      |

|                                                                         Throws                                                                          ||
|------------------------------------------------------------------------------------------|---------------------------------------------------------------|
| [SecurityException](https://developer.android.com/reference/java/lang/SecurityException) | if the protocol handler of the URL is different from this one |

### setURL

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
protected void setURL (URL u, 
                String protocol, 
                String host, 
                int port, 
                String authority, 
                String userInfo, 
                String path, 
                String query, 
                String ref)
```

Sets the fields of the`URL`argument to the indicated values. Only classes derived from URLStreamHandler are able to use this method to set the values of the URL fields.

<br />

|                            Parameters                            ||
|-------------|-----------------------------------------------------|
| `u`         | `URL`: the URL to modify. <br />                    |
| `protocol`  | `String`: the protocol name. <br />                 |
| `host`      | `String`: the remote host value for the URL. <br /> |
| `port`      | `int`: the port on the remote machine. <br />       |
| `authority` | `String`: the authority part for the URL. <br />    |
| `userInfo`  | `String`: the userInfo part of the URL. <br />      |
| `path`      | `String`: the path component of the URL. <br />     |
| `query`     | `String`: the query part for the URL. <br />        |
| `ref`       | `String`: the reference. <br />                     |

|                                                                         Throws                                                                          ||
|------------------------------------------------------------------------------------------|---------------------------------------------------------------|
| [SecurityException](https://developer.android.com/reference/java/lang/SecurityException) | if the protocol handler of the URL is different from this one |

### toExternalForm

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
protected String toExternalForm (URL u)
```

Converts a`URL`of a specific protocol to a`String`.

<br />

|         Parameters          ||
|-----|------------------------|
| `u` | `URL`: the URL. <br /> |

|                                                         Returns                                                         ||
|--------------------------------------------------------------------|-----------------------------------------------------|
| [String](https://developer.android.com/reference/java/lang/String) | a string representation of the`URL`argument. <br /> |