< prev index next >

src/java.base/share/classes/sun/net/www/protocol/jar/JarURLConnection.java

Print this page




  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.net.www.protocol.jar;
  27 
  28 import java.io.InputStream;
  29 import java.io.IOException;
  30 import java.io.FileNotFoundException;
  31 import java.io.BufferedInputStream;

  32 import java.net.URL;
  33 import java.net.URLConnection;
  34 import java.net.MalformedURLException;
  35 import java.net.UnknownServiceException;

  36 import java.util.Enumeration;
  37 import java.util.Map;
  38 import java.util.List;
  39 import java.util.jar.JarEntry;
  40 import java.util.jar.JarFile;
  41 import java.util.jar.Manifest;
  42 import java.security.Permission;
  43 
  44 /**
  45  * @author Benjamin Renaud
  46  * @since 1.2
  47  */
  48 public class JarURLConnection extends java.net.JarURLConnection {
  49 
  50     private static final boolean debug = false;
  51 
  52     /* the Jar file factory. It handles both retrieval and caching.
  53      */
  54     private static final JarFileFactory factory = JarFileFactory.getInstance();
  55 



  56     /* the url for the Jar file */
  57     private URL jarFileURL;
  58 
  59     /* the permission to get this JAR file. This is the actual, ultimate,
  60      * permission, returned by the jar file factory.
  61      */
  62     private Permission permission;
  63 
  64     /* the url connection for the JAR file */
  65     private URLConnection jarFileURLConnection;
  66 
  67     /* the entry name, if any */
  68     private String entryName;
  69 
  70     /* the JarEntry */
  71     private JarEntry jarEntry;
  72 
  73     /* the jar file corresponding to this connection */
  74     private JarFile jarFile;
  75 


 142                                                     jarFile.getName());
 143                 }
 144             }
 145             connected = true;
 146         }
 147     }
 148 
 149     public InputStream getInputStream() throws IOException {
 150         connect();
 151 
 152         InputStream result = null;
 153 
 154         if (entryName == null) {
 155             throw new IOException("no entry name specified");
 156         } else {
 157             if (jarEntry == null) {
 158                 throw new FileNotFoundException("JAR entry " + entryName +
 159                                                 " not found in " +
 160                                                 jarFile.getName());
 161             }
 162             result = new JarURLInputStream (jarFile.getInputStream(jarEntry));









 163         }
 164         return result;
 165     }
 166 
 167     public int getContentLength() {
 168         long result = getContentLengthLong();
 169         if (result > Integer.MAX_VALUE)
 170             return -1;
 171         return (int) result;
 172     }
 173 
 174     public long getContentLengthLong() {
 175         long result = -1;
 176         try {
 177             connect();
 178             if (jarEntry == null) {
 179                 /* if the URL referes to an archive */
 180                 result = jarFileURLConnection.getContentLengthLong();
 181             } else {
 182                 /* if the URL referes to an archive entry */




  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.net.www.protocol.jar;
  27 
  28 import java.io.InputStream;
  29 import java.io.IOException;
  30 import java.io.FileNotFoundException;
  31 import java.io.BufferedInputStream;
  32 import java.io.ByteArrayInputStream;
  33 import java.net.URL;
  34 import java.net.URLConnection;
  35 import java.net.MalformedURLException;
  36 import java.net.UnknownServiceException;
  37 import java.nio.ByteBuffer;
  38 import java.util.Enumeration;
  39 import java.util.Map;
  40 import java.util.List;
  41 import java.util.jar.JarEntry;
  42 import java.util.jar.JarFile;
  43 import java.util.jar.Manifest;
  44 import java.security.Permission;
  45 
  46 /**
  47  * @author Benjamin Renaud
  48  * @since 1.2
  49  */
  50 public class JarURLConnection extends java.net.JarURLConnection {
  51 
  52     private static final boolean debug = false;
  53 
  54     /* the Jar file factory. It handles both retrieval and caching.
  55      */
  56     private static final JarFileFactory factory = JarFileFactory.getInstance();
  57 
  58     private static final sun.misc.JavaUtilZipFileAccess zipAccess
  59             = sun.misc.SharedSecrets.getJavaUtilZipFileAccess();
  60 
  61     /* the url for the Jar file */
  62     private URL jarFileURL;
  63 
  64     /* the permission to get this JAR file. This is the actual, ultimate,
  65      * permission, returned by the jar file factory.
  66      */
  67     private Permission permission;
  68 
  69     /* the url connection for the JAR file */
  70     private URLConnection jarFileURLConnection;
  71 
  72     /* the entry name, if any */
  73     private String entryName;
  74 
  75     /* the JarEntry */
  76     private JarEntry jarEntry;
  77 
  78     /* the jar file corresponding to this connection */
  79     private JarFile jarFile;
  80 


 147                                                     jarFile.getName());
 148                 }
 149             }
 150             connected = true;
 151         }
 152     }
 153 
 154     public InputStream getInputStream() throws IOException {
 155         connect();
 156 
 157         InputStream result = null;
 158 
 159         if (entryName == null) {
 160             throw new IOException("no entry name specified");
 161         } else {
 162             if (jarEntry == null) {
 163                 throw new FileNotFoundException("JAR entry " + entryName +
 164                                                 " not found in " +
 165                                                 jarFile.getName());
 166             }
 167             long size = jarEntry.getSize();
 168             if (size > 0 && size < 512 * 1024) {
 169                 byte[] bytes = new byte[(int) size];
 170                 ByteBuffer bb = ByteBuffer.wrap(bytes);
 171                 zipAccess.fillByteBuffer(jarFile, jarEntry, bb);
 172                 result = new JarURLInputStream(new ByteArrayInputStream(bytes, 0, bb.position()));
 173             } else {
 174                 // Unknown or too large ZipEntry size to use a array
 175                 result = new JarURLInputStream(jarFile.getInputStream(jarEntry));
 176             }
 177         }
 178         return result;
 179     }
 180 
 181     public int getContentLength() {
 182         long result = getContentLengthLong();
 183         if (result > Integer.MAX_VALUE)
 184             return -1;
 185         return (int) result;
 186     }
 187 
 188     public long getContentLengthLong() {
 189         long result = -1;
 190         try {
 191             connect();
 192             if (jarEntry == null) {
 193                 /* if the URL referes to an archive */
 194                 result = jarFileURLConnection.getContentLengthLong();
 195             } else {
 196                 /* if the URL referes to an archive entry */


< prev index next >