Implementation of Jar that is optimised for file based JAR URLs that refer directly to a JAR file (e.g. URLs of the form jar:file: ... .jar!/ or file:... .jar).
| 38 | * of the form jar:file: ... .jar!/ or file:... .jar). |
| 39 | */ |
| 40 | public class JarFileUrlJar implements Jar { |
| 41 | |
| 42 | private final JarFile jarFile; |
| 43 | private final URL jarFileURL; |
| 44 | private final boolean multiRelease; |
| 45 | private Enumeration<JarEntry> entries; |
| 46 | private Set<String> entryNamesSeen; |
| 47 | private JarEntry entry = null; |
| 48 | |
| 49 | /** |
| 50 | * Constructs a new JarFileUrlJar. |
| 51 | * |
| 52 | * @param url the URL of the JAR file |
| 53 | * @param startsWithJar whether the URL starts with "jar:" |
| 54 | * @throws IOException if an I/O error occurs |
| 55 | */ |
| 56 | public JarFileUrlJar(URL url, boolean startsWithJar) throws IOException { |
| 57 | if (startsWithJar) { |
| 58 | // jar:file:... |
| 59 | JarURLConnection jarConn = (JarURLConnection) url.openConnection(); |
| 60 | jarConn.setUseCaches(false); |
| 61 | jarFile = jarConn.getJarFile(); |
| 62 | jarFileURL = jarConn.getJarFileURL(); |
| 63 | } else { |
| 64 | // file:... |
| 65 | File f; |
| 66 | try { |
| 67 | f = new File(url.toURI()); |
| 68 | } catch (URISyntaxException e) { |
| 69 | throw new IOException(e); |
| 70 | } |
| 71 | jarFile = new JarFile(f, true, ZipFile.OPEN_READ, Runtime.version()); |
| 72 | jarFileURL = url; |
| 73 | } |
| 74 | boolean multiReleaseValue = false; |
| 75 | try { |
| 76 | multiReleaseValue = jarFile.isMultiRelease(); |
| 77 | } catch (IllegalStateException ignore) { |
| 78 | /* |
| 79 | * ISE can be thrown if the JAR URL is bad, for example: |
| 80 | * https://github.com/spring-projects/spring-boot/issues/33633 |
| 81 | * |
| 82 | * The Javadoc does not document that ISE and given what it does for a vanilla IOE, this looks like a Java |
| 83 | * bug, it should return false instead. |
| 84 | */ |
| 85 | } |
| 86 | multiRelease = multiReleaseValue; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | @Override |
| 91 | public URL getJarFileURL() { |
| 92 | return jarFileURL; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | @Override |
| 97 | public InputStream getInputStream(String name) throws IOException { |
nothing calls this directly
no outgoing calls
no test coverage detected