Here you can find the source of getChecksum(final File file)
Parameter | Description |
---|---|
file | the File to calculate the checksum from |
public static long getChecksum(final File file)
//package com.java2s; //License from project: Apache License import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.zip.Adler32; import java.util.zip.CheckedInputStream; public class Main { /**//from w w w . jav a 2 s. c o m * Returns the {@link Adler32} checksum of the contents of a given * {@link File}. * * @param file * the {@link File} to calculate the checksum from * @return the resulting checksum as {@code long} value */ public static long getChecksum(final File file) { if (file.isDirectory()) { return 0L; } CheckedInputStream cis = null; try { cis = new CheckedInputStream(new FileInputStream(file), new Adler32()); final byte[] tempBuf = new byte[8192]; final Thread currentThread = Thread.currentThread(); while (cis.read(tempBuf) >= 0) { if (currentThread.isInterrupted()) { return 0L; } } return cis.getChecksum().getValue(); } catch (final IOException e) { return 0L; } finally { if (cis != null) { try { cis.close(); } catch (final IOException e) { } } } } }