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:org.apache.hadoop.raid.Decoder.java

/**
 * Recover a corrupt block to local file. Using the stripe information 
 * stored in the Stripe Store.//  www.  j a  v  a 2s  .  c o m
 * 
 * @param srcFs The filesystem containing the source file.
 * @param srcPath The damaged source file.
 * @param lostBlock The missing/corrupted block
 * @param localBlockFile The file to write the block to.
 * @param blockSize The block size of the file.
 * @param lostBlockOffset The start offset of the block in the file.
 * @param limit The maximum number of bytes to be written out.
 * @param si  The StripeInfo retrieved from Stripe Store.
 * @param context
 * @return
 * @throws java.io.IOException
 */
public CRC32 recoverBlockToFileFromStripeInfo(FileSystem srcFs, Path srcPath, Block lostBlock,
        File localBlockFile, long blockSize, long lostBlockOffset, long limit, StripeInfo si, Context context)
        throws IOException {
    OutputStream out = null;
    try {
        out = new FileOutputStream(localBlockFile);
        CRC32 crc = null;
        if (checksumStore != null) {
            crc = new CRC32();
        }
        fixErasedBlockImpl(srcFs, srcPath, srcFs, null, true, blockSize, lostBlockOffset, limit, false, out,
                context, crc, si, true, lostBlock);
        return crc;
    } finally {
        if (null != out) {
            out.close();
        }
    }
}

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

public static String generateEmailHash(String email) {
    email = email.trim().toLowerCase();/*from   w w  w . j  av a2s  . c om*/
    CRC32 crc = new CRC32();
    crc.update(email.getBytes());
    String md5 = MD5(email);
    return crc.getValue() + "_" + md5;
}

From source file:org.pentaho.di.core.row.ValueDataUtil.java

public static Long ChecksumCRC32(ValueMetaInterface metaA, Object dataA) {
    long checksum = 0;
    FileObject file = null;/* w ww . ja  va2 s.  co m*/
    try {
        file = KettleVFS.getFileObject(dataA.toString());
        CheckedInputStream cis = null;

        // Computer CRC32 checksum
        cis = new CheckedInputStream(((LocalFile) file).getInputStream(), new CRC32());
        byte[] buf = new byte[128];
        int readSize = 0;
        do {
            readSize = cis.read(buf);
        } while (readSize >= 0);

        checksum = cis.getChecksum().getValue();

    } catch (Exception e) {
        // ignore - should likely log the exception
    } finally {
        if (file != null) {
            try {
                file.close();
                file = null;
            } catch (Exception e) {
                // Ignore
            }
        }
    }
    return checksum;
}

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);//from www . j av a2  s. co 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:de.catma.ui.repository.wizard.FileTypePanel.java

private boolean loadSourceDocumentAndContent(SourceDocumentResult sdr) {
    try {/*w ww  .  ja  va  2s  .  c  o  m*/
        SourceDocumentHandler sourceDocumentHandler = new SourceDocumentHandler();
        SourceDocument sourceDocument = sourceDocumentHandler.loadSourceDocument(sdr.getSourceDocumentID(),
                sdr.getSourceDocumentInfo());
        sdr.setSourceDocument(sourceDocument);

        TechInfoSet techInfoSet = sdr.getSourceDocumentInfo().getTechInfoSet();
        String documentId = sdr.getSourceDocumentID();

        ProtocolHandler protocolHandler = getProtocolHandlerForUri(techInfoSet.getURI(), documentId,
                techInfoSet.getMimeType());

        byte[] currentByteContent = protocolHandler.getByteContent();

        sourceDocument.getSourceContentHandler().load(new ByteArrayInputStream(currentByteContent));

        FileOSType fileOSType = FileOSType.getFileOSType(sourceDocument.getContent());

        sdr.getSourceDocumentInfo().getTechInfoSet().setFileOSType(fileOSType);
        CRC32 checksum = new CRC32();
        checksum.update(currentByteContent);
        sdr.getSourceDocumentInfo().getTechInfoSet().setChecksum(checksum.getValue());
        return true;
    } catch (Exception e) {
        TechInfoSet techInfoSet = sdr.getSourceDocumentInfo().getTechInfoSet();
        Notification.show(
                "Information",
                "Sorry, CATMA wasn't able to process the file as "
                        + techInfoSet.getFileType()
                        + (techInfoSet.getFileType().isCharsetSupported()
                                ? " with " + ((techInfoSet.getCharset() == null) ? "unknown charset"
                                        : " charset " + techInfoSet.getCharset())
                                : "")
                        + "\n\nThe original error message is: " + e.getLocalizedMessage(),
                Notification.Type.WARNING_MESSAGE);
        return false;
    }
}

From source file:org.apache.hadoop.raid.TestDirectoryRaidEncoder.java

