Example usage for java.util.zip CRC32 getValue

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

Introduction

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

Prototype

@Override
public long getValue() 

Source Link

Document

Returns CRC-32 value.

Usage

From source file:com.espertech.esper.core.context.mgr.ContextControllerHashedGetterCRC32Serialized.java

public Object get(EventBean eventBean) throws PropertyAccessException {
    EventBean[] events = new EventBean[] { eventBean };

    Object[] parameters = new Object[evaluators.length];
    for (int i = 0; i < serializers.length; i++) {
        parameters[i] = evaluators[i].evaluate(events, true, null);
    }/* w w  w .jav  a  2 s . com*/

    byte[] bytes;
    try {
        bytes = SerializerFactory.serialize(serializers, parameters);
    } catch (IOException e) {
        log.error("Exception serializing parameters for computing consistent hash for statement '"
                + statementName + "': " + e.getMessage(), e);
        bytes = new byte[0];
    }

    CRC32 crc = new CRC32();
    crc.update(bytes);
    long value = crc.getValue() % granularity;

    int result = (int) value;
    if (result >= 0) {
        return result;
    }
    return -result;
}

From source file:eionet.webq.service.RemoteFileServiceImpl.java

/**
 * Calculates crc32 checksum./*  w  w  w  . ja  v a2 s .c  o  m*/
 *
 * @param bytes bytes to calculate checksum.
 * @return checksum
 * @see java.util.zip.CRC32
 */
private long crc32Checksum(byte[] bytes) {
    CRC32 crc32 = new CRC32();
    crc32.update(bytes);
    return crc32.getValue();
}

From source file:org.apache.hadoop.raid.tools.FastFileCheck.java

/**
 * Verify the certain offset of a file./*  w  w  w .  ja v a  2s.c  o  m*/
 */
private static boolean verifyFile(Configuration conf, FileSystem srcFs, FileSystem parityFs, FileStatus stat,
        Path parityPath, Codec codec, long blockOffset, Progressable reporter)
        throws IOException, InterruptedException {
    Path srcPath = stat.getPath();
    LOG.info("Verify file: " + srcPath + " at offset: " + blockOffset);
    int limit = (int) Math.min(stat.getBlockSize(), DEFAULT_VERIFY_LEN);
    if (reporter == null) {
        reporter = RaidUtils.NULL_PROGRESSABLE;
    }

    // try to decode.
    Decoder decoder = new Decoder(conf, codec);
    if (codec.isDirRaid) {
        decoder.connectToStore(srcPath);
    }

    List<Long> errorOffsets = new ArrayList<Long>();
    // first limit bytes
    errorOffsets.add(blockOffset);
    long left = Math.min(stat.getBlockSize(), stat.getLen() - blockOffset);
    if (left > limit) {
        // last limit bytes
        errorOffsets.add(blockOffset + left - limit);
        // random limit bytes.
        errorOffsets.add(blockOffset + rand.nextInt((int) (left - limit)));
    }

    byte[] buffer = new byte[limit];
    FSDataInputStream is = srcFs.open(srcPath);
    try {
        for (long errorOffset : errorOffsets) {
            is.seek(errorOffset);
            is.read(buffer);
            // calculate the oldCRC.
            CRC32 oldCrc = new CRC32();
            oldCrc.update(buffer);

            CRC32 newCrc = new CRC32();
            DecoderInputStream stream = decoder.new DecoderInputStream(RaidUtils.NULL_PROGRESSABLE, limit,
                    stat.getBlockSize(), errorOffset, srcFs, srcPath, parityFs, parityPath, null, null, false);
            try {
                stream.read(buffer);
                newCrc.update(buffer);
                if (oldCrc.getValue() != newCrc.getValue()) {
                    LogUtils.logFileCheckMetrics(LOGRESULTS.FAILURE, codec, srcPath, srcFs, errorOffset, limit,
                            null, reporter);
                    LOG.error("mismatch crc, old " + oldCrc.getValue() + ", new " + newCrc.getValue()
                            + ", for file: " + srcPath + " at offset " + errorOffset + ", read limit " + limit);
                    return false;
                }
            } finally {
                reporter.progress();
                if (stream != null) {
                    stream.close();
                }
            }
        }
        return true;
    } finally {
        is.close();
    }
}

From source file:com.googlecode.flyway.core.migration.sql.SqlMigration.java

/**
 * Calculates the checksum of this sql script.
 *
 * @return The crc-32 checksum of the script.
 *//* w  ww .  j a v  a 2s  .com*/
private int calculateChecksum(String sql) {
    final CRC32 crc32 = new CRC32();
    crc32.update(sql.getBytes());
    return (int) crc32.getValue();
}

From source file:it.cnr.icar.eric.common.Utility.java

public static ZipOutputStream createZipOutputStream(String baseDir, String[] relativeFilePaths, OutputStream os)
        throws FileNotFoundException, IOException {
    if (baseDir.startsWith("file:/")) {
        baseDir = baseDir.substring(5);/* w  ww .j  av  a2  s. c  o m*/
    }
    ZipOutputStream zipoutputstream = new ZipOutputStream(os);

    zipoutputstream.setMethod(ZipOutputStream.STORED);

    for (int i = 0; i < relativeFilePaths.length; i++) {
        File file = new File(baseDir + FILE_SEPARATOR + relativeFilePaths[i]);

        byte[] buffer = new byte[1000];

        int n;

        FileInputStream fis;

        // Calculate the CRC-32 value.  This isn't strictly necessary
        //   for deflated entries, but it doesn't hurt.

        CRC32 crc32 = new CRC32();

        fis = new FileInputStream(file);

        while ((n = fis.read(buffer)) > -1) {
            crc32.update(buffer, 0, n);
        }

        fis.close();

        // Create a zip entry.

        ZipEntry zipEntry = new ZipEntry(relativeFilePaths[i]);

        zipEntry.setSize(file.length());
        zipEntry.setTime(file.lastModified());
        zipEntry.setCrc(crc32.getValue());

        // Add the zip entry and associated data.

        zipoutputstream.putNextEntry(zipEntry);

        fis = new FileInputStream(file);

        while ((n = fis.read(buffer)) > -1) {
            zipoutputstream.write(buffer, 0, n);
        }

        fis.close();

        zipoutputstream.closeEntry();
    }

    return zipoutputstream;
}

From source file:com.eventattend.portal.bl.FaceBookBL.java

public static String generateEmailHash(String email) {
    email = email.trim().toLowerCase();//  ww w.ja v a  2s . c  o  m
    CRC32 crc = new CRC32();
    crc.update(email.getBytes());
    String md5 = MD5(email);
    return crc.getValue() + "_" + md5;
}

From source file:com.seer.datacruncher.utils.CryptoUtil.java

private boolean checkKeyVerify() {
    CRC32 crc = new CRC32();
    crc.update(cryWorm(Reserved.ENCRYPTIONKEY, Reserved.CHECK, 0).getBytes());
    return Long.toString((crc.getValue())).equals(Reserved.CHECK);
}

From source file:org.digidoc4j.impl.bdoc.asic.AsicContainerCreator.java

private ZipEntry getAsicMimeTypeZipEntry(byte[] mimeTypeBytes) {
    ZipEntry entryMimetype = new ZipEntry(ZIP_ENTRY_MIMETYPE);
    entryMimetype.setMethod(ZipEntry.STORED);
    entryMimetype.setSize(mimeTypeBytes.length);
    entryMimetype.setCompressedSize(mimeTypeBytes.length);
    CRC32 crc = new CRC32();
    crc.update(mimeTypeBytes);//from ww  w. j  a  va  2  s . c o  m
    entryMimetype.setCrc(crc.getValue());
    return entryMimetype;
}

From source file:org.nuxeo.template.odt.OOoArchiveModifier.java

protected void writeOOoEntry(ZipOutputStream zipOutputStream, String entryName, File fileEntry, int zipMethod)
        throws IOException {

    if (fileEntry.isDirectory()) {
        entryName = entryName + "/";
        ZipEntry zentry = new ZipEntry(entryName);
        zipOutputStream.putNextEntry(zentry);
        zipOutputStream.closeEntry();/*from w  ww .j av a 2 s.  co m*/
        for (File child : fileEntry.listFiles()) {
            writeOOoEntry(zipOutputStream, entryName + child.getName(), child, zipMethod);
        }
        return;
    }

    ZipEntry zipEntry = new ZipEntry(entryName);
    try (InputStream entryInputStream = new FileInputStream(fileEntry)) {
        zipEntry.setMethod(zipMethod);
        if (zipMethod == ZipEntry.STORED) {
            byte[] inputBytes = IOUtils.toByteArray(entryInputStream);
            CRC32 crc = new CRC32();
            crc.update(inputBytes);
            zipEntry.setCrc(crc.getValue());
            zipEntry.setSize(inputBytes.length);
            zipEntry.setCompressedSize(inputBytes.length);
            zipOutputStream.putNextEntry(zipEntry);
            IOUtils.write(inputBytes, zipOutputStream);
        } else {
            zipOutputStream.putNextEntry(zipEntry);
            IOUtils.copy(entryInputStream, zipOutputStream);
        }
    }
    zipOutputStream.closeEntry();
}

From source file:org.ambiance.codec.YEncDecoder.java

/**
 * Decode a single file/* w  w w . j a  va2s  .c  o m*/
 */
public void decode(File input) throws DecoderException {
    try {
        YEncFile yencFile = new YEncFile(input);

        // Get the output file
        StringBuffer outputName = new StringBuffer(outputDirName);
        outputName.append(File.separator);
        outputName.append(yencFile.getHeader().getName());
        RandomAccessFile output = new RandomAccessFile(outputName.toString(), "rw");

        // Place the pointer to the begining of data to decode
        long pos = yencFile.getDataBegin();
        yencFile.getInput().seek(pos);

        // Bufferise the file
        // TODO - A Amliorer
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while (pos < yencFile.getDataEnd()) {
            baos.write(yencFile.getInput().read());
            pos++;
        }

        byte[] buff = decoder.decode(baos.toByteArray());

        // Write and close output
        output.write(buff);
        output.close();

        // Warn if CRC32 check is not OK
        CRC32 crc32 = new CRC32();
        crc32.update(buff);
        if (!yencFile.getTrailer().getCrc32().equals(Long.toHexString(crc32.getValue()).toUpperCase()))
            throw new DecoderException("Error in CRC32 check.");

    } catch (YEncException ye) {
        throw new DecoderException("Input file is not a YEnc file or contains error : " + ye.getMessage());
    } catch (FileNotFoundException fnfe) {
        throw new DecoderException("Enable to create output file : " + fnfe.getMessage());
    } catch (IOException ioe) {
        throw new DecoderException("Enable to read input file : " + ioe.getMessage());
    }
}