private String[] toFilePaths(final List<String> urls) { final List<String> files = new ArrayList<String>(); for (String url : urls) { final File dir = new File(url); if (!dir.isDirectory()) { continue; } for (File f : Files.collect(dir, new ClassFilter())) { files.add(f.getAbsolutePath()); } } return files.toArray(new String[files.size()]); }
public static String hash(final Set<URL> urls, final String algo) { final Collection<File> files = new ArrayList<File>(); for (final URL u : urls) { final File file = toFile(u); if (!file.isDirectory()) { files.add(file); } else { files.addAll(Files.collect(file, TrueFilter.INSTANCE)); } } MessageDigest digest = DIGESTS.get(algo); if (digest == null) { try { digest = MessageDigest.getInstance(algo); } catch (final NoSuchAlgorithmException e) { throw new LoaderRuntimeException(e); } DIGESTS.put(algo, digest); } for (final File file : files) { if (!file.exists()) { continue; } DigestInputStream is = null; try { is = new DigestInputStream(new FileInputStream(file), digest); IO.copy(is, new NoopOutputStream()); // read the stream } catch (final IOException e) { // no-op: shouldn't occur here } finally { IO.close(is); } } final byte[] hash = digest.digest(); digest.reset(); final StringBuilder sb = new StringBuilder(""); for (final byte b : hash) { // hex convertion sb.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1)); } return sb.toString(); }