private static JarURLConnection notFound(JarFile jarFile, JarEntryName jarEntryName) throws IOException { if (Boolean.TRUE.equals(useFastExceptions.get())) { return NOT_FOUND_CONNECTION; } return new JarURLConnection(null, jarFile, jarEntryName); }
@Override public URL getJarFileURL() { if (this.jarFile == null) { throw NOT_FOUND_CONNECTION_EXCEPTION; } if (this.jarFileUrl == null) { this.jarFileUrl = buildJarFileUrl(); } return this.jarFileUrl; }
@Override public JarFile getJarFile() throws IOException { connect(); return this.jarFile; }
static JarURLConnection get(URL url, JarFile jarFile) throws IOException { StringSequence spec = new StringSequence(url.getFile()); int index = indexOfRootSpec(spec, jarFile.getPathFromRoot()); if (index == -1) { return (Boolean.TRUE.equals(useFastExceptions.get()) ? NOT_FOUND_CONNECTION : new JarURLConnection(url, null, EMPTY_JAR_ENTRY_NAME)); } int separator; while ((separator = spec.indexOf(SEPARATOR, index)) > 0) { JarEntryName entryName = JarEntryName.get(spec.subSequence(index, separator)); JarEntry jarEntry = jarFile.getJarEntry(entryName.toCharSequence()); if (jarEntry == null) { return JarURLConnection.notFound(jarFile, entryName); } jarFile = jarFile.getNestedJarFile(jarEntry); index = separator + SEPARATOR.length(); } JarEntryName jarEntryName = JarEntryName.get(spec, index); if (Boolean.TRUE.equals(useFastExceptions.get()) && !jarEntryName.isEmpty() && !jarFile.containsEntry(jarEntryName.toString())) { return NOT_FOUND_CONNECTION; } return new JarURLConnection(url, jarFile, jarEntryName); }
@Override public void connect() throws IOException { if (this.jarFile == null) { throw FILE_NOT_FOUND_EXCEPTION; } if (!this.jarEntryName.isEmpty() && this.jarEntry == null) { this.jarEntry = this.jarFile.getJarEntry(getEntryName()); if (this.jarEntry == null) { throwFileNotFound(this.jarEntryName, this.jarFile); } } this.connected = true; }
@Override public InputStream getInputStream() throws IOException { if (this.jarFile == null) { throw FILE_NOT_FOUND_EXCEPTION; } if (this.jarEntryName.isEmpty() && this.jarFile.getType() == JarFile.JarFileType.DIRECT) { throw new IOException("no entry name specified"); } connect(); InputStream inputStream = (this.jarEntryName.isEmpty() ? this.jarFile.getData().getInputStream() : this.jarFile.getInputStream(this.jarEntry)); if (inputStream == null) { throwFileNotFound(this.jarEntryName, this.jarFile); } return inputStream; }
@Override public long getLastModified() { if (this.jarFile == null || this.jarEntryName.isEmpty()) { return 0; } try { JarEntry entry = getJarEntry(); return (entry != null) ? entry.getTime() : 0; } catch (IOException ex) { return 0; } }
/** * Set if a generic static exception can be thrown when a URL cannot be connected. * This optimization is used during class loading to save creating lots of exceptions * which are then swallowed. * @param useFastConnectionExceptions if fast connection exceptions can be used. */ public static void setUseFastConnectionExceptions( boolean useFastConnectionExceptions) { JarURLConnection.setUseFastExceptions(useFastConnectionExceptions); }
@Override public int getContentLength() { long length = getContentLengthLong(); if (length > Integer.MAX_VALUE) { return -1; } return (int) length; }
private static JarURLConnection notFound() { try { return notFound(null, null); } catch (IOException ex) { throw new IllegalStateException(ex); } }
@Override protected URLConnection openConnection(URL url) throws IOException { if (this.jarFile != null && isUrlInJarFile(url, this.jarFile)) { return JarURLConnection.get(url, this.jarFile); } try { return JarURLConnection.get(url, getRootJarFileFromUrl(url)); } catch (Exception ex) { return openFallbackConnection(url, ex); } }
@Override public long getContentLengthLong() { if (this.jarFile == null) { return -1; } try { if (this.jarEntryName.isEmpty()) { return this.jarFile.size(); } JarEntry entry = getJarEntry(); return (entry != null) ? (int) entry.getSize() : -1; } catch (IOException ex) { return -1; } }
@Override public JarEntry getJarEntry() throws IOException { if (this.jarEntryName == null || this.jarEntryName.isEmpty()) { return null; } connect(); return this.jarEntry; }
@Override public Object getContent() throws IOException { connect(); return this.jarEntryName.isEmpty() ? this.jarFile : super.getContent(); }