Example usage for java.util.zip CRC32 CRC32

List of usage examples for java.util.zip CRC32 CRC32

Introduction

In this page you can find the example usage for java.util.zip CRC32 CRC32.

Prototype

public CRC32() 

Source Link

Document

Creates a new CRC32 object.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    byte[] bytes = "some data".getBytes();

    // Compute Adler-32 checksum
    Checksum checksumEngine = new CRC32();
    checksumEngine.update(bytes, 0, bytes.length);
    long checksum = checksumEngine.getValue();

    checksumEngine.reset();// ww w.j  av  a  2s . co  m
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    FileInputStream fin = new FileInputStream("a.zip");
    Checksum cs = new CRC32();
    for (int b = fin.read(); b != -1; b = fin.read()) {
        cs.update(b);/*from   w w  w  .  j a v a2  s .  c o  m*/
    }
    System.out.println(cs.getValue());
    fin.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    BufferedInputStream is = new BufferedInputStream(new FileInputStream("a.exe"));
    byte[] bytes = new byte[1024];
    int len = 0;//from ww w . j av a 2 s. c o  m

    while ((len = is.read(bytes)) >= 0) {
        new CRC32().update(bytes, 0, len);
    }
    is.close();
    System.out.println(Arrays.toString(bytes));

}

From source file:Main.java

public static long getStringCRC(String localData) {
    if (localData == null)
        return 0;
    CRC32 crc = new CRC32();
    crc.update(localData.getBytes());/*from   w  w w  . ja v  a  2 s .com*/
    return crc.getValue();
}

From source file:Main.java

public static long doChecksum(String text) throws UnsupportedEncodingException {
    byte bytes[] = text.getBytes("UTF-8"); // This makes this hash function platform independent
    Checksum checksum = new CRC32();
    checksum.update(bytes, 0, bytes.length);
    long lngChecksum = checksum.getValue();
    return lngChecksum;
}

From source file:Main.java

public static long getCRC32(InputStream in) throws IOException {
    Checksum cs = new CRC32();

    for (int b = in.read(); b != -1; b = in.read()) {
        cs.update(b);/*from   www.  j  av a 2s.c  o m*/
    }
    return cs.getValue();
}

From source file:MainClass.java

public static long getCRC32(InputStream in) throws IOException {

    Checksum cs = new CRC32();

    // more efficient to read chunks of data at a time
    for (int b = in.read(); b != -1; b = in.read()) {
        cs.update(b);/* w  w  w  . j a v  a 2s .c o  m*/
    }
    return cs.getValue();
}

From source file:Main.java

public static String getCRC32(File file) {
    CRC32 crc32 = new CRC32();
    CheckedInputStream checkedinputstream = null;
    String crc = null;/* w  w  w . j a  v a  2s.co m*/
    try {
        checkedinputstream = new CheckedInputStream(new FileInputStream(file), crc32);
        byte[] buf = new byte[1024];
        while (checkedinputstream.read(buf) >= 0) {
        }
        crc = Long.toHexString(crc32.getValue()).toUpperCase();
    } catch (Exception e) {
        Log.w(TAG, e);
        Log.e("WxException", e.getMessage(), e);
    } finally {
        if (checkedinputstream != null) {
            try {
                checkedinputstream.close();
            } catch (IOException e) {
            }
        }
    }
    return crc;
}

From source file:com.esri.geoportal.harvester.api.base.SimpleScrambler.java

/**
 * Encodes string.//from  ww  w. j  a  v a2 s  . c o  m
 * @param txt string to encode
 * @return encoded string or <code>null</code> if error encoding string
 */
public static String encode(String txt) {
    txt = StringUtils.defaultIfEmpty(txt, "");
    try {
        CRC32 crC32 = new CRC32();
        crC32.update(txt.getBytes("UTF-8"));
        long crc = crC32.getValue();
        String crctxt = String.format("%10d%s", crc, txt);
        Base64.Encoder encoder = Base64.getEncoder();
        return encoder.encodeToString(crctxt.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException ex) {
        return null;
    }
}

From source file:CRC32HashBuilder.java

/**
 * {@inheritDoc}/*  w  w  w.  j a va2  s  .  c  o m*/
 */
public String getHash(final InputStream input) throws IOException {
    if (input == null) {
        throw new IllegalArgumentException("Content cannot be null!");
    }
    final Checksum checksum = new CRC32();
    final byte[] bytes = new byte[1024];
    int len = 0;
    while ((len = input.read(bytes)) >= 0) {
        checksum.update(bytes, 0, len);
    }

    final String hash = new BigInteger(Long.toString(checksum.getValue())).toString(16);
    return hash;
}