Added in API level 1

JarURLConnection

abstract class JarURLConnection : URLConnection

A URL Connection to a Java ARchive (JAR) file or an entry in a JAR file.

The syntax of a JAR URL is:

jar:<url>!/{entry}
  

for example:

jar:http://www.foo.com/bar/baz.jar!/COM/foo/Quux.class

Jar URLs should be used to refer to a JAR file or entries in a JAR file. The example above is a JAR URL which refers to a JAR entry. If the entry name is omitted, the URL refers to the whole JAR file: jar:http://www.foo.com/bar/baz.jar!/

Users should cast the generic URLConnection to a JarURLConnection when they know that the URL they created is a JAR URL, and they need JAR-specific functionality. For example:

URL url = new URL("jar:file:/home/duke/duke.jar!/");
  JarURLConnection jarConnection = (JarURLConnection)url.openConnection();
  Manifest manifest = jarConnection.getManifest();
  

JarURLConnection instances can only be used to read from JAR files. It is not possible to get a java.io.OutputStream to modify or write to the underlying JAR file using this class.

Examples:

A Jar entry
jar:http://www.foo.com/bar/baz.jar!/COM/foo/Quux.class
A Jar file
jar:http://www.foo.com/bar/baz.jar!/
A Jar directory
jar:http://www.foo.com/bar/baz.jar!/COM/foo/

!/ is referred to as the separator.

When constructing a JAR url via new URL(context, spec), the following rules apply:

  • if there is no context URL and the specification passed to the URL constructor doesn't contain a separator, the URL is considered to refer to a JarFile.
  • if there is a context URL, the context URL is assumed to refer to a JAR file or a Jar directory.
  • if the specification begins with a '/', the Jar directory is ignored, and the spec is considered to be at the root of the Jar file.

    Examples:

    context: jar:http://www.foo.com/bar/jar.jar!/, spec:baz/entry.txt
    url:jar:http://www.foo.com/bar/jar.jar!/baz/entry.txt
    context: jar:http://www.foo.com/bar/jar.jar!/baz, spec:entry.txt
    url:jar:http://www.foo.com/bar/jar.jar!/baz/entry.txt
    context: jar:http://www.foo.com/bar/jar.jar!/baz, spec:/entry.txt
    url:jar:http://www.foo.com/bar/jar.jar!/entry.txt

Summary

Protected constructors

Creates the new JarURLConnection to the specified URL.

Public methods
open Attributes!

Return the Attributes object for this connection if the URL for it points to a JAR file entry, null otherwise.

open Array<Certificate!>!

Return the Certificate object for this connection if the URL for it points to a JAR file entry, null otherwise.

open String!

Return the entry name for this connection.

open JarEntry!

Return the JAR entry object for this connection, if any.

abstract JarFile!

Return the JAR file for this connection.

open URL!

Returns the URL for the Jar file for this connection.

open Attributes!

Returns the main Attributes for the JAR file for this connection.

open Manifest!

Returns the Manifest for this connection, or null if none.

Inherited functions
Unit addRequestProperty(key: String!, value: String!)

Adds a general request property specified by a key-value pair. This method will not overwrite existing values associated with the same key.

Unit connect()

Opens a communications link to the resource referenced by this URL, if such a connection has not already been established.

If the connect method is called when the connection has already been opened (indicated by the connected field having the value true), the call is ignored.

URLConnection objects go through two phases: first they are created, then they are connected. After being created, and before being connected, various options can be specified (e.g., doInput and UseCaches). After connecting, it is an error to try to set them. Operations that depend on being connected, like getContentLength, will implicitly perform the connection, if necessary.

Boolean getAllowUserInteraction()

Returns the value of the allowUserInteraction field for this object.

Int getConnectTimeout()

Returns setting for connect timeout.

0 return implies that the option is disabled (i.e., timeout of infinity).

Any! getContent()

Retrieves the contents of this URL connection.

This method first determines the content type of the object by calling the getContentType method. If this is the first time that the application has seen that specific content type, a content handler for that content type is created:

  1. If the application has set up a content handler factory instance using the setContentHandlerFactory method, the createContentHandler method of that instance is called with the content type as an argument; the result is a content handler for that content type.
  2. If no content handler factory has yet been set up, or if the factory's createContentHandler method returns null, then the application loads the class named:
    sun.net.www.content.<<i>contentType</i>>
          
    where <contentType> is formed by taking the content-type string, replacing all slash characters with a period ('.'), and all other non-alphanumeric characters with the underscore character '_'. The alphanumeric characters are specifically the 26 uppercase ASCII letters 'A' through 'Z', the 26 lowercase ASCII letters 'a' through 'z', and the 10 ASCII digits '0' through '9'. If the specified class does not exist, or is not a subclass of ContentHandler, then an UnknownServiceException is thrown.

