Java Checksum Calculate checksum(String filename)

Here you can find the source of checksum(String filename)

Description

Calculate a checksum for a file, based on the Adler32 algorithm invented by Mark Adler.

License

Open Source License

Parameter

Parameter Description
filename The name of the file to be checksummed.

Exception

Parameter Description
IOException the IO exception

Return

The Adler32 checksum.

Declaration

public static long checksum(String filename) throws IOException 

Method Source Code

//package com.java2s;
/*-/*ww w  .  jav  a2 s.  c o m*/
 * Copyright ? 2009 Diamond Light Source Ltd., Science and Technology
 * Facilities Council Daresbury Laboratory
 *
 * This file is part of GDA.
 *
 * GDA is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License version 3 as published by the Free
 * Software Foundation.
 *
 * GDA is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along
 * with GDA. If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.FileInputStream;

import java.io.IOException;

import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;

public class Main {
    /**
     * Calculate a checksum for a file, based on the Adler32 algorithm invented by Mark Adler. This is almost as
     * reliable as a 32-bit cyclic redundancy check for protecting against accidental modification of data.
     * 
     * @param filename
     *            The name of the file to be checksummed.
     * @return The Adler32 checksum.
     * @throws IOException
     *             the IO exception
     */
    public static long checksum(String filename) throws IOException {
        CheckedInputStream cis = new CheckedInputStream(new FileInputStream(filename), new Adler32());
        byte[] buffer = new byte[128];
        while (cis.read(buffer) >= 0)
            continue;
        return cis.getChecksum().getValue();
    }
}

Related

  1. checksum(byte[] message, int offset, int count)
  2. checksum(File file)
  3. checksum(File file)
  4. checksum(InputStream is)
  5. checksum(InputStream is)
  6. checksum(String nmea)
  7. checksum_icmpv6(byte pkt[])
  8. checksumBytesToString(byte[] digestBytes)
  9. checksumFile(File file)