Java Checksum Calculate getChecksum(byte[] buffer, int offset, int length)

Here you can find the source of getChecksum(byte[] buffer, int offset, int length)

Description

get Checksum

License

Open Source License

Declaration

public static long getChecksum(byte[] buffer, int offset, int length) 

Method Source Code

//package com.java2s;
/**/*  ww w. j  av a 2  s .  c  o  m*/
 * Classe di utilities varie.
 * 
 * <br><br>License:    GNU General Public License<br>
 * 
 * @author     Pier Paolo Ciarravano  
 * @version     Vers. 0.98 (29/09/2009) 
 */

import java.io.ByteArrayInputStream;

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

public class Main {
    public static long getChecksum(byte[] buffer, int offset, int length) {
        long result = 0;
        try {

            ByteArrayInputStream bais = new ByteArrayInputStream(buffer, offset, length);
            CheckedInputStream cis = new CheckedInputStream(bais, new Adler32());
            byte readBuffer[] = new byte[length];
            cis.read(readBuffer, 0, length);
            result = cis.getChecksum().getValue();

            //byte readBuffer[] = new byte[5];
            //while (cis.read(readBuffer) >= 0) {
            //   long value = cis.getChecksum().getValue();
            //   System.out.println("The value of checksum is " + value);
            //}
        } catch (Exception e) {

            System.out.println("Exception has been caught" + e);
        }
        return result;

    }
}

Related

  1. createChecksum(File file, String shaAlgo)
  2. generateChecksum(InputStream is)
  3. generateChecksum(String path, String flavor)
  4. generateChecksum(String pType, String pInFile)
  5. generateCheckSum(String string)
  6. getChecksum(final File file)
  7. getCheckSum(String filePath)
  8. getChecksumAlder32(File file)
  9. getCheckSums(String archiveFile)