Source:
protected URL retrieve(Channel channel, long sum1, long sum2) throws IOException, InterruptedException { Checksum expected = new Checksum(sum1, sum2); File target = map(sum1, sum2); if (target.exists()) { Checksum actual = fileChecksum(target); if (expected.equals(actual)) { LOGGER.fine(String.format("Jar file already exists: %s", expected)); return target.toURI().toURL(); } LOGGER.warning(String.format( "Cached file checksum mismatch: %s%nExpected: %s%n Actual: %s", target.getAbsolutePath(), expected, actual )); Files.delete(PathUtils.fileToPath(target)); synchronized (checksumsByPath) { checksumsByPath.remove(target.getCanonicalPath()); } } try { File tmp = createTempJar(target); try { RemoteOutputStream o = new RemoteOutputStream(new FileOutputStream(tmp)); try { LOGGER.log(Level.FINE, String.format("Retrieving jar file %16X%16X",sum1,sum2)); getJarLoader(channel).writeJarTo(sum1, sum2, o); } finally { o.close(); } Checksum actual = Checksum.forFile(tmp); if (!expected.equals(actual)) { throw new IOException(String.format( "Incorrect checksum of retrieved jar: %s%nExpected: %s%nActual: %s", tmp.getAbsolutePath(), expected, actual)); } if (!tmp.renameTo(target)) { if (!target.exists()) { throw new IOException("Unable to create " + target + " from " + tmp); } actual = fileChecksum(target); if (!expected.equals(actual)) { throw new IOException(String.format( "Incorrect checksum of previous jar: %s%nExpected: %s%nActual: %s", target.getAbsolutePath(), expected, actual)); } } return target.toURI().toURL(); } finally { Files.deleteIfExists(PathUtils.fileToPath(tmp)); } } catch (IOException e) { throw (IOException) new IOException("Failed to write to "+target, e); } }

Target:
protected URL retrieve(Channel channel, long sum1, long sum2) throws IOException, InterruptedException { Checksum expected = new Checksum(sum1, sum2); File target = map(sum1, sum2); if (target.exists()) { Checksum actual = fileChecksum(target); if (expected.equals(actual)) { LOGGER.fine(String.format("Jar file already exists: %s", expected)); return target.toURI().toURL(); } LOGGER.warning(String.format( "Cached file checksum mismatch: %s%nExpected: %s%n Actual: %s", target.getAbsolutePath(), expected, actual )); Files.delete(PathUtils.fileToPath(target)); synchronized (checksumsByPath) { checksumsByPath.remove(target.getCanonicalPath()); } } try { File tmp = createTempJar(target); try { RemoteOutputStream o = new RemoteOutputStream(new FileOutputStream(tmp)); try { LOGGER.log(Level.FINE, String.format("Retrieving jar file %16X%16X",sum1,sum2)); getJarLoader(channel).writeJarTo(sum1, sum2, o); } finally { o.close(); } Checksum actual = Checksum.forFile(tmp); if (!expected.equals(actual)) { throw new IOException(String.format( "Incorrect checksum of retrieved jar: %s%nExpected: %s%nActual: %s", tmp.getAbsolutePath(), expected, actual)); } if (!tmp.renameTo(target)) { if (!target.exists()) { throw new IOException("Unable to create " + target + " from " + tmp); } actual = fileChecksum(target); if (!expected.equals(actual)) { throw new IOException(String.format( "Incorrect checksum of previous jar: %s%nExpected: %s%nActual: %s", target.getAbsolutePath(), expected, actual)); } } return target.toURI().toURL(); } finally { Files.deleteIfExists(PathUtils.fileToPath(tmp)); } } catch (IOException e) { throw new IOException("Failed to write to "+target, e); } }