Any! getContent(classes: Array<Class<Any!>!>!)

Retrieves the contents of this URL connection.

String! getContentEncoding()

Returns the value of the content-encoding header field.

Int getContentLength()

Returns the value of the content-length header field.

Note: getContentLengthLong() should be preferred over this method, since it returns a long instead and is therefore more portable.

Long getContentLengthLong()

Returns the value of the content-length header field as a long.

String! getContentType()

Returns the value of the content-type header field.

Long getDate()

Returns the value of the date header field.

Boolean getDefaultAllowUserInteraction()

Returns the default value of the allowUserInteraction field.

Ths default is "sticky", being a part of the static state of all URLConnections. This flag applies to the next, and all following URLConnections that are created.

String! getDefaultRequestProperty(key: String!)

Returns the value of the default request property. Default request properties are set for every connection.

Boolean getDefaultUseCaches()

Returns the default value of a URLConnection's useCaches flag.

Ths default is "sticky", being a part of the static state of all URLConnections. This flag applies to the next, and all following URLConnections that are created.

Boolean getDoInput()

Returns the value of this URLConnection's doInput flag.

Boolean getDoOutput()

Returns the value of this URLConnection's doOutput flag.

Long getExpiration()

Returns the value of the expires header field.

FileNameMap! getFileNameMap()

Returns a FileNameMap implementation suitable for guessing a content type based on a URL's "file" component.

String! getHeaderField(n: Int)

Returns the value for the nth header field. It returns null if there are fewer than n+1fields.

This method can be used in conjunction with the getHeaderFieldKey method to iterate through all the headers in the message.

String! getHeaderField(name: String!)

Returns the value of the named header field.

If called on a connection that sets the same header multiple times with possibly different values, only the last value is returned.

Long getHeaderFieldDate(name: String!, Default: Long)

Returns the value of the named field parsed as date. The result is the number of milliseconds since January 1, 1970 GMT represented by the named field.

This form of getHeaderField exists because some connection types (e.g., http-ng) have pre-parsed headers. Classes for that connection type can override this method and short-circuit the parsing.

Int getHeaderFieldInt(name: String!, Default: Int)

Returns the value of the named field parsed as a number.

This form of getHeaderField exists because some connection types (e.g., http-ng) have pre-parsed headers. Classes for that connection type can override this method and short-circuit the parsing.

String! getHeaderFieldKey(n: Int)

Returns the key for the nth header field. It returns null if there are fewer than n+1 fields.

Long getHeaderFieldLong(name: String!, Default: Long)

Returns the value of the named field parsed as a number.

This form of getHeaderField exists because some connection types (e.g., http-ng) have pre-parsed headers. Classes for that connection type can override this method and short-circuit the parsing.

MutableMap<String!, MutableList<String!>!>! getHeaderFields()

Returns an unmodifiable Map of the header fields. The Map keys are Strings that represent the response-header field names. Each Map value is an unmodifiable List of Strings that represents the corresponding field values.

Long getIfModifiedSince()

Returns the value of this object's ifModifiedSince field.

InputStream! getInputStream()

Returns an input stream that reads from this open connection. A SocketTimeoutException can be thrown when reading from the returned input stream if the read timeout expires before data is available for read.

Long getLastModified()

Returns the value of the last-modified header field. The result is the number of milliseconds since January 1, 1970 GMT.

OutputStream! getOutputStream()

Returns an output stream that writes to this connection.

Permission! getPermission()

Returns a permission object representing the permission necessary to make the connection represented by this object. This method returns null if no permission is required to make the connection. By default, this method returns java.security.AllPermission. Subclasses should override this method and return the permission that best represents the permission required to make a a connection to the URL. For example, a URLConnection representing a file: URL would return a java.io.FilePermission object.

The permission returned may dependent upon the state of the connection. For example, the permission before connecting may be different from that after connecting. For example, an HTTP sever, say foo.com, may redirect the connection to a different host, say bar.com. Before connecting the permission returned by the connection will represent the permission needed to connect to foo.com, while the permission returned after connecting will be to bar.com.

Permissions are generally used for two purposes: to protect caches of objects obtained through URLConnections, and to check the right of a recipient to learn about a particular URL. In the first case, the permission should be obtained after the object has been obtained. For example, in an HTTP connection, this will represent the permission to connect to the host from which the data was ultimately fetched. In the second case, the permission should be obtained and tested before connecting.

Int getReadTimeout()

Returns setting for read timeout. 0 return implies that the option is disabled (i.e., timeout of infinity).

MutableMap<String!, MutableList<String!>!>! getRequestProperties()

