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:com.petercho.Encoder.java

public static long checksumAdler32(String text) {
    Adler32 crc = new Adler32();
    try (InputStream is = new ByteArrayInputStream(text.getBytes(DEFAULT_ENCODING))) {
        checksum(is, crc);// w  w  w  .  j av  a2s  . c  o  m
        return crc.getValue();
    } catch (IOException e) {
        throw new IllegalStateException("Error getting checksum, e:" + e, e);
    }
}

From source file:org.anarres.lzo.LzopInputStream.java

public LzopInputStream(InputStream in) throws IOException {
    super(in, new LzoDecompressor1x());
    this.flags = readHeader();
    this.c_crc32_c = ((flags & LzopConstants.F_CRC32_C) == 0) ? null : new CRC32();
    this.c_crc32_d = ((flags & LzopConstants.F_CRC32_D) == 0) ? null : new CRC32();
    this.c_adler32_c = ((flags & LzopConstants.F_ADLER32_C) == 0) ? null : new Adler32();
    this.c_adler32_d = ((flags & LzopConstants.F_ADLER32_D) == 0) ? null : new Adler32();
    this.eof = false;
    // logState();
}

From source file:com.sqli.liferay.imex.util.xml.ZipUtils.java

public void _unzipArchive(File archive, File outputDir) throws IOException {
    BufferedOutputStream dest = null;
    FileInputStream fis = new FileInputStream(archive);
    CheckedInputStream checksum = new CheckedInputStream(fis, new Adler32());
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(checksum));
    ZipEntry entry;/*  ww  w .  j  a  v  a2 s  .  co  m*/
    while ((entry = zis.getNextEntry()) != null) {
        log.debug("Extracting: " + entry);
        int count;
        byte data[] = new byte[BUFFER];
        // write the files to the disk
        FileOutputStream fos = new FileOutputStream(entry.getName());
        dest = new BufferedOutputStream(fos, BUFFER);
        while ((count = zis.read(data, 0, BUFFER)) != -1) {
            dest.write(data, 0, count);
        }
        dest.flush();
        dest.close();
    }
    zis.close();
    log.debug("Checksum:" + checksum.getChecksum().getValue());

}

From source file:org.anarres.lzo.LzopOutputStream.java

/**
 * Constructs a new LzopOutputStream.//from   www . j ava  2s  .  co  m
 *
 * I recommend limiting flags to the following unless you REALLY know what
 * you are doing:
 * <ul>
 * <li>{@link LzopConstants#F_ADLER32_C}</li>
 * <li>{@link LzopConstants#F_ADLER32_D}</li>
 * <li>{@link LzopConstants#F_CRC32_C}</li>
 * <li>{@link LzopConstants#F_CRC32_D}</li>
 * </ul>
 */
public LzopOutputStream(OutputStream out, LzoCompressor compressor, int inputBufferSize, long flags)
        throws IOException {
    super(out, compressor, inputBufferSize);
    this.flags = flags;
    this.c_crc32_c = ((flags & LzopConstants.F_CRC32_C) == 0) ? null : new CRC32();
    this.c_crc32_d = ((flags & LzopConstants.F_CRC32_D) == 0) ? null : new CRC32();
    this.c_adler32_c = ((flags & LzopConstants.F_ADLER32_C) == 0) ? null : new Adler32();
    this.c_adler32_d = ((flags & LzopConstants.F_ADLER32_D) == 0) ? null : new Adler32();
    writeLzopHeader();
}

From source file:org.theospi.portfolio.presentation.export.PresentationExport.java

public void createZip(OutputStream out) throws IOException {
    File directory = new File(tempDirectory + webappName);

    CheckedOutputStream checksum = null;
    ZipOutputStream zos = null;/*from  w ww.  ja va  2  s  . com*/
    try {
        checksum = new CheckedOutputStream(out, new Adler32());
        zos = new ZipOutputStream(new BufferedOutputStream(checksum));
        recurseDirectory("", directory, zos);

        zos.finish();
        zos.flush();
    } finally {
        if (zos != null) {
            try {
                zos.close();
            } catch (IOException e) {
            }
        }
        if (checksum != null) {
            try {
                checksum.close();
            } catch (IOException e) {
            }
        }
    }

}

From source file:com.sqli.liferay.imex.util.xml.ZipUtils.java

public void zipFiles(File archive, File inputDir) throws IOException {

    FileOutputStream dest = new FileOutputStream(archive);
    CheckedOutputStream checksum = new CheckedOutputStream(dest, new Adler32());
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(checksum));

    zipEntries(out, inputDir, inputDir);

    out.close();/*from  w  w  w.j a  v a 2s .c om*/
    log.debug("checksum:" + checksum.getChecksum().getValue());

}

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

/**
 * Read and verify an lzo header, setting relevant block checksum options
 * and ignoring most everything else./*from ww w .j ava  2 s.co  m*/
 * @param in InputStream
 * @throws IOException if there is a error in lzo header
 */
