Example usage for java.util.zip CRC32 update

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

Introduction

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

Prototype

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

Source Link

Document

Updates the CRC-32 checksum with the specified array of bytes.

Usage

From source file:com.rover12421.shaka.apktool.lib.AndrolibAj.java

public static CRC32 calculateCrc(InputStream input) throws IOException {
    CRC32 crc = new CRC32();
    int bytesRead;
    byte[] buffer = new byte[4096];
    while ((bytesRead = input.read(buffer)) != -1) {
        crc.update(buffer, 0, bytesRead);
    }// w  w  w.  j  a v  a  2 s .  co  m
    return crc;
}

From source file:FileUtils.java

/**
 * Utility method for copying file/*from   w w  w.  j  a v a2 s.  co m*/
 * @param srcFile - source file
 * @param destFile - destination file
 */
public static void copyFile(File srcFile, File destFile) throws IOException {
    if (!srcFile.getPath().toLowerCase().endsWith(JPG) && !srcFile.getPath().toLowerCase().endsWith(JPEG)) {
        return;
    }
    final InputStream in = new FileInputStream(srcFile);
    final OutputStream out = new FileOutputStream(destFile);
    try {
        long millis = System.currentTimeMillis();
        CRC32 checksum;
        if (VERIFY) {
            checksum = new CRC32();
            checksum.reset();
        }
        final byte[] buffer = new byte[BUFFER_SIZE];
        int bytesRead = in.read(buffer);
        while (bytesRead >= 0) {
            if (VERIFY) {
                checksum.update(buffer, 0, bytesRead);
            }
            out.write(buffer, 0, bytesRead);
            bytesRead = in.read(buffer);
        }
        if (CLOCK) {
            millis = System.currentTimeMillis() - millis;
            System.out.println("Copy file '" + srcFile.getPath() + "' on " + millis / 1000L + " second(s)");
        }
    } catch (IOException e) {
        throw e;
    } finally {
        out.close();
        in.close();
    }
}

From source file:it.reexon.lib.files.FileUtils.java

/**
 * Utility method for copying file /*w ww . j  av a  2s .  c o m*/
 *  
 * @param srcFile - source file 
 * @param destFile - destination file 
 * @author A. Weinberger
 * 
 * @throws IOException              If the first byte cannot be read for any reason other than the end of the file, if the input stream has been closed, or if some other I/O error occurs.
 * @throws IllegalArgumentException If file are null
 * @throws FileNotFoundException    If srcFile doens't exist
 */
public static void copyFile(File srcFile, File destFile) throws IOException {
    if (srcFile == null || destFile == null)
        throw new IllegalArgumentException("Files cannot be null");
    if (!srcFile.exists())
        throw new FileNotFoundException("Start file must be exists");

    try (final InputStream in = new FileInputStream(srcFile);
            final OutputStream out = new FileOutputStream(destFile);) {
        long millis = System.currentTimeMillis();
        CRC32 checksum;
        if (VERIFY) {
            checksum = new CRC32();
            checksum.reset();
        }
        final byte[] buffer = new byte[BUFFER_SIZE];
        int bytesRead = in.read(buffer);
        while (bytesRead >= 0) {
            if (VERIFY) {
                checksum.update(buffer, 0, bytesRead);
            }
            out.write(buffer, 0, bytesRead);
            bytesRead = in.read(buffer);
        }
        if (CLOCK) {
            millis = System.currentTimeMillis() - millis;
            if (LOGS)
                System.out.println("Copy file '" + srcFile.getPath() + "' on " + millis / 1000L + " second(s)");
        }
    } catch (IOException e) {
        throw e;
    }
}

From source file:org.kalypso.commons.java.util.zip.ZipUtilities.java

private static void processFiles(final File packDir, final File file, final ZipOutputStream out,
        final IFileFilter filter) throws IOException {
    if (!filter.accept(file))
        return;// w  ww  .ja  v a2  s . co  m

    if (file.isDirectory()) {
        final ZipEntry e = new ZipEntry(convertFileName(packDir, file) + "/"); //$NON-NLS-1$
        out.putNextEntry(e);
        out.closeEntry();

        final File[] files = file.listFiles();
        for (final File f : files) {
            processFiles(packDir, f, out, filter);
        }
    } else {

        final BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

        final ZipEntry e = new ZipEntry(convertFileName(packDir, file));
        final CRC32 crc = new CRC32();

        out.putNextEntry(e);

        final byte[] buf = new byte[4096];
        int len = 0;

        while ((len = bis.read(buf)) > 0) {
            out.write(buf, 0, len);
            crc.update(buf, 0, len);
        }
        e.setCrc(crc.getValue());

        out.closeEntry();
        bis.close();

    }
}

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  w  ww  . java 2  s  .c om
    }
    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:org.apache.hadoop.hdfs.TestRaidDfs.java

