Java Checksum Calculate calculateChecksum(InputStream is, String algorithm)

Here you can find the source of calculateChecksum(InputStream is, String algorithm)

Description

Calculates checksum, closing the inputstream in the end.

License

LGPL

Declaration

public static String calculateChecksum(InputStream is, String algorithm)
        throws NoSuchAlgorithmException, IOException 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.io.IOException;
import java.io.InputStream;

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

import javax.xml.bind.DatatypeConverter;

public class Main {
    /**// w w  w .j av a2s.c o m
     * Calculates checksum, closing the inputstream in the end.
     */
    public static String calculateChecksum(InputStream is, String algorithm)
            throws NoSuchAlgorithmException, IOException {
        MessageDigest digester = MessageDigest.getInstance(algorithm);
        try {
            byte[] block = new byte[4096];
            int length;
            while ((length = is.read(block)) > 0) {
                digester.update(block, 0, length);
            }
        } finally {
            is.close();
        }

        return DatatypeConverter.printHexBinary(digester.digest());
    }
}

Related

  1. calcChecksum(byte[] buffer, int offset, int length)
  2. calcChecksum(byte[] buffer, int start, int end)
  3. calcCheckSum(byte[] bytes, int num)
  4. calcCheckSum(byte[] data, int offset, int maxIdx)
  5. calcChecksum(String value, int length)
  6. calculateChecksums(Optional zos, InputStream inputStream, Set checksumAlgorithms)
  7. checkSum(boolean[] a)
  8. checkSum(byte abyte0[])
  9. checksum(byte current, final byte[] data, final int offset, final int length)