Returns an unmodifiable Map of general request properties for this connection. The Map keys are Strings that represent the request-header field names. Each Map value is a unmodifiable List of Strings that represents the corresponding field values.

String! getRequestProperty(key: String!)

Returns the value of the named general request property for this connection.

URL! getURL()

Returns the value of this URLConnection's URL field.

Boolean getUseCaches()

Returns the value of this URLConnection's useCaches field.

String! guessContentTypeFromName(fname: String!)

Tries to determine the content type of an object, based on the specified "file" component of a URL. This is a convenience method that can be used by subclasses that override the getContentType method.

String! guessContentTypeFromStream(is: InputStream!)

Tries to determine the type of an input stream based on the characters at the beginning of the input stream. This method can be used by subclasses that override the getContentType method.

Ideally, this routine would not be needed. But many http servers return the incorrect content type; in addition, there are many nonstandard extensions. Direct inspection of the bytes to determine the content type is often more accurate than believing the content type claimed by the http server.

Unit setAllowUserInteraction(allowuserinteraction: Boolean)

Set the value of the allowUserInteraction field of this URLConnection.

Unit setConnectTimeout(timeout: Int)

Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced by this URLConnection. If the timeout expires before the connection can be established, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.

Some non-standard implementation of this method may ignore the specified timeout. To see the connect timeout set, please call getConnectTimeout().

Warning: If the hostname resolves to multiple IP addresses, Android's default implementation of HttpURLConnection will try each in RFC 3484 order. If connecting to each of these addresses fails, multiple timeouts will elapse before the connect attempt throws an exception. Host names that support both IPv6 and IPv4 always have at least 2 IP addresses.

Unit setContentHandlerFactory(fac: ContentHandlerFactory!)

Sets the ContentHandlerFactory of an application. It can be called at most once by an application.

The ContentHandlerFactory instance is used to construct a content handler from a content type

If there is a security manager, this method first calls the security manager's checkSetFactory method to ensure the operation is allowed. This could result in a SecurityException.

Unit setDefaultAllowUserInteraction(defaultallowuserinteraction: Boolean)

Sets the default value of the allowUserInteraction field for all future URLConnection objects to the specified value.

Unit setDefaultRequestProperty(key: String!, value: String!)

Sets the default value of a general request property. When a URLConnection is created, it is initialized with these properties.

Unit setDefaultUseCaches(defaultusecaches: Boolean)

Sets the default value of the useCaches field to the specified value.

Unit setDoInput(doinput: Boolean)

Sets the value of the doInput field for this URLConnection to the specified value.

A URL connection can be used for input and/or output. Set the DoInput flag to true if you intend to use the URL connection for input, false if not. The default is true.

Unit setDoOutput(dooutput: Boolean)

Sets the value of the doOutput field for this URLConnection to the specified value.

A URL connection can be used for input and/or output. Set the DoOutput flag to true if you intend to use the URL connection for output, false if not. The default is false.

Unit setFileNameMap(map: FileNameMap!)

Sets the FileNameMap.

If there is a security manager, this method first calls the security manager's checkSetFactory method to ensure the operation is allowed. This could result in a SecurityException.

Unit setIfModifiedSince(ifmodifiedsince: Long)

Sets the value of the ifModifiedSince field of this URLConnection to the specified value.

Unit setReadTimeout(timeout: Int)

Sets the read timeout to a specified timeout, in milliseconds. A non-zero value specifies the timeout when reading from Input stream when a connection is established to a resource. If the timeout expires before there is data available for read, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.

Some non-standard implementation of this method ignores the specified timeout. To see the read timeout set, please call getReadTimeout().

Unit setRequestProperty(key: String!, value: String!)

Sets the general request property. If a property with the key already exists, overwrite its value with the new value.

NOTE: HTTP requires all request properties which can legally have multiple instances with the same key to use a comma-separated list syntax which enables multiple properties to be appended into a single property.

Unit setUseCaches(usecaches: Boolean)

Sets the value of the useCaches field of this URLConnection to the specified value.

Some protocols do caching of documents. Occasionally, it is important to be able to "tunnel through" and ignore the caches (e.g., the "reload" button in a browser). If the UseCaches flag on a connection is true, the connection is allowed to use whatever caches it can. If false, caches are to be ignored. The default value comes from DefaultUseCaches, which defaults to true.

String toString()

Returns a String representation of this URL connection.

Properties
URLConnection!

The connection to the JAR file URL, if the connection has been initiated.

Inherited properties
Boolean allowUserInteraction

If true, this URL is being examined in a context in which it makes sense to allow user interactions such as popping up an authentication dialog. If false, then no user interaction is allowed.

