package com.nitorcreations.willow.utils;

import java.io.ByteArrayOutputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

public class MD5SumInputStream extends FilterInputStream {
  private final MessageDigest digest;

  public MD5SumInputStream(InputStream in) throws NoSuchAlgorithmException {
    super(in);
    digest = MessageDigest.getInstance("MD5");
  }

  @Override
  public int read() throws IOException {
    int ret = super.read();
    if (ret > -1) {
      digest.update((byte) (ret & 0xFF));
    }
    return ret;
  }

  @Override
  public int read(byte[] buff, int off, int len) throws IOException {
    int ret = super.read(buff, off, len);
    if (ret > 0) {
      digest.update(buff, off, ret);
    }
    return ret;
  }

  public byte[] digest() {
    return digest.digest();
  }
  @SuppressFBWarnings(value={"RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE"},
      justification="null check in check-with-resources magic bytecode")
  public static byte[] getMd5FromURL(URL url) throws IOException {
    try (ByteArrayOutputStream out = new ByteArrayOutputStream();
        InputStream in = url.openConnection().getInputStream()) {
      int read=0;
      byte[] buff = new byte[1024 * 4];
      while (-1 < (read = in.read(buff))) {
        out.write(buff, 0, read);
      }
      String md5Str = new String(out.toByteArray(), 0, 32, StandardCharsets.UTF_8);
      return hexStringToByteArray(md5Str);
    }
  }

  public static byte[] hexStringToByteArray(String s) {
    int len = s.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
      data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
          + Character.digit(s.charAt(i+1), 16));
    }
    return data;
  }
}

@@ -77,42 +77,8 @@
 am;%0A
-import java.io.FilterInputStream;%0A
 impo
@@ -101,16 +101,16 @@
 eption;%0A
+
 import j
@@ -192,16 +192,56 @@
 arsets;%0A
+import java.security.DigestInputStream;%0A
 import j
@@ -419,14 +419,14 @@
 nds 
-Filter
+Digest
 Inpu
@@ -439,47 +439,8 @@
 m %7B%0A
-  private final MessageDigest digest;%0A%0A
   pu
@@ -462,16 +462,22 @@
 tStream(
+final 
 InputStr
@@ -480,18 +480,22 @@
 tStream 
-in
+stream
 ) throws
@@ -536,25 +536,15 @@
 per(
-in);%0A    digest =
+stream,
  Mes
@@ -576,392 +576,15 @@
 D5%22)
-;%0A  %7D%0A%0A  @Override%0A  public int read() throws IOException %7B%0A    int ret = super.read();%0A    if (ret %3E -1) %7B%0A      digest.update((byte) (ret & 0xFF));%0A    %7D%0A    return ret;%0A  %7D%0A%0A  @Override%0A  public int read(byte%5B%5D buff, int off, int len) throws IOException %7B%0A    int ret = super.read(buff, off, len);%0A    if (ret %3E 0) %7B%0A      digest.update(buff, off, ret);%0A    %7D%0A    return ret
+)
 ;%0A  %7D%0A
-%0A
   pu
@@ -617,22 +617,34 @@
  return 
-d
+getMessageD
 igest
+()
 .digest(
@@ -1277,13 +1277,12 @@
   %7D%0A
+
   %7D%0A
-%0A
   pu

Switch to JDK default implementation