Example usage for java.util.zip Adler32 update

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

Introduction

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

Prototype

@Override
public void update(byte[] b, int off, int len) 

Source Link

Document

Updates the checksum with the specified array of bytes.

Usage

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

/**
 * Write an lzop-compatible header to the OutputStream provided.
 *///from w ww.  j a  v  a  2  s.  c  o m
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:com.hadoop.compression.lzo.LzopInputStream.java

/**
 * Read bytes, update checksums, return first four bytes as an int, first
 * byte read in the MSB./*ww  w  . j  a  va  2s.  c  o m*/
 */
private static int readHeaderItem(InputStream in, byte[] buf, int len, Adler32 adler, CRC32 crc32)
        throws IOException {
    int ret = readInt(in, buf, len);
    adler.update(buf, 0, len);
    crc32.update(buf, 0, len);
    Arrays.fill(buf, (byte) 0);
    return ret;
}

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  ava2s .  co m*/
 * @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:es.mityc.firmaJava.libreria.utilidades.CopiaFicherosManager.java

/**
 * Comprueba si el fichero indicado es ntegro.
 * //from   w  ww . j av  a  2s  .co  m
 * @param file
 * @param crcValue
 * @param size
 * @return
 */
private boolean checkIntegrityFile(File file, long crcValue, long size)
        throws FileNotFoundException, IOException {
    if (log.isTraceEnabled())
        log.trace(I18n.getResource(ConstantesXADES.LIBRERIAXADES_FIRMAMOZILLA_INFO_6) + ConstantesXADES.ESPACIO
                + file.getAbsolutePath());
    if (!file.exists())
        return false;
    // Primero comprueba el tamao
    if (file.length() != size)
        return false;
    // despus comprueba el crc
    Adler32 crc = new Adler32();
    InputStream entrada = new BufferedInputStream(new FileInputStream(file), BUFFER_IN_SIZE);
    byte[] buffer = new byte[BUFFER_OUT_SIZE];
    int readed = entrada.read(buffer);
    while (readed > 0) {
        crc.update(buffer, 0, readed);
        readed = entrada.read(buffer);
    }
    if (log.isTraceEnabled())
        log.trace(I18n.getResource(ConstantesXADES.LIBRERIAXADES_FIRMAMOZILLA_INFO_7) + ConstantesXADES.ESPACIO
                + crc.getValue() + ConstantesXADES.ESPACIO
                + I18n.getResource(ConstantesXADES.LIBRERIAXADES_FIRMAMOZILLA_INFO_8) + ConstantesXADES.ESPACIO
                + crcValue);
    if (crc.getValue() != crcValue)
        return false;
    return true;
}

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

/**
 * Read bytes, update checksums, return first four bytes as an int, first
 * byte read in the MSB./*ww  w.j a  v  a 2  s  .  c om*/
 */
private int readHeaderItem(byte[] buf, int len, Adler32 adler, CRC32 crc32) throws IOException {
    int ret = readInt(buf, len);
    adler.update(buf, 0, len);
    crc32.update(buf, 0, len);
    Arrays.fill(buf, (byte) 0);
    return ret;
}

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

/**
 * Writes an lzop-compatible header to the OutputStream provided.
 *///from  w w w.  j  av  a  2 s.  c  o  m
protected void writeLzopHeader() throws IOException {
    DataOutputBuffer dob = new DataOutputBuffer();
    try {
        dob.writeShort(LzopConstants.LZOP_VERSION);
        dob.writeShort(LzoVersion.LZO_LIBRARY_VERSION);
        dob.writeShort(LzopConstants.LZOP_COMPAT_VERSION);
        switch (getAlgorithm()) {
        case LZO1X:
            // case LZO1X_1:
            dob.writeByte(LzopConstants.M_LZO1X_1);
            dob.writeByte(5);
            break;
        /*
        case LZO1X_15:
        dob.writeByte(LzopConstants.M_LZO1X_1_15);
        dob.writeByte(1);
        break;
        case LZO1X_999:
        dob.writeByte(LzopConstants.M_LZO1X_999);
        dob.writeByte(9);
        break;
         */
        default:
            throw new IOException("Incompatible lzop algorithm " + getAlgorithm());
        }
        long mask = LzopConstants.F_ADLER32_C | LzopConstants.F_ADLER32_D;
        mask = mask | LzopConstants.F_CRC32_C | LzopConstants.F_CRC32_D;
        dob.writeInt((int) (flags & mask & 0xFFFFFFFF)); // all flags 0
        dob.writeInt(33188); // 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(LzopConstants.LZOP_MAGIC);
        out.write(dob.getData(), 0, dob.getLength());
    } finally {
        dob.close();
    }
}