static long bufferCRC(byte[] buf) {
    CRC32 crc = new CRC32();
    crc.update(buf, 0, buf.length);
    return crc.getValue();
}

From source file:net.librec.util.FileUtil.java

/**
 * Zip a given folder/*ww w  .ja va  2  s  . c o  m*/
 *
 * @param dirPath    a given folder: must be all files (not sub-folders)
 * @param filePath   zipped file
 * @throws Exception if error occurs
 */
public static void zipFolder(String dirPath, String filePath) throws Exception {
    File outFile = new File(filePath);
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(outFile));
    int bytesRead;
    byte[] buffer = new byte[1024];
    CRC32 crc = new CRC32();
    for (File file : listFiles(dirPath)) {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        crc.reset();
        while ((bytesRead = bis.read(buffer)) != -1) {
            crc.update(buffer, 0, bytesRead);
        }
        bis.close();

        // Reset to beginning of input stream
        bis = new BufferedInputStream(new FileInputStream(file));
        ZipEntry entry = new ZipEntry(file.getName());
        entry.setMethod(ZipEntry.STORED);
        entry.setCompressedSize(file.length());
        entry.setSize(file.length());
        entry.setCrc(crc.getValue());
        zos.putNextEntry(entry);
        while ((bytesRead = bis.read(buffer)) != -1) {
            zos.write(buffer, 0, bytesRead);
        }
        bis.close();
    }
    zos.close();

    LOG.debug("A zip-file is created to: " + outFile.getPath());
}

From source file:com.jivesoftware.os.amza.service.storage.WALStorage.java

private static long[] buildEndOfMergeMarker(long deltaWALId, long highestTxId, long oldestTimestamp,
        long oldestVersion, long oldestTombstonedTimestamp, long oldestTombstonedVersion, long keyCount,
        long clobberCount, long fpOfLastLeap, long updatesSinceLeap, long[] stripedKeyHighwaterTimestamps,
        int offset) {
    final long[] marker = new long[EOM_HIGHWATER_STRIPES_OFFSET + numKeyHighwaterStripes];
    marker[EOM_VERSION_INDEX] = 1; // version
    marker[EOM_CHECKSUM_INDEX] = 0; // placeholder checksum
    marker[EOM_DELTA_WAL_ID_INDEX] = deltaWALId;
    marker[EOM_HIGHEST_TX_ID_INDEX] = highestTxId;
    marker[EOM_OLDEST_TIMESTAMP_INDEX] = oldestTimestamp;
    marker[EOM_OLDEST_VERSION_INDEX] = oldestVersion;
    marker[EOM_OLDEST_TOMBSTONED_TIMESTAMP_INDEX] = oldestTombstonedTimestamp;
    marker[EOM_OLDEST_TOMBSTONED_VERSION_INDEX] = oldestTombstonedVersion;
    marker[EOM_KEY_COUNT_INDEX] = keyCount;
    marker[EOM_CLOBBER_COUNT_INDEX] = clobberCount;

    marker[EOM_FP_OF_LAST_LEAP_INDEX] = fpOfLastLeap;
    marker[EOM_UPDATES_SINCE_LAST_LEAP_INDEX] = updatesSinceLeap;

    System.arraycopy(stripedKeyHighwaterTimestamps, offset, marker, EOM_HIGHWATER_STRIPES_OFFSET,
            numKeyHighwaterStripes);/*ww w  . j  a  v a2 s .  c  o m*/

    CRC32 crC32 = new CRC32();
    byte[] hintsAsBytes = UIO.longsBytes(marker);
    crC32.update(hintsAsBytes, 16, hintsAsBytes.length - 16); // 16 skips the version and checksum
    marker[EOM_CHECKSUM_INDEX] = crC32.getValue();
    return marker;
}

From source file:ch.cyberduck.core.io.CRC32ChecksumCompute.java

@Override
public Checksum compute(final InputStream in, final TransferStatus status) throws ChecksumException {
    final CRC32 crc32 = new CRC32();
    try {/*from w  w  w  . j  a v  a2 s.  c  o m*/
        byte[] buffer = new byte[16384];
        int bytesRead;
        while ((bytesRead = in.read(buffer, 0, buffer.length)) != -1) {
            crc32.update(buffer, 0, bytesRead);
        }
    } catch (IOException e) {
        throw new ChecksumException(LocaleFactory.localizedString("Checksum failure", "Error"), e.getMessage(),
                e);
    } finally {
        IOUtils.closeQuietly(in);
    }
    return new Checksum(HashAlgorithm.crc32, Long.toHexString(crc32.getValue()));
}

From source file:org.apache.hadoop.hdfs.TestLookasideCache.java

/**
 * returns the CRC32 of the buffer//from w w  w.  ja  v  a 2  s  . c o m
 */
private long bufferCRC(byte[] buf) {
    CRC32 crc = new CRC32();
    crc.update(buf, 0, buf.length);
    return crc.getValue();
}