Example usage for java.util.zip Adler32 Adler32

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

Introduction

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

Prototype

public Adler32() 

Source Link

Document

Creates a new Adler32 object.

Usage

From source file:MultiFileChecksumHelper.java

public static long getChecksum(File[] files) {
    CheckedInputStream cis = null;
    FileInputStream is = null;/*from  w w w.j  a va  2 s . c  o m*/
    Checksum checksum = new Adler32();
    byte[] tempBuf = new byte[128];

    for (int i = 0; i < files.length && files[i] != null && files[i].exists() && files[i].isFile(); i++) {
        try {
            is = new FileInputStream(files[i]);
            cis = new CheckedInputStream(is, checksum);
            while (cis.read(tempBuf) >= 0) {
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (cis != null) {
                try {
                    cis.close();
                } catch (IOException ioe) {
                }
                cis = null;
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException ioe) {
                }
                is = null;
            }
        }
    }
    return checksum.getValue();
}

From source file:org.broadleafcommerce.common.util.StringUtil.java

public static long getChecksum(String test) {
    try {/*ww w . ja v  a  2  s  . com*/
        byte buffer[] = test.getBytes();
        ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
        CheckedInputStream cis = new CheckedInputStream(bais, new Adler32());
        byte readBuffer[] = new byte[buffer.length];
        cis.read(readBuffer);
        return cis.getChecksum().getValue();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.yccheok.jstock.analysis.Utils.java

public static long getChecksum(File file) {
    FileInputStream stream = null;
    CheckedInputStream cis = null;
    try {/*from   ww w .j a  va  2 s.  c o m*/
        // Compute Adler-32 checksum
        stream = new FileInputStream(file);
        cis = new CheckedInputStream(stream, new Adler32());
        byte[] tempBuf = new byte[128];
        while (cis.read(tempBuf) >= 0) {
        }
        long checksum = cis.getChecksum().getValue();
        return checksum;
    } catch (IOException ex) {
        log.error(null, ex);
    } finally {
        org.yccheok.jstock.file.Utils.close(cis);
        org.yccheok.jstock.file.Utils.close(stream);
    }
    return 0;
}

From source file:edu.cornell.med.icb.util.SimpleChecksum.java

/**
 * Takes a string and returns a that same string
 * with two additional characters which are a simple
 * checksum, each char will be "A".."Z". An empty
 * (no characters) or null string will just be
 * returned with no change./* w w  w .  ja v  a 2  s .co m*/
 * @param valToChecksum the string to add the checksum to
 * @return the string with the checksum
 */
public static String simpleChecksum(final String valToChecksum) {
    if (StringUtils.isEmpty(valToChecksum)) {
        return valToChecksum;
    }

    final Adler32 adler = new Adler32();
    adler.update(valToChecksum.getBytes());
    final long[] result = splitLong(adler.getValue());

    return String.format("%s%c%c", valToChecksum, CHECKSUM_CHARS[(int) result[0]],
            CHECKSUM_CHARS[(int) result[1]]);
}

From source file:ChecksumCalculator.java

public static long compute(byte[] data, int start, int length) {
    Checksum checksum = new Adler32();
    checksum.update(data, start, length);
    return checksum.getValue();
}

From source file:com.hadoop.compression.lzo.LzopOutputStream.java

/**
 * Write an lzop-compatible header to the OutputStream provided.
 *//*from www . ja  va  2 s . c om*/
protected static void writeLzopHeader(OutputStream out, LzoCompressor.CompressionStrategy strategy)
        throws IOException {
    DataOutputBuffer dob = new DataOutputBuffer();
    try {
        dob.writeShort(LzopCodec.LZOP_VERSION);
        dob.writeShort(LzoCompressor.LZO_LIBRARY_VERSION);
        dob.writeShort(LzopCodec.LZOP_COMPAT_VERSION);
        switch (strategy) {
        case LZO1X_1:
            dob.writeByte(1);
            dob.writeByte(5);
            break;
        case LZO1X_15:
            dob.writeByte(2);
            dob.writeByte(1);
            break;
        case LZO1X_999:
            dob.writeByte(3);
            dob.writeByte(9);
            break;
        default:
            throw new IOException("Incompatible lzop strategy: " + strategy);
        }
        dob.writeInt(0); // all flags 0
        dob.writeInt(0x81A4); // mode
        dob.writeInt((int) (System.currentTimeMillis() / 1000)); // mtime
        dob.writeInt(0); // gmtdiff ignored
        dob.writeByte(0); // no filename
        Adler32 headerChecksum = new Adler32();
        headerChecksum.update(dob.getData(), 0, dob.getLength());
        int hc = (int) headerChecksum.getValue();
        dob.writeInt(hc);
        out.write(LzopCodec.LZO_MAGIC);
        out.write(dob.getData(), 0, dob.getLength());
    } finally {
        dob.close();
    }
}

From source file:org.codice.ddf.checksum.impl.Adler32ChecksumProvider.java

@Override
public String calculateChecksum(InputStream inputStream) throws IOException, NoSuchAlgorithmException {

    if (inputStream == null) {
        throw new IllegalArgumentException("InputStream cannot be null");
    }/*from w ww . j  a v  a 2  s. com*/

    long checksumValue = 0L;

    try (CheckedInputStream cis = new CheckedInputStream(inputStream, new Adler32())) {
        IOUtils.toByteArray(cis);
        checksumValue = cis.getChecksum().getValue();
    }

    return Long.toHexString(checksumValue);

}

From source file:de.fu_berlin.inf.dpp.core.util.FileUtils.java

/**
 * Calculate Adler32 checksum for given file.
 *
 * @return checksum of file//w w w.  j a v  a2  s. c  om
 * @throws IOException if checksum calculation has been failed.
 */
public static long checksum(IFile file) throws IOException {

    InputStream in;
    try {
        in = file.getContents();
    } catch (IOException e) {
        throw new IOException("failed to calculate checksum.", e);
    }

    byte[] buffer = new byte[BUFFER_SIZE];

    Adler32 adler = new Adler32();

    int read;

    try {
        while ((read = in.read(buffer)) != -1) {
            adler.update(buffer, 0, read);
        }
    } finally {
        IOUtils.closeQuietly(in);
    }

    return adler.getValue();
}

From source file:org.openmrs.module.sync.serialization.ZipPackage.java

public ZipPackage(File root, String target) {
    try {/*w  w  w .j av a 2  s  .co m*/
        this.rootName = root.getAbsolutePath();
        this.targetName = target;
        File archiveFolder = new File(root, "archive");
        archiveFolder.mkdir();
        File f = new File(archiveFolder, targetName);
        f.mkdir();
        if (!f.exists())
            f.mkdirs();
        File outputStreamDir = new File(f,
                targetName + "_" + SyncConstants.SYNC_FILENAME_MASK.format(new Date()) + ".zip");
        this.dest = new FileOutputStream(outputStreamDir);
        this.checksum = new CheckedOutputStream(dest, new Adler32());
        this.out = new ZipOutputStream(new BufferedOutputStream(checksum));
        this.data = new byte[BUFFER];
    } catch (Exception e) {
        log.error(e.toString());
    }
}

From source file:AbstractFileSystemHelper.java

public static long getChecksum(InputStream is) {
    CheckedInputStream cis = null;
    long checksum = 0;
    try {//  w  ww. j a v a2 s  . c  om
        cis = new CheckedInputStream(is, new Adler32());
        byte[] tempBuf = new byte[128];
        while (cis.read(tempBuf) >= 0) {
        }
        checksum = cis.getChecksum().getValue();
    } catch (IOException e) {
        checksum = 0;
    } finally {
        if (cis != null) {
            try {
                cis.close();
            } catch (IOException ioe) {
            }
        }
    }
    return checksum;
}