Example usage for java.util.zip CRC32 reset

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

Introduction

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

Prototype

@Override
public void reset() 

Source Link

Document

Resets CRC-32 to initial value.

Usage

From source file:org.broad.igv.util.Utilities.java

/**
 * @param buffer/*ww w.j  a  v  a  2 s . c om*/
 * @return
 * @throws java.io.IOException
 */
static private long getCrc(byte[] buffer) throws IOException {

    CRC32 crc = new CRC32();

    crc.reset();
    crc.update(buffer, 0, buffer.length);
    return crc.getValue();
}

From source file:FileUtils.java

/**
 * Utility method for copying file/* ww  w  .ja v  a  2  s .c  o  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:com.smash.revolance.ui.model.helper.ArchiveHelper.java

public static File buildArchive(File archive, File... files) throws FileNotFoundException {
    FileOutputStream fos = new FileOutputStream(archive);
    ZipOutputStream zos = new ZipOutputStream(fos);
    int bytesRead;
    byte[] buffer = new byte[1024];
    CRC32 crc = new CRC32();

    for (File file : files) {
        if (!file.exists()) {
            System.err.println("Skipping: " + file);
            continue;
        }/*ww w.  j  a v a  2s  .c  om*/
        BufferedInputStream bis = null;
        try {
            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));
            String entryPath = FileHelper.getRelativePath(archive.getParentFile(), file);

            ZipEntry entry = new ZipEntry(entryPath);
            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);
            }
        } catch (FileNotFoundException e) {

        } catch (IOException e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        } finally {
            IOUtils.closeQuietly(bis);
        }
    }
    IOUtils.closeQuietly(zos);
    return archive;
}

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

/**
 * Utility method for copying file /*from   w w w . j  a v a  2 s  .  co  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.mariotaku.twidere.util.TwitterContentUtils.java

public static boolean isOfficialKey(final Context context, final String consumerKey,
        final String consumerSecret) {
    if (context == null || consumerKey == null || consumerSecret == null)
        return false;
    final String[] keySecrets = context.getResources()
            .getStringArray(R.array.values_official_consumer_secret_crc32);
    final CRC32 crc32 = new CRC32();
    final byte[] consumerSecretBytes = consumerSecret.getBytes(Charset.forName("UTF-8"));
    crc32.update(consumerSecretBytes, 0, consumerSecretBytes.length);
    final long value = crc32.getValue();
    crc32.reset();
    for (final String keySecret : keySecrets) {
        if (Long.parseLong(keySecret, 16) == value)
            return true;
    }/*  w  w  w . j  ava  2 s  .co  m*/
    return false;
}

From source file:org.mariotaku.twidere.util.TwitterContentUtils.java

public static String getOfficialKeyName(final Context context, final String consumerKey,
        final String consumerSecret) {
    if (context == null || consumerKey == null || consumerSecret == null)
        return null;
    final String[] keySecrets = context.getResources()
            .getStringArray(R.array.values_official_consumer_secret_crc32);
    final String[] keyNames = context.getResources().getStringArray(R.array.names_official_consumer_secret);
    final CRC32 crc32 = new CRC32();
    final byte[] consumerSecretBytes = consumerSecret.getBytes(Charset.forName("UTF-8"));
    crc32.update(consumerSecretBytes, 0, consumerSecretBytes.length);
    final long value = crc32.getValue();
    crc32.reset();
    for (int i = 0, j = keySecrets.length; i < j; i++) {
        if (Long.parseLong(keySecrets[i], 16) == value)
            return keyNames[i];
    }//from   w w  w  . j  av a2s  .c  o m
    return null;
}

From source file:org.mariotaku.twidere.util.TwitterContentUtils.java

@NonNull
public static ConsumerKeyType getOfficialKeyType(final Context context, final String consumerKey,
        final String consumerSecret) {
    if (context == null || consumerKey == null || consumerSecret == null) {
        return ConsumerKeyType.UNKNOWN;
    }//from  ww  w. j  a  va  2  s  .c o m
    final String[] keySecrets = context.getResources()
            .getStringArray(R.array.values_official_consumer_secret_crc32);
    final String[] keyNames = context.getResources().getStringArray(R.array.types_official_consumer_secret);
    final CRC32 crc32 = new CRC32();
    final byte[] consumerSecretBytes = consumerSecret.getBytes(Charset.forName("UTF-8"));
    crc32.update(consumerSecretBytes, 0, consumerSecretBytes.length);
    final long value = crc32.getValue();
    crc32.reset();
    for (int i = 0, j = keySecrets.length; i < j; i++) {
        if (Long.parseLong(keySecrets[i], 16) == value) {
            return ConsumerKeyType.parse(keyNames[i]);
        }
    }
    return ConsumerKeyType.UNKNOWN;
}

From source file:JarUtil.java

/**
 * @param entry/*from w w  w .  ja  v a  2  s. co  m*/
 * @param in
 * @param out
 * @param crc
 * @param buffer
 * @throws IOException
 */
private static void add(JarEntry entry, InputStream in, JarOutputStream out, CRC32 crc, byte[] buffer)
        throws IOException {
    out.putNextEntry(entry);
    int read;
    long size = 0;
    while ((read = in.read(buffer)) != -1) {
        crc.update(buffer, 0, read);
        out.write(buffer, 0, read);
        size += read;
    }
    entry.setCrc(crc.getValue());
    entry.setSize(size);
    in.close();
    out.closeEntry();
    crc.reset();
}

From source file:com.aol.advertising.qiao.util.CommonUtils.java

public static long checksum(RandomAccessFile raFile, int numBytes)
        throws IOException, InsufficientFileLengthException {
    CRC32 _crc = new CRC32();

    long pos = raFile.getFilePointer();
    try {//w  ww  .  j a  v a  2 s  . c o  m
        byte[] buffer = new byte[numBytes];
        raFile.seek(0);
        int n = raFile.read(buffer);
        if (n < numBytes) {
            String s;
            logger.warn(s = ("not enough data for checksum: current file size=" + n));
            throw new InsufficientFileLengthException(s);
        }

        synchronized (_crc) {
            _crc.reset();
            _crc.update(buffer);

            return _crc.getValue();
        }
    } finally {
        raFile.seek(pos);
    }

}

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

static void validateFileCopy(FileSystem fs, Path path, long size, long[] blockCrcs, boolean twiceThrough)
        throws IOException {

    final int timesThrough = (twiceThrough ? 2 : 1);
    final int numBlocks = (int) Math.ceil((double) size / BLOCK_SIZE);

    // Check all the blocks timesThrough times
    FSDataInputStream in = fs.open(path);
    CRC32 crc = new CRC32();

    for (int i = 0; i < timesThrough; i++) {
        for (int b = 0; b < numBlocks; b++) {
            int chunkSize = (int) Math.min(BLOCK_SIZE, (size - (b * BLOCK_SIZE)));
            byte[] buf = new byte[chunkSize];

            in.read(buf);//from  ww  w  .  ja  v  a 2s .  c  om
            crc.reset();
            crc.update(buf);

            assertEquals(("Block crc " + b + " did not match on iteration " + i), blockCrcs[b], crc.getValue());
        }
        assert in.getPos() == size : "Did not read to end of file";
        if (i < (timesThrough - 1)) {
            in.seekToNewSource(0);
        }
    }
}