FileUtils { @SuppressWarnings("try") public static String getChecksum(final File file) throws IOException { try (TelemetryUtils.ScopedSpan scope = TelemetryUtils.startScopedSpanLow("FileUtils.getChecksum")) { TelemetryUtils.ScopedSpan.addAnnotation( TelemetryUtils.annotationBuilder().put("file", file.getPath()).build("args")); if (!file.exists()) { return RandomStringUtils.random(10); } try { final MessageDigest md = MessageDigest.getInstance(ALGORITHM_SHA_512); try (final InputStream is = Files.newInputStream(file.toPath()); DigestInputStream dis = new DigestInputStream(is, md)) { final byte[] buf = new byte[4096]; while (dis.read(buf) != -1) {} final byte[] digest = md.digest(); final StringBuilder sb = new StringBuilder(128); for (final int b : digest) { sb.append(Character.forDigit(b >> 4 & 0xF, 16)); sb.append(Character.forDigit(b & 0xF, 16)); } return sb.toString(); } } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } } }  static boolean isJavaFile(final File file); @SuppressWarnings("try") static String getChecksum(final File file); static List<File> listJavaFiles(File parent); @SuppressWarnings("try") static List<File> collectFiles(final File root, final String ext); @SuppressWarnings("try") static Optional<File> collectFile(final File root, final String ext); static boolean filterFile(final File file); @SuppressWarnings("try") static Optional<String> findProjectID(final File root, final String target); static String getVersionInfo(); @SuppressWarnings("try") static Optional<File> getSourceFile(final String importClass, final Set<File> sourceRoots); @SuppressWarnings("try") static Optional<File> getClassFile(
      String path, final Set<File> sourceRoots, final File out); @SuppressWarnings("try") static Collection<File> getPackagePrivateSource(final List<File> compileFiles); @SuppressWarnings("try") static List<File> getModifiedSources(
      final File projectRoot,
      final List<File> sourceFiles,
      final Set<File> sourceRoots,
      final File output); @SuppressWarnings("try") static List<String> readLines(File file); @SuppressWarnings("try") static void readRangeLines(File file, long start, long end, Consumer<String> fn); @SuppressWarnings("try") static void formatJavaFile(final Properties properties, final String path); @SuppressWarnings("try") static void formatJavaFile(final String path); static Properties loadPropertiesFile(final File file); @SuppressWarnings("try") static Optional<File> existsFQCN(final Set<File> roots, final String fqcn); @SuppressWarnings("try") static Optional<Source> getSource(final File file); @SuppressWarnings("try") static Optional<String> convertPathToClass(final Set<File> roots, final File file); static String shortenPath(String path); static final String JAVA_EXT; static final String JAR_EXT; static final String CLASS_EXT; }
@Test public void testMd5sum1() throws Exception { final String sum = timeIt( () -> { return FileUtils.getChecksum( new File( project.getProjectRootPath(), "./src/main/java/meghanada/analyze/JavaAnalyzer.java") .getCanonicalFile()); }); System.out.println(sum); }
@Test public void testMd5sum2() throws Exception { final String javaHome = System.getProperty("java.home"); final String sum = timeIt( () -> { File jar = new File(javaHome, "/lib/rt.jar"); if (!jar.exists()) { jar = new File(javaHome, "/lib/jrt-fs.jar"); } return FileUtils.getChecksum(jar.getCanonicalFile()); }); System.out.println(sum); }