protected void readHeader(InputStream in) throws IOException {
    readFully(in, buf, 0, 9);
    if (!Arrays.equals(buf, LzopCodec.LZO_MAGIC)) {
        throw new IOException("Invalid LZO header");
    }
    Arrays.fill(buf, (byte) 0);
    Adler32 adler = new Adler32();
    CRC32 crc32 = new CRC32();
    int hitem = readHeaderItem(in, buf, 2, adler, crc32); // lzop version
    if (hitem > LzopCodec.LZOP_VERSION) {
        LOG.debug("Compressed with later version of lzop: " + Integer.toHexString(hitem) + " (expected 0x"
                + Integer.toHexString(LzopCodec.LZOP_VERSION) + ")");
    }
    hitem = readHeaderItem(in, buf, 2, adler, crc32); // lzo library version
    if (hitem < LzoDecompressor.MINIMUM_LZO_VERSION) {
        throw new IOException("Compressed with incompatible lzo version: 0x" + Integer.toHexString(hitem)
                + " (expected at least 0x" + Integer.toHexString(LzoDecompressor.MINIMUM_LZO_VERSION) + ")");
    }
    hitem = readHeaderItem(in, buf, 2, adler, crc32); // lzop extract version
    if (hitem > LzopCodec.LZOP_VERSION) {
        throw new IOException("Compressed with incompatible lzop version: 0x" + Integer.toHexString(hitem)
                + " (expected 0x" + Integer.toHexString(LzopCodec.LZOP_VERSION) + ")");
    }
    hitem = readHeaderItem(in, buf, 1, adler, crc32); // method
    if (hitem < 1 || hitem > 3) {
        throw new IOException("Invalid strategy: " + Integer.toHexString(hitem));
    }
    readHeaderItem(in, buf, 1, adler, crc32); // ignore level

    // flags
    hitem = readHeaderItem(in, buf, 4, adler, crc32);
    try {
        for (DChecksum f : dflags) {
            if (0 == (f.getHeaderMask() & hitem)) {
                dflags.remove(f);
            } else {
                dcheck.put(f, (int) f.getChecksumClass().newInstance().getValue());
            }
        }
        for (CChecksum f : cflags) {
            if (0 == (f.getHeaderMask() & hitem)) {
                cflags.remove(f);
            } else {
                ccheck.put(f, (int) f.getChecksumClass().newInstance().getValue());
            }
        }
    } catch (InstantiationException e) {
        throw new RuntimeException("Internal error", e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException("Internal error", e);
    }
    ((LzopDecompressor) decompressor).initHeaderFlags(dflags, cflags);
    boolean useCRC32 = 0 != (hitem & 0x00001000); // F_H_CRC32
    boolean extraField = 0 != (hitem & 0x00000040); // F_H_EXTRA_FIELD
    if (0 != (hitem & 0x400)) { // F_MULTIPART
        throw new IOException("Multipart lzop not supported");
    }
    if (0 != (hitem & 0x800)) { // F_H_FILTER
        throw new IOException("lzop filter not supported");
    }
    if (0 != (hitem & 0x000FC000)) { // F_RESERVED
        throw new IOException("Unknown flags in header");
    }
    // known !F_H_FILTER, so no optional block

    readHeaderItem(in, buf, 4, adler, crc32); // ignore mode
    readHeaderItem(in, buf, 4, adler, crc32); // ignore mtime
    readHeaderItem(in, buf, 4, adler, crc32); // ignore gmtdiff
    hitem = readHeaderItem(in, buf, 1, adler, crc32); // fn len
    if (hitem > 0) {
        // skip filename
        int filenameLen = Math.max(4, hitem); // buffer must be at least 4 bytes for readHeaderItem to work.
        readHeaderItem(in, new byte[filenameLen], hitem, adler, crc32);
    }
    int checksum = (int) (useCRC32 ? crc32.getValue() : adler.getValue());
    hitem = readHeaderItem(in, buf, 4, adler, crc32); // read checksum
    if (hitem != checksum) {
        throw new IOException("Invalid header checksum: " + Long.toHexString(checksum) + " (expected 0x"
                + Integer.toHexString(hitem) + ")");
    }
    if (extraField) { // lzop 1.08 ultimately ignores this
        LOG.debug("Extra header field not processed");
        adler.reset();
        crc32.reset();
        hitem = readHeaderItem(in, buf, 4, adler, crc32);
        readHeaderItem(in, new byte[hitem], hitem, adler, crc32);
        checksum = (int) (useCRC32 ? crc32.getValue() : adler.getValue());
        if (checksum != readHeaderItem(in, buf, 4, adler, crc32)) {
            throw new IOException("Invalid checksum for extra header field");
        }
    }
}

From source file:nl.nn.adapterframework.pipes.ChecksumPipe.java

protected ChecksumGenerator createChecksumGenerator() throws NoSuchAlgorithmException {
    if (CHECKSUM_MD5.equals(getType())) {
        return new MessageDigestChecksumGenerator(getType());
    } else if (CHECKSUM_CRC32.equals(getType())) {
        return new ZipChecksumGenerator(new CRC32());
    } else if (CHECKSUM_ADLER32.equals(getType())) {
        return new ZipChecksumGenerator(new Adler32());
    } else if (CHECKSUM_SHA.equals(getType())) {
        return new MessageDigestChecksumGenerator(getType());
    } else {/*from ww w  . j  a  va 2 s  . c om*/
        throw new NoSuchAlgorithmException("unsupported algorithm [" + getType() + "]");
    }
}

From source file:org.anarres.lzo.LzopInputStream.java

/**
 * Read and verify an lzo header, setting relevant block checksum options
 * and ignoring most everything else.//from   w  w  w . j a v a2 s  .  c  o m
 */
protected int readHeader() throws IOException {
    byte[] buf = new byte[9];
    readBytes(buf, 0, 9);
    if (!Arrays.equals(buf, LzopConstants.LZOP_MAGIC))
        throw new IOException("Invalid LZO header");
    Arrays.fill(buf, (byte) 0);
    Adler32 adler = new Adler32();
    CRC32 crc32 = new CRC32();
    int hitem = readHeaderItem(buf, 2, adler, crc32); // lzop version
    if (hitem > LzopConstants.LZOP_VERSION) {
        LOG.debug("Compressed with later version of lzop: " + Integer.toHexString(hitem) + " (expected 0x"
                + Integer.toHexString(LzopConstants.LZOP_VERSION) + ")");
    }
    hitem = readHeaderItem(buf, 2, adler, crc32); // lzo library version
    if (hitem > LzoVersion.LZO_LIBRARY_VERSION) {
        throw new IOException("Compressed with incompatible lzo version: 0x" + Integer.toHexString(hitem)
                + " (expected 0x" + Integer.toHexString(LzoVersion.LZO_LIBRARY_VERSION) + ")");
    }
    hitem = readHeaderItem(buf, 2, adler, crc32); // lzop extract version
    if (hitem > LzopConstants.LZOP_VERSION) {
        throw new IOException("Compressed with incompatible lzop version: 0x" + Integer.toHexString(hitem)
                + " (expected 0x" + Integer.toHexString(LzopConstants.LZOP_VERSION) + ")");
    }
    hitem = readHeaderItem(buf, 1, adler, crc32); // method
    switch (hitem) {
    case LzopConstants.M_LZO1X_1:
    case LzopConstants.M_LZO1X_1_15:
    case LzopConstants.M_LZO1X_999:
        break;
    default:
        throw new IOException("Invalid strategy " + Integer.toHexString(hitem));
    }
    readHeaderItem(buf, 1, adler, crc32); // ignore level

    // flags
    int flags = readHeaderItem(buf, 4, adler, crc32);
    boolean useCRC32 = (flags & LzopConstants.F_H_CRC32) != 0;
    boolean extraField = (flags & LzopConstants.F_H_EXTRA_FIELD) != 0;
    if ((flags & LzopConstants.F_MULTIPART) != 0)
        throw new IOException("Multipart lzop not supported");
    if ((flags & LzopConstants.F_H_FILTER) != 0)
        throw new IOException("lzop filter not supported");
    if ((flags & LzopConstants.F_RESERVED) != 0)
        throw new IOException("Unknown flags in header");
    // known !F_H_FILTER, so no optional block

    readHeaderItem(buf, 4, adler, crc32); // ignore mode
    readHeaderItem(buf, 4, adler, crc32); // ignore mtime
    readHeaderItem(buf, 4, adler, crc32); // ignore gmtdiff
    hitem = readHeaderItem(buf, 1, adler, crc32); // fn len
    if (hitem > 0) {
        byte[] tmp = (hitem > buf.length) ? new byte[hitem] : buf;
        readHeaderItem(tmp, hitem, adler, crc32); // skip filename
    }
    int checksum = (int) (useCRC32 ? crc32.getValue() : adler.getValue());
    hitem = readHeaderItem(buf, 4, adler, crc32); // read checksum
    if (hitem != checksum) {
        throw new IOException("Invalid header checksum: " + Long.toHexString(checksum) + " (expected 0x"
                + Integer.toHexString(hitem) + ")");
    }
    if (extraField) { // lzop 1.08 ultimately ignores this
        LOG.debug("Extra header field not processed");
        adler.reset();
        crc32.reset();
        hitem = readHeaderItem(buf, 4, adler, crc32);
        readHeaderItem(new byte[hitem], hitem, adler, crc32);
        checksum = (int) (useCRC32 ? crc32.getValue() : adler.getValue());
        if (checksum != readHeaderItem(buf, 4, adler, crc32)) {
            throw new IOException("Invalid checksum for extra header field");
        }
    }

    return flags;
}

From source file:org.olat.upgrade.OLATUpgrade_9_0_0.java

private long checksum(byte[] binaryDatas) throws IOException {
    InputStream in = null;/*w w w.j  av a  2s  .  c  om*/
    Adler32 checksum = new Adler32();
    try {
        in = new CheckedInputStream(new ByteArrayInputStream(binaryDatas), checksum);
        IOUtils.copy(in, new NullOutputStream());
    } finally {
        IOUtils.closeQuietly(in);
    }
    return checksum.getValue();
}