title: java md5 is slow in javaagent mode
body: <pre><code> public static void main(String[] args) {
      File f = new File(args[0]);
      System.out.println(&quot;1: &quot; + DownloadUtils.getMD5(f));
      System.out.println(&quot;2: &quot; + DownloadUtils.getMD5InJava(f));
      System.out.println(&quot;3: &quot; + DownloadUtils.getMD5InJava2(f));
  }
  
  public static void premain(String args, Instrumentation inst) {
      File f = new File(args);
      System.out.println(&quot;1: &quot; + DownloadUtils.getMD5(f));
      System.out.println(&quot;2: &quot; + DownloadUtils.getMD5InJava(f));
      System.out.println(&quot;3: &quot; + DownloadUtils.getMD5InJava2(f));
      System.exit(1);
  }
  </code></pre>
  <p>md5:</p>
  <pre><code>public static String getMD5(File file) {
      long start = System.currentTimeMillis();
      try {
          // fast-md5
          return MD5.asHex(MD5.getHash(file));
      } catch (IOException e) {
          e.printStackTrace();
          return &quot;&quot;;
      } finally {
          System.out.println(&quot;checkout md5 cost  : &quot; + (System.currentTimeMillis() - start));
      }
  }
  
  public static String getMD5InJava(File file) {
      long start = System.currentTimeMillis();
      BigInteger bi = null;
      try {
          byte[] buffer = new byte[8192];
          int len = 0;
          MessageDigest md = MessageDigest.getInstance(&quot;MD5&quot;);
          FileInputStream fis = new FileInputStream(file);
          while ((len = fis.read(buffer)) != -1) {
              md.update(buffer, 0, len);
          }
          fis.close();
          byte[] b = md.digest();
          bi = new BigInteger(1, b);
      } catch (NoSuchAlgorithmException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
      String md5 = bi.toString(16);
      if (md5.length() == 31) {
          return &quot;0&quot; + md5;
      }
      System.out.println(&quot;checkout md5 cost  : &quot; + (System.currentTimeMillis() - start));
      return md5;
  }
  
  public static String getMD5InJava2(File file) {
      long start = System.currentTimeMillis();
      MessageDigest md = null;
      try {
          md = MessageDigest.getInstance(&quot;MD5&quot;);
          md.update(Files.readAllBytes(Paths.get(file.getPath())));
          byte[] digest = md.digest();
          String md5 = DatatypeConverter.printHexBinary(digest).toUpperCase();
          return md5;
      } catch (Exception e) {
          e.printStackTrace();
          return &quot;&quot;;
      } finally {
          System.out.println(&quot;checkout md5 cost  : &quot; + (System.currentTimeMillis() - start));
      }
  }
  </code></pre>
  <p>and package a test.jar</p>
  <p>run <code>java -jar test.jar /root/bigfile.jar</code>  (bigfile.jar 100M) , and output:</p>
  <pre><code>checkout md5 cost  : 558
  1: ac6e30dfb8493bb21dfb9cd51beac152
  checkout md5 cost  : 537
  2: ac6e30dfb8493bb21dfb9cd51beac152
  checkout md5 cost  : 692
  3: AC6E30DFB8493BB21DFB9CD51BEAC152
  </code></pre>
  <p>run <code>java -javaagent:test.jar=/root/bigfile.jar -jar demo.jar</code> ，and output:</p>
  <pre><code>checkout md5 cost  : 7582
  1: ac6e30dfb8493bb21dfb9cd51beac152
  checkout md5 cost  : 17506
  2: ac6e30dfb8493bb21dfb9cd51beac152
  checkout md5 cost  : 17360
  3: AC6E30DFB8493BB21DFB9CD51BEAC152
  </code></pre>
  <p>Why md5 in javaagent is so slow?</p>