@Test(expectedExceptions = IOException.class, expectedExceptionsMessageRegExp = "Mismatched hash on .+: expected \\w+ but got \\w+") public void testChecksum() throws IOException { DependencyLoader loader = new DependencyLoader(); loader.dependencyStore = store; Dependency dependency = new Dependency(); dependency.setUrl(new URL("https: dependency.setSha512sum(DatatypeConverter.parseHexBinary( "a84ad9f1646b52b6e19f55c3c63936c6dbe59597d53cec6855f85e21ad26f9bc27b0c541b793fab117e2359b1bf3fcd59b222255345fce858bc03cc48cbffd65")); DependencySet dependencySet = new DependencySet(); dependencySet.setDependencies(Collections.singletonList(dependency)); loader.downloadDependencies(dependencySet); }
@SneakyThrows(NoSuchAlgorithmException.class) public DependencyLoader downloadDependencies(DependencySet dependencySet) throws IOException { initDependencyStore(); for (Dependency dependency : dependencySet.getDependencies()) { URL cached = dependencyStore.getCachedDependency(dependency); if (cached == null) { logger.info("Downloading " + dependency.getUrl()); MessageDigest digest = MessageDigest.getInstance("SHA-512"); @SuppressWarnings("resource") ByteArrayOutputStream memoryStream = new ByteArrayOutputStream(); try (InputStream stream = dependency.getUrl().openStream()) { byte[] buf = new byte[4096]; int len; while ((len = stream.read(buf)) >= 0) { digest.update(buf, 0, len); memoryStream.write(buf, 0, len); } } byte[] hash = digest.digest(); if (!Arrays.equals(hash, dependency.getSha512sum())) { throw new IOException("Mismatched hash on " + dependency.getUrl() + ": expected " + DatatypeConverter.printHexBinary(dependency.getSha512sum()) + " but got " + DatatypeConverter.printHexBinary(hash)); } cached = dependencyStore.saveDependency(dependency, memoryStream.toByteArray()); } urls.add(cached); } logger.info("Added " + dependencySet.getDependencies().size() + " dependencies."); return this; }
DependencyLoader { @SneakyThrows(NoSuchAlgorithmException.class) public DependencyLoader downloadDependencies(DependencySet dependencySet) throws IOException { initDependencyStore(); for (Dependency dependency : dependencySet.getDependencies()) { URL cached = dependencyStore.getCachedDependency(dependency); if (cached == null) { logger.info("Downloading " + dependency.getUrl()); MessageDigest digest = MessageDigest.getInstance("SHA-512"); @SuppressWarnings("resource") ByteArrayOutputStream memoryStream = new ByteArrayOutputStream(); try (InputStream stream = dependency.getUrl().openStream()) { byte[] buf = new byte[4096]; int len; while ((len = stream.read(buf)) >= 0) { digest.update(buf, 0, len); memoryStream.write(buf, 0, len); } } byte[] hash = digest.digest(); if (!Arrays.equals(hash, dependency.getSha512sum())) { throw new IOException("Mismatched hash on " + dependency.getUrl() + ": expected " + DatatypeConverter.printHexBinary(dependency.getSha512sum()) + " but got " + DatatypeConverter.printHexBinary(hash)); } cached = dependencyStore.saveDependency(dependency, memoryStream.toByteArray()); } urls.add(cached); } logger.info("Added " + dependencySet.getDependencies().size() + " dependencies."); return this; } }
DependencyLoader { @SneakyThrows(NoSuchAlgorithmException.class) public DependencyLoader downloadDependencies(DependencySet dependencySet) throws IOException { initDependencyStore(); for (Dependency dependency : dependencySet.getDependencies()) { URL cached = dependencyStore.getCachedDependency(dependency); if (cached == null) { logger.info("Downloading " + dependency.getUrl()); MessageDigest digest = MessageDigest.getInstance("SHA-512"); @SuppressWarnings("resource") ByteArrayOutputStream memoryStream = new ByteArrayOutputStream(); try (InputStream stream = dependency.getUrl().openStream()) { byte[] buf = new byte[4096]; int len; while ((len = stream.read(buf)) >= 0) { digest.update(buf, 0, len); memoryStream.write(buf, 0, len); } } byte[] hash = digest.digest(); if (!Arrays.equals(hash, dependency.getSha512sum())) { throw new IOException("Mismatched hash on " + dependency.getUrl() + ": expected " + DatatypeConverter.printHexBinary(dependency.getSha512sum()) + " but got " + DatatypeConverter.printHexBinary(hash)); } cached = dependencyStore.saveDependency(dependency, memoryStream.toByteArray()); } urls.add(cached); } logger.info("Added " + dependencySet.getDependencies().size() + " dependencies."); return this; } @SuppressWarnings("UseOfSystemOutOrSystemErr")  DependencyLoader();  DependencyLoader(PrintStream streamLogger);  DependencyLoader(java.util.logging.Logger julLogger); private  DependencyLoader(Logger logger); }
DependencyLoader { @SneakyThrows(NoSuchAlgorithmException.class) public DependencyLoader downloadDependencies(DependencySet dependencySet) throws IOException { initDependencyStore(); for (Dependency dependency : dependencySet.getDependencies()) { URL cached = dependencyStore.getCachedDependency(dependency); if (cached == null) { logger.info("Downloading " + dependency.getUrl()); MessageDigest digest = MessageDigest.getInstance("SHA-512"); @SuppressWarnings("resource") ByteArrayOutputStream memoryStream = new ByteArrayOutputStream(); try (InputStream stream = dependency.getUrl().openStream()) { byte[] buf = new byte[4096]; int len; while ((len = stream.read(buf)) >= 0) { digest.update(buf, 0, len); memoryStream.write(buf, 0, len); } } byte[] hash = digest.digest(); if (!Arrays.equals(hash, dependency.getSha512sum())) { throw new IOException("Mismatched hash on " + dependency.getUrl() + ": expected " + DatatypeConverter.printHexBinary(dependency.getSha512sum()) + " but got " + DatatypeConverter.printHexBinary(hash)); } cached = dependencyStore.saveDependency(dependency, memoryStream.toByteArray()); } urls.add(cached); } logger.info("Added " + dependencySet.getDependencies().size() + " dependencies."); return this; } @SuppressWarnings("UseOfSystemOutOrSystemErr")  DependencyLoader();  DependencyLoader(PrintStream streamLogger);  DependencyLoader(java.util.logging.Logger julLogger); private  DependencyLoader(Logger logger); DependencyLoader addUrls(URL... urls); @SneakyThrows(NoSuchAlgorithmException.class) DependencyLoader downloadDependencies(DependencySet dependencySet); DependencyLoader downloadDependencies(); @SneakyThrows(ReflectiveOperationException.class) void appendToClassLoader(URLClassLoader classLoader); ClassLoader toParentFirstClassLoader(); ClassLoader toParentFirstClassLoader(ClassLoader parent); ClassLoader toParentLastClassLoader(); ClassLoader toParentLastClassLoader(ClassLoader parent); }
DependencyLoader { @SneakyThrows(NoSuchAlgorithmException.class) public DependencyLoader downloadDependencies(DependencySet dependencySet) throws IOException { initDependencyStore(); for (Dependency dependency : dependencySet.getDependencies()) { URL cached = dependencyStore.getCachedDependency(dependency); if (cached == null) { logger.info("Downloading " + dependency.getUrl()); MessageDigest digest = MessageDigest.getInstance("SHA-512"); @SuppressWarnings("resource") ByteArrayOutputStream memoryStream = new ByteArrayOutputStream(); try (InputStream stream = dependency.getUrl().openStream()) { byte[] buf = new byte[4096]; int len; while ((len = stream.read(buf)) >= 0) { digest.update(buf, 0, len); memoryStream.write(buf, 0, len); } } byte[] hash = digest.digest(); if (!Arrays.equals(hash, dependency.getSha512sum())) { throw new IOException("Mismatched hash on " + dependency.getUrl() + ": expected " + DatatypeConverter.printHexBinary(dependency.getSha512sum()) + " but got " + DatatypeConverter.printHexBinary(hash)); } cached = dependencyStore.saveDependency(dependency, memoryStream.toByteArray()); } urls.add(cached); } logger.info("Added " + dependencySet.getDependencies().size() + " dependencies."); return this; } @SuppressWarnings("UseOfSystemOutOrSystemErr")  DependencyLoader();  DependencyLoader(PrintStream streamLogger);  DependencyLoader(java.util.logging.Logger julLogger); private  DependencyLoader(Logger logger); DependencyLoader addUrls(URL... urls); @SneakyThrows(NoSuchAlgorithmException.class) DependencyLoader downloadDependencies(DependencySet dependencySet); DependencyLoader downloadDependencies(); @SneakyThrows(ReflectiveOperationException.class) void appendToClassLoader(URLClassLoader classLoader); ClassLoader toParentFirstClassLoader(); ClassLoader toParentFirstClassLoader(ClassLoader parent); ClassLoader toParentLastClassLoader(); ClassLoader toParentLastClassLoader(ClassLoader parent);  }