当前位置:网站首页>How to calculate MD5 value without additional read stream

How to calculate MD5 value without additional read stream

2021-09-15 07:46:08 The end of time

Imagine a scenario like this : Read the file from the network stream to the hard disk and calculate the size of the file md5 value . The usual practice is to save the file first , Then calculate the of the file md5, But this will happen three times I/O, A network I/O, Secondary disk I/O.

Cause additional disk I/O Writing

import org.apache.commons.io.IOUtils;
import org.springframework.util.DigestUtils;
import java.io.*;
import java.net.URL;
public class Main {
    public static void main(String[] args) throws Exception {
        // Download the file , Baidu logo
        URL url = new URL("https://www.baidu.com/img/flexible/logo/plus_logo_web_2.png");
        File file = new File("file.png");
        try (InputStream in = url.openStream(); OutputStream out = new FileOutputStream(file)) {
            IOUtils.copy(in, out);
        }
        // Calculation md5
        try (FileInputStream fileInputStream = new FileInputStream(file)) {
            // Calculation md5
            String md5 = DigestUtils.md5DigestAsHex(fileInputStream);
            System.out.println(md5);
        }
    }
}

The above code is used spring Provided MD5 Tool class , Pass the stream as a parameter , Go straight back to md5 value , Such a tool is simple and convenient to use , Is the first choice for many people .
However ,java Provided md5 Tools don't have such interfaces ,java Provide MessageDigest Tool class calculation md5 Only the lower level interface is provided , Because it's not easy to use , Many people ignore it .

In the following example , Download the file and calculate md5 It only takes one disk I/O

import java.io.*;
import java.math.BigInteger;
import java.net.URL;
import java.security.MessageDigest;

public class Main {
    public static void main(String[] args) throws Exception {
        // Download the file , Baidu logo
        URL url = new URL("https://www.baidu.com/img/flexible/logo/plus_logo_web_2.png");
        File file = new File("file.png");

        // Calculation md5
        MessageDigest md5Digest = MessageDigest.getInstance("md5");
        try (InputStream in = url.openStream(); OutputStream out = new FileOutputStream(file)) {
            int len;
            byte[] buffer = new byte[1024 * 4];
            while ((len = in.read(buffer)) != -1) {
                // Update hash value 
                md5Digest.update(buffer, 0, len);
                // write file 
                out.write(buffer, 0, len);
            }
        }
        // Hash value array 
        byte[] digest = md5Digest.digest();
        //1 Indicates that this is an unsigned integer 
        BigInteger bigInteger = new BigInteger(1, digest);
        // With 16 Output in hexadecimal form 
        System.out.println(bigInteger.toString(16));
    }
}

The above is the way to avoid additional read streams md5, Of course, it can also be extended to other hash Algorithm , for example sha256 etc. .

版权声明
本文为[The end of time]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/09/20210909132620853E.html

随机推荐