Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/net/JarURLConnection.java
38829 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.net;2627import java.io.IOException;28import java.util.jar.JarFile;29import java.util.jar.JarEntry;30import java.util.jar.Attributes;31import java.util.jar.Manifest;32import java.security.Permission;33import sun.net.www.ParseUtil;3435/**36* A URL Connection to a Java ARchive (JAR) file or an entry in a JAR37* file.38*39* <p>The syntax of a JAR URL is:40*41* <pre>42* jar:<url>!/{entry}43* </pre>44*45* <p>for example:46*47* <p>{@code jar:http://www.foo.com/bar/baz.jar!/COM/foo/Quux.class}48*49* <p>Jar URLs should be used to refer to a JAR file or entries in50* a JAR file. The example above is a JAR URL which refers to a JAR51* entry. If the entry name is omitted, the URL refers to the whole52* JAR file:53*54* {@code jar:http://www.foo.com/bar/baz.jar!/}55*56* <p>Users should cast the generic URLConnection to a57* JarURLConnection when they know that the URL they created is a JAR58* URL, and they need JAR-specific functionality. For example:59*60* <pre>61* URL url = new URL("jar:file:/home/duke/duke.jar!/");62* JarURLConnection jarConnection = (JarURLConnection)url.openConnection();63* Manifest manifest = jarConnection.getManifest();64* </pre>65*66* <p>JarURLConnection instances can only be used to read from JAR files.67* It is not possible to get a {@link java.io.OutputStream} to modify or write68* to the underlying JAR file using this class.69* <p>Examples:70*71* <dl>72*73* <dt>A Jar entry74* <dd>{@code jar:http://www.foo.com/bar/baz.jar!/COM/foo/Quux.class}75*76* <dt>A Jar file77* <dd>{@code jar:http://www.foo.com/bar/baz.jar!/}78*79* <dt>A Jar directory80* <dd>{@code jar:http://www.foo.com/bar/baz.jar!/COM/foo/}81*82* </dl>83*84* <p>{@code !/} is referred to as the <em>separator</em>.85*86* <p>When constructing a JAR url via {@code new URL(context, spec)},87* the following rules apply:88*89* <ul>90*91* <li>if there is no context URL and the specification passed to the92* URL constructor doesn't contain a separator, the URL is considered93* to refer to a JarFile.94*95* <li>if there is a context URL, the context URL is assumed to refer96* to a JAR file or a Jar directory.97*98* <li>if the specification begins with a '/', the Jar directory is99* ignored, and the spec is considered to be at the root of the Jar100* file.101*102* <p>Examples:103*104* <dl>105*106* <dt>context: <b>jar:http://www.foo.com/bar/jar.jar!/</b>,107* spec:<b>baz/entry.txt</b>108*109* <dd>url:<b>jar:http://www.foo.com/bar/jar.jar!/baz/entry.txt</b>110*111* <dt>context: <b>jar:http://www.foo.com/bar/jar.jar!/baz</b>,112* spec:<b>entry.txt</b>113*114* <dd>url:<b>jar:http://www.foo.com/bar/jar.jar!/baz/entry.txt</b>115*116* <dt>context: <b>jar:http://www.foo.com/bar/jar.jar!/baz</b>,117* spec:<b>/entry.txt</b>118*119* <dd>url:<b>jar:http://www.foo.com/bar/jar.jar!/entry.txt</b>120*121* </dl>122*123* </ul>124*125* @see java.net.URL126* @see java.net.URLConnection127*128* @see java.util.jar.JarFile129* @see java.util.jar.JarInputStream130* @see java.util.jar.Manifest131* @see java.util.zip.ZipEntry132*133* @author Benjamin Renaud134* @since 1.2135*/136public abstract class JarURLConnection extends URLConnection {137138private URL jarFileURL;139private String entryName;140141/**142* The connection to the JAR file URL, if the connection has been143* initiated. This should be set by connect.144*/145protected URLConnection jarFileURLConnection;146147/**148* Creates the new JarURLConnection to the specified URL.149* @param url the URL150* @throws MalformedURLException if no legal protocol151* could be found in a specification string or the152* string could not be parsed.153*/154155protected JarURLConnection(URL url) throws MalformedURLException {156super(url);157parseSpecs(url);158}159160/* get the specs for a given url out of the cache, and compute and161* cache them if they're not there.162*/163private void parseSpecs(URL url) throws MalformedURLException {164String spec = url.getFile();165166int separator = spec.indexOf("!/");167/*168* REMIND: we don't handle nested JAR URLs169*/170if (separator == -1) {171throw new MalformedURLException("no !/ found in url spec:" + spec);172}173174jarFileURL = new URL(spec.substring(0, separator++));175entryName = null;176177/* if ! is the last letter of the innerURL, entryName is null */178if (++separator != spec.length()) {179entryName = spec.substring(separator, spec.length());180entryName = ParseUtil.decode (entryName);181}182}183184/**185* Returns the URL for the Jar file for this connection.186*187* @return the URL for the Jar file for this connection.188*/189public URL getJarFileURL() {190return jarFileURL;191}192193/**194* Return the entry name for this connection. This method195* returns null if the JAR file URL corresponding to this196* connection points to a JAR file and not a JAR file entry.197*198* @return the entry name for this connection, if any.199*/200public String getEntryName() {201return entryName;202}203204/**205* Return the JAR file for this connection.206*207* @return the JAR file for this connection. If the connection is208* a connection to an entry of a JAR file, the JAR file object is209* returned210*211* @exception IOException if an IOException occurs while trying to212* connect to the JAR file for this connection.213*214* @see #connect215*/216public abstract JarFile getJarFile() throws IOException;217218/**219* Returns the Manifest for this connection, or null if none.220*221* @return the manifest object corresponding to the JAR file object222* for this connection.223*224* @exception IOException if getting the JAR file for this225* connection causes an IOException to be thrown.226*227* @see #getJarFile228*/229public Manifest getManifest() throws IOException {230return getJarFile().getManifest();231}232233/**234* Return the JAR entry object for this connection, if any. This235* method returns null if the JAR file URL corresponding to this236* connection points to a JAR file and not a JAR file entry.237*238* @return the JAR entry object for this connection, or null if239* the JAR URL for this connection points to a JAR file.240*241* @exception IOException if getting the JAR file for this242* connection causes an IOException to be thrown.243*244* @see #getJarFile245* @see #getJarEntry246*/247public JarEntry getJarEntry() throws IOException {248return getJarFile().getJarEntry(entryName);249}250251/**252* Return the Attributes object for this connection if the URL253* for it points to a JAR file entry, null otherwise.254*255* @return the Attributes object for this connection if the URL256* for it points to a JAR file entry, null otherwise.257*258* @exception IOException if getting the JAR entry causes an259* IOException to be thrown.260*261* @see #getJarEntry262*/263public Attributes getAttributes() throws IOException {264JarEntry e = getJarEntry();265return e != null ? e.getAttributes() : null;266}267268/**269* Returns the main Attributes for the JAR file for this270* connection.271*272* @return the main Attributes for the JAR file for this273* connection.274*275* @exception IOException if getting the manifest causes an276* IOException to be thrown.277*278* @see #getJarFile279* @see #getManifest280*/281public Attributes getMainAttributes() throws IOException {282Manifest man = getManifest();283return man != null ? man.getMainAttributes() : null;284}285286/**287* Return the Certificate object for this connection if the URL288* for it points to a JAR file entry, null otherwise. This method289* can only be called once290* the connection has been completely verified by reading291* from the input stream until the end of the stream has been292* reached. Otherwise, this method will return {@code null}293*294* @return the Certificate object for this connection if the URL295* for it points to a JAR file entry, null otherwise.296*297* @exception IOException if getting the JAR entry causes an298* IOException to be thrown.299*300* @see #getJarEntry301*/302public java.security.cert.Certificate[] getCertificates()303throws IOException304{305JarEntry e = getJarEntry();306return e != null ? e.getCertificates() : null;307}308}309310311