Java MD5 inputStreamMD5(InputStream entity)

Here you can find the source of inputStreamMD5(InputStream entity)

Description

Computes the MD5 of an InputStream.

License

Open Source License

Parameter

Parameter Description
entity the InputStream whose contents to checksum.

Return

the MD5 hex string.

Declaration

public static String inputStreamMD5(InputStream entity) 

Method Source Code


//package com.java2s;
/*//from   w  w  w  .  ja  v a  2  s .c  o  m
 * Copyright 2015 EMC Corporation. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 * http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

import javax.xml.bind.DatatypeConverter;
import java.io.IOException;
import java.io.InputStream;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    private static final int MAX_MARK = 256 * 1024 * 1024;

    /**
     * Computes the MD5 of an InputStream.  The Input stream must support mark/reset and there must not be more than
     * MAX_MARK bytes to checksum.
     * @param entity the InputStream whose contents to checksum.
     * @return the MD5 hex string.
     */
    public static String inputStreamMD5(InputStream entity) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            // Should never happen
            throw new RuntimeException("Could not load MD5", e);
        }

        entity.mark(MAX_MARK);
        byte[] buffer = new byte[16 * 1024];
        int c = 0;
        try {
            while ((c = entity.read(buffer)) != -1) {
                md5.update(buffer, 0, c);
            }
        } catch (IOException e) {
            throw new RuntimeException("Error reading InputStream to MD5");
        }

        try {
            entity.reset();
        } catch (IOException e) {
            throw new RuntimeException("Could not reset InputStream!", e);
        }

        return toHexString(md5.digest());
    }

    /**
     * Converts a byte array to a hex string.
     * @param arr the array to convert.
     * @return the array's contents as a string of hex characters.
     */
    public static String toHexString(byte[] arr) {
        return DatatypeConverter.printHexBinary(arr);
    }
}

Related

  1. getMD5Checksum(String path)
  2. getMd5DigestHex(final String content)
  3. getMD5Hex(byte[] digest)
  4. getMD5Prefix()
  5. getMd5Script(String dstFilePath)
  6. isEtagAlsoAnMD5Hash(String etag)
  7. looksLikeValidMd5(String possibleMd5)
  8. md5(File f)
  9. md5(final String text)