The value of this field can be set by the setAllowUserInteraction method. Its value is returned by the getAllowUserInteraction method. Its default value is the value of the argument in the last invocation of the setDefaultAllowUserInteraction method.

Boolean connected

If false, this connection object has not created a communications link to the specified URL. If true, the communications link has been established.

Boolean doInput

This variable is set by the setDoInput method. Its value is returned by the getDoInput method.

A URL connection can be used for input and/or output. Setting the doInput flag to true indicates that the application intends to read data from the URL connection.

The default value of this field is true.

Boolean doOutput

This variable is set by the setDoOutput method. Its value is returned by the getDoOutput method.

A URL connection can be used for input and/or output. Setting the doOutput flag to true indicates that the application intends to write data to the URL connection.

The default value of this field is false.

Long ifModifiedSince

Some protocols support skipping the fetching of the object unless the object has been modified more recently than a certain time.

A nonzero value gives a time as the number of milliseconds since January 1, 1970, GMT. The object is fetched only if it has been modified more recently than that time.

This variable is set by the setIfModifiedSince method. Its value is returned by the getIfModifiedSince method.

The default value of this field is 0, indicating that the fetching must always occur.

URL! url

The URL represents the remote object on the World Wide Web to which this connection is opened.

The value of this field can be accessed by the getURL method.

The default value of this variable is the value of the URL argument in the URLConnection constructor.

Boolean useCaches

If true, the protocol is allowed to use caching whenever it can. If false, the protocol must always try to get a fresh copy of the object.

This field is set by the setUseCaches method. Its value is returned by the getUseCaches method.

Its default value is the value given in the last invocation of the setDefaultUseCaches method.

Protected constructors

JarURLConnection

Added in API level 1
protected JarURLConnection(url: URL!)

Creates the new JarURLConnection to the specified URL.

Parameters
url URL!: the URL
Exceptions
java.net.MalformedURLException if no legal protocol could be found in a specification string or the string could not be parsed.

Public methods

getAttributes

Added in API level 1
open fun getAttributes(): Attributes!

Return the Attributes object for this connection if the URL for it points to a JAR file entry, null otherwise.

Return
Attributes! the Attributes object for this connection if the URL for it points to a JAR file entry, null otherwise.
Exceptions
java.io.IOException if getting the JAR entry causes an IOException to be thrown.

See Also

getCertificates

Added in API level 1
open fun getCertificates(): Array<Certificate!>!

Return the Certificate object for this connection if the URL for it points to a JAR file entry, null otherwise. This method can only be called once the connection has been completely verified by reading from the input stream until the end of the stream has been reached. Otherwise, this method will return null

Return
Array<Certificate!>! the Certificate object for this connection if the URL for it points to a JAR file entry, null otherwise.
Exceptions
java.io.IOException if getting the JAR entry causes an IOException to be thrown.

See Also

getEntryName

Added in API level 1
open fun getEntryName(): String!

Return the entry name for this connection. This method returns null if the JAR file URL corresponding to this connection points to a JAR file and not a JAR file entry.

Return
String! the entry name for this connection, if any.

getJarEntry

Added in API level 1
open fun getJarEntry(): JarEntry!

Return the JAR entry object for this connection, if any. This method returns null if the JAR file URL corresponding to this connection points to a JAR file and not a JAR file entry.

Return
JarEntry! the JAR entry object for this connection, or null if the JAR URL for this connection points to a JAR file.
Exceptions
java.io.IOException if getting the JAR file for this connection causes an IOException to be thrown.

getJarFile

Added in API level 1
abstract fun getJarFile(): JarFile!

Return the JAR file for this connection.

Return
JarFile! the JAR file for this connection. If the connection is a connection to an entry of a JAR file, the JAR file object is returned
Exceptions
java.io.IOException if an IOException occurs while trying to connect to the JAR file for this connection.

See Also

getJarFileURL

Added in API level 1
open fun getJarFileURL(): URL!

Returns the URL for the Jar file for this connection.

Return
URL! the URL for the Jar file for this connection.

getMainAttributes

Added in API level 1
open fun getMainAttributes(): Attributes!

Returns the main Attributes for the JAR file for this connection.

Return
Attributes! the main Attributes for the JAR file for this connection.
Exceptions
java.io.IOException if getting the manifest causes an IOException to be thrown.

getManifest

Added in API level 1
open fun getManifest(): Manifest!

Returns the Manifest for this connection, or null if none.

Return
Manifest! the manifest object corresponding to the JAR file object for this connection.
Exceptions
java.io.IOException if getting the JAR file for this connection causes an IOException to be thrown.

See Also

Properties

jarFileURLConnection

Added in API level 1
protected var jarFileURLConnection: URLConnection!

The connection to the JAR file URL, if the connection has been initiated. This should be set by connect.