private URLConnection openFallbackConnection(URL url, Exception reason) throws IOException { try { return openConnection(getFallbackHandler(), url); } catch (Exception ex) { if (reason instanceof IOException) { log(false, "Unable to open fallback handler", ex); throw (IOException) reason; } log(true, "Unable to open fallback handler", ex); if (reason instanceof RuntimeException) { throw (RuntimeException) reason; } throw new IllegalStateException(reason); } }
@Override protected void parseURL(URL context, String spec, int start, int limit) { if (spec.regionMatches(true, 0, JAR_PROTOCOL, 0, JAR_PROTOCOL.length())) { setFile(context, getFileFromSpec(spec.substring(start, limit))); } else { setFile(context, getFileFromContext(context, spec.substring(start, limit))); } }
public JarFile getRootJarFileFromUrl(URL url) throws IOException { String spec = url.getFile(); int separatorIndex = spec.indexOf(SEPARATOR); if (separatorIndex == -1) { throw new MalformedURLException("Jar URL does not contain !/ separator"); } String name = spec.substring(0, separatorIndex); return getRootJarFile(name); }
@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 protected URLConnection openConnection(URL url) throws IOException { URLConnection urlConnection = super.openConnection(url); return indexes.contains(url.toString()) && urlConnection instanceof JarURLConnection ? new XBootURLConnection((JarURLConnection) urlConnection, xDecryptor, xEncryptor, xKey) : urlConnection; }
private int hashCode(String protocol, String file) { int result = (protocol != null) ? protocol.hashCode() : 0; int separatorIndex = file.indexOf(SEPARATOR); if (separatorIndex == -1) { return result + file.hashCode(); } String source = file.substring(0, separatorIndex); String entry = canonicalize(file.substring(separatorIndex + 2)); try { result += new URL(source).hashCode(); } catch (MalformedURLException ex) { result += source.hashCode(); } result += entry.hashCode(); return result; }
@Override protected int hashCode(URL u) { return hashCode(u.getProtocol(), u.getFile()); }
/** * Return a URL that can be used to access this JAR file. NOTE: the specified URL * cannot be serialized and or cloned. * @return the URL * @throws MalformedURLException if the URL is malformed */ public URL getUrl() throws MalformedURLException { if (this.url == null) { Handler handler = new Handler(this); String file = this.rootFile.getFile().toURI() + this.pathFromRoot + "!/"; file = file.replace("file:////", "file://"); // Fix UNC paths this.url = new URL("jar", "", -1, file, handler); } return this.url; }
private JarFile getRootJarFile(String name) throws IOException { try { if (!name.startsWith(FILE_PROTOCOL)) { throw new IllegalStateException("Not a file URL"); } String path = name.substring(FILE_PROTOCOL.length()); File file = new File(URLDecoder.decode(path, "UTF-8")); Map<File, JarFile> cache = rootFileCache.get(); JarFile result = (cache != null) ? cache.get(file) : null; if (result == null) { result = new JarFile(file); addToRootFileCache(file, result); } return result; } catch (Exception ex) { throw new IOException("Unable to open root Jar file '" + name + "'", ex); } }
@Override protected boolean sameFile(URL u1, URL u2) { if (!u1.getProtocol().equals("jar") || !u2.getProtocol().equals("jar")) { return false; } int separator1 = u1.getFile().indexOf(SEPARATOR); int separator2 = u2.getFile().indexOf(SEPARATOR); if (separator1 == -1 || separator2 == -1) { return super.sameFile(u1, u2); } String nested1 = u1.getFile().substring(separator1 + SEPARATOR.length()); String nested2 = u2.getFile().substring(separator2 + SEPARATOR.length()); if (!nested1.equals(nested2)) { String canonical1 = canonicalize(nested1); String canonical2 = canonicalize(nested2); if (!canonical1.equals(canonical2)) { return false; } } String root1 = u1.getFile().substring(0, separator1); String root2 = u2.getFile().substring(0, separator2); try { return super.sameFile(new URL(root1), new URL(root2)); } catch (MalformedURLException ex) { // Continue } return super.sameFile(u1, u2); }