private void printFileCRC(FileSystem fs, Path file, long bufferSize) throws IOException {
    byte[] buffer = new byte[(int) bufferSize];
    FSDataInputStream stm = fs.open(file);
    StringBuilder sb = new StringBuilder();
    sb.append("CRC for file: " + file + " size " + fs.getFileStatus(file).getLen() + "\n");
    while (stm.read(buffer) >= 0) {
        CRC32 crc = new CRC32();
        crc.update(buffer);/*from www.j a v  a 2 s .c  o m*/
        sb.append(" " + crc.getValue());
    }
    sb.append("\n");
    System.out.println(sb.toString());
    stm.close();
}

From source file:org.kuali.kfs.module.ar.report.service.impl.TransmitContractsAndGrantsInvoicesServiceImpl.java

/**
 *
 * @param report//w ww.  ja va 2  s  . c  o m
 * @param invoiceFileWritten
 * @param zos
 * @param buffer
 * @param crc
 * @return
 * @throws IOException
 */
private boolean writeFile(byte[] arrayToWrite, ZipOutputStream zos, String fileName) throws IOException {
    int bytesRead;
    byte[] buffer = new byte[1024];
    CRC32 crc = new CRC32();

    if (ObjectUtils.isNotNull(arrayToWrite) && arrayToWrite.length > 0) {
        BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(arrayToWrite));
        try {
            while ((bytesRead = bis.read(buffer)) != -1) {
                crc.update(buffer, 0, bytesRead);
            }
            bis.close();
            // Reset to beginning of input stream
            bis = new BufferedInputStream(new ByteArrayInputStream(arrayToWrite));
            ZipEntry entry = new ZipEntry(fileName);
            entry.setMethod(ZipEntry.STORED);
            entry.setCompressedSize(arrayToWrite.length);
            entry.setSize(arrayToWrite.length);
            entry.setCrc(crc.getValue());
            zos.putNextEntry(entry);
            while ((bytesRead = bis.read(buffer)) != -1) {
                zos.write(buffer, 0, bytesRead);
            }
        } finally {
            bis.close();
        }
        return true;
    }
    return false;
}

From source file:org.cryptomator.crypto.aes256.Aes256Cryptor.java

private long crc32Sum(byte[] source) {
    final CRC32 crc32 = new CRC32();
    crc32.update(source);/*from   w  ww  .ja va2s. c  om*/
    return crc32.getValue();
}

From source file:org.docx4j.openpackaging.io3.stores.ZipPartStore.java

public void saveBinaryPart(Part part) throws Docx4JException {

    // Drop the leading '/'
    String resolvedPartUri = part.getPartName().getName().substring(1);

    try {//from   ww  w . java 2s.com

        byte[] bytes = null;

        if (((BinaryPart) part).isLoaded()) {

            bytes = ((BinaryPart) part).getBytes();

        } else {

            if (this.sourcePartStore == null) {

                throw new Docx4JException("part store has changed, and sourcePartStore not set");

            } else if (this.sourcePartStore == this) {

                // Just use the ByteArray
                log.debug(part.getPartName() + " is clean");
                ByteArray byteArray = partByteArrays.get(part.getPartName().getName().substring(1));
                if (byteArray == null)
                    throw new IOException("part '" + part.getPartName() + "' not found");
                bytes = byteArray.getBytes();

            } else {

                InputStream is = sourcePartStore.loadPart(part.getPartName().getName().substring(1));
                bytes = IOUtils.toByteArray(is);
            }
        }

        // Add ZIP entry to output stream.
        if (part instanceof OleObjectBinaryPart) {
            // Workaround: Powerpoint 2010 (32-bit) can't play eg WMV if it is compressed!
            // (though 64-bit version is fine)

            ZipEntry ze = new ZipEntry(resolvedPartUri);
            ze.setMethod(ZipOutputStream.STORED);

            // must set size, compressed size, and crc-32
            ze.setSize(bytes.length);
            ze.setCompressedSize(bytes.length);

            CRC32 crc = new CRC32();
            crc.update(bytes);
            ze.setCrc(crc.getValue());

            zos.putNextEntry(ze);
        } else {
            zos.putNextEntry(new ZipEntry(resolvedPartUri));
        }

        zos.write(bytes);

        // Complete the entry
        zos.closeEntry();

    } catch (Exception e) {
        throw new Docx4JException("Failed to put binary part", e);
    }

    log.info("success writing part: " + resolvedPartUri);

}

From source file:com.utdallas.s3lab.smvhunter.monkey.MonkeyMe.java

/**
 * @param apkName/*  w  ww. j  a va  2  s .c  om*/
 * @return
 */
public static String genCrc(String apkName) {
    CRC32 crc = new CRC32();
    crc.update(apkName.getBytes());

    String input = "";
    if (crc.getValue() < 0) {
        input = Long.toString(crc.getValue() * -1);
    } else {
        input = Long.toString(crc.getValue());
    }
    return input;
}