ClassPathURLStreamHandler.java revision 05a5c2f89e12e27db69f24165a05bdfd0476c73a
1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package libcore.io; 18 19import java.io.File; 20import java.io.FileNotFoundException; 21import java.io.FilterInputStream; 22import java.io.IOException; 23import java.io.InputStream; 24import java.net.JarURLConnection; 25import java.net.MalformedURLException; 26import java.net.URL; 27import java.net.URLConnection; 28import java.net.URLStreamHandler; 29import java.util.jar.JarFile; 30import java.util.jar.StrictJarFile; 31import java.util.zip.ZipEntry; 32import libcore.net.url.JarHandler; 33 34/** 35 * A {@link URLStreamHandler} for a specific class path {@link JarFile}. This class avoids the need 36 * to open a jar file multiple times to read resources if the jar file can be held open. The 37 * {@link URLConnection} objects created are a subclass of {@link JarURLConnection}. 38 * 39 * <p>Use {@link #getEntryUrlOrNull(String)} to obtain a URL backed by this stream handler. 40 */ 41public class ClassPathURLStreamHandler extends JarHandler { 42 private final String fileUri; 43 private final StrictJarFile jarFile; 44 45 public ClassPathURLStreamHandler(String jarFileName) throws IOException { 46 // We use StrictJarFile because it is much less heap memory hungry than ZipFile / JarFile. 47 jarFile = new StrictJarFile(jarFileName); 48 49 // File.toURI() is compliant with RFC 1738 in always creating absolute path names. If we 50 // construct the URL by concatenating strings, we might end up with illegal URLs for relative 51 // names. 52 this.fileUri = new File(jarFileName).toURI().toString(); 53 } 54 55 /** 56 * Returns a URL backed by this stream handler for the named resource, or {@code null} if the 57 * resource cannot be found under the exact name presented. 58 */ 59 public URL getEntryUrlOrNull(String entryName) { 60 if (jarFile.findEntry(entryName) != null) { 61 try { 62 // We rely on the URL/the stream handler to deal with any url encoding necessary here, and 63 // we assume it is completely reversible. 64 return new URL("jar", null, -1, fileUri + "!/" + entryName, this); 65 } catch (MalformedURLException e) { 66 throw new RuntimeException("Invalid entry name", e); 67 } 68 } 69 return null; 70 } 71 72 @Override 73 protected URLConnection openConnection(URL url) throws IOException { 74 return new ClassPathURLConnection(url, jarFile); 75 } 76 77 public void close() throws IOException { 78 jarFile.close(); 79 } 80 81 private static class ClassPathURLConnection extends JarURLConnection { 82 83 private final StrictJarFile strictJarFile; 84 private ZipEntry jarEntry; 85 private InputStream jarInput; 86 private boolean closed; 87 private JarFile jarFile; 88 89 public ClassPathURLConnection(URL url, StrictJarFile strictJarFile) 90 throws MalformedURLException { 91 super(url); 92 this.strictJarFile = strictJarFile; 93 } 94 95 @Override 96 public void connect() throws IOException { 97 if (!connected) { 98 this.jarEntry = strictJarFile.findEntry(getEntryName()); 99 if (jarEntry == null) { 100 throw new FileNotFoundException( 101 "URL does not correspond to an entry in the zip file. URL=" + url 102 + ", zipfile=" + strictJarFile.getName()); 103 } 104 connected = true; 105 } 106 } 107 108 @Override 109 public JarFile getJarFile() throws IOException { 110 connect(); 111 112 // This is expensive because we only pretend that we wrap a JarFile. 113 if (jarFile == null) { 114 jarFile = new JarFile(strictJarFile.getName()); 115 } 116 return jarFile; 117 } 118 119 @Override 120 public InputStream getInputStream() throws IOException { 121 if (closed) { 122 throw new IllegalStateException("JarURLConnection InputStream has been closed"); 123 } 124 connect(); 125 if (jarInput != null) { 126 return jarInput; 127 } 128 return jarInput = new FilterInputStream(strictJarFile.getInputStream(jarEntry)) { 129 @Override 130 public void close() throws IOException { 131 super.close(); 132 closed = true; 133 } 134 }; 135 } 136 137 /** 138 * Returns the content type of the entry based on the name of the entry. Returns 139 * non-null results ("content/unknown" for unknown types). 140 * 141 * @return the content type 142 */ 143 @Override 144 public String getContentType() { 145 String cType = guessContentTypeFromName(getEntryName()); 146 if (cType == null) { 147 cType = "content/unknown"; 148 } 149 return cType; 150 } 151 152 @Override 153 public int getContentLength() { 154 try { 155 connect(); 156 return (int) getJarEntry().getSize(); 157 } catch (IOException e) { 158 // Ignored 159 } 160 return -1; 161 } 162 } 163} 164