Here you can find the source of checksum(String filename)
Parameter | Description |
---|---|
filename | The name of the file to be checksummed. |
Parameter | Description |
---|---|
IOException | the IO exception |
public static long checksum(String filename) throws IOException
//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(); } }