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:net.sourceforge.squirrel_sql.fw.util.IOUtilitiesImpl.java

/**
 * @see net.sourceforge.squirrel_sql.fw.util.IOUtilities#getCheckSum(java.io.File)
 *///from  w  w  w .  j  a  v  a  2s . c o  m
public long getCheckSum(File f) throws IOException {
    CRC32 result = new CRC32();
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(f);
        int len = 0;
        byte[] buffer = new byte[DISK_DATA_BUFFER_SIZE];
        while ((len = fis.read(buffer)) != -1) {
            result.update(buffer, 0, len);
        }
    } finally {
        closeInputStream(fis);
    }
    return result.getValue();
}

From source file:net.sourceforge.subsonic.controller.DownloadController.java

/**
 * Computes the CRC checksum for the given file.
 *
 * @param file The file to compute checksum for.
 * @return A CRC32 checksum./*from  www . j  a va 2 s .  c  o  m*/
 * @throws IOException If an I/O error occurs.
 */
private long computeCrc(File file) throws IOException {
    CRC32 crc = new CRC32();
    InputStream in = new FileInputStream(file);

    try {

        byte[] buf = new byte[8192];
        int n = in.read(buf);
        while (n != -1) {
            crc.update(buf, 0, n);
            n = in.read(buf);
        }

    } finally {
        in.close();
    }

    return crc.getValue();
}

From source file:org.alfresco.repo.domain.audit.AbstractAuditDAOImpl.java

/**
 * {@inheritDoc}/*ww  w . j av  a2 s.c  o  m*/
 */
public Pair<Long, ContentData> getOrCreateAuditModel(URL url) {
    InputStream is = null;
    try {
        is = url.openStream();
        // Calculate the CRC and find an entry that matches
        CRC32 crcCalc = new CRC32();
        byte[] buffer = new byte[1024];
        int read = -1;
        do {
            read = is.read(buffer);
            if (read < 0) {
                break;
            }
            crcCalc.update(buffer, 0, read);
        } while (true);
        long crc = crcCalc.getValue();
        // Find an existing entry
        AuditModelEntity existingEntity = getAuditModelByCrc(crc);
        if (existingEntity != null) {
            Long existingEntityId = existingEntity.getId();
            // Locate the content
            ContentData existingContentData = contentDataDAO.getContentData(existingEntity.getContentDataId())
                    .getSecond();
            Pair<Long, ContentData> result = new Pair<Long, ContentData>(existingEntityId, existingContentData);
            // Done
            if (logger.isDebugEnabled()) {
                logger.debug("Found existing model with same CRC: \n" + "   URL:    " + url + "\n"
                        + "   CRC:    " + crc + "\n" + "   Result: " + result);
            }
            return result;
        } else {
            // Upload the content afresh
            is.close();
            is = url.openStream();
            ContentWriter writer = contentService.getWriter(null, null, false);
            writer.setEncoding("UTF-8");
            writer.setMimetype(MimetypeMap.MIMETYPE_XML);
            writer.putContent(is);
            ContentData newContentData = writer.getContentData();
            Long newContentDataId = contentDataDAO.createContentData(newContentData).getFirst();
            AuditModelEntity newEntity = createAuditModel(newContentDataId, crc);
            Pair<Long, ContentData> result = new Pair<Long, ContentData>(newEntity.getId(), newContentData);
            // Done
            if (logger.isDebugEnabled()) {
                logger.debug("Created new audit model: \n" + "   URL:    " + url + "\n" + "   CRC:    " + crc
                        + "\n" + "   Result: " + result);
            }
            return result;
        }
    } catch (IOException e) {
        throw new AlfrescoRuntimeException("Failed to read Audit model: " + url);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (Throwable e) {
            }
        }
    }
}

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

/**
 *
 * @param report/* w w w . j ava 2s  .  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.kuali.kfs.module.ar.document.service.impl.DunningLetterServiceImpl.java

/**
 * This method generates the actual pdf files to print.
 *
 * @param mapping//  w ww  .  jav  a2s.  co  m
 * @param form
 * @param list
 * @return
 */
@Override
public boolean createZipOfPDFs(byte[] report, ByteArrayOutputStream baos) throws IOException {

    ZipOutputStream zos = new ZipOutputStream(baos);
    int bytesRead;
    byte[] buffer = new byte[1024];
    CRC32 crc = new CRC32();

    if (ObjectUtils.isNotNull(report)) {
        BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(report));
        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 ByteArrayInputStream(report));
        ZipEntry entry = new ZipEntry("DunningLetters&Invoices-"
                + getDateTimeService().toDateStringForFilename(getDateTimeService().getCurrentDate()) + ".pdf");
        entry.setMethod(ZipEntry.STORED);
        entry.setCompressedSize(report.length);
        entry.setSize(report.length);
        entry.setCrc(crc.getValue());
        zos.putNextEntry(entry);
        while ((bytesRead = bis.read(buffer)) != -1) {
            zos.write(buffer, 0, bytesRead);
        }
        bis.close();
    }

    zos.close();
    return true;
}

From source file:com.webpagebytes.cms.local.WPBLocalFileStorage.java

public void storeFile(InputStream is, WPBFilePath file) throws IOException {
    String fullFilePath = getLocalFullDataPath(file);
    OutputStream fos = createStorageOutputStream(fullFilePath);
    byte[] buffer = new byte[4096];
    CRC32 crc = new CRC32();
    MessageDigest md = null;//from   w  w w .  j av a  2s .  c  om
    try {
        md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        IOUtils.closeQuietly(fos);
        throw new IOException("Cannot calculate md5 to store the file", e);
    }

    int count = 0;
    int size = 0;
    while ((count = is.read(buffer)) != -1) {
        size += count;
        fos.write(buffer, 0, count);
        crc.update(buffer, 0, count);
        md.update(buffer, 0, count);
    }

    IOUtils.closeQuietly(fos);

    Properties props = new Properties();
    props.put("path", file.getPath());
    props.put("contentType", "application/octet-stream");
    props.put("crc32", String.valueOf(crc.getValue()));
    props.put("md5", DatatypeConverter.printBase64Binary(md.digest()));
    props.put("creationTime",
            String.valueOf(Calendar.getInstance(TimeZone.getTimeZone("GMT")).getTime().getTime()));
    props.put("size", String.valueOf(size));

    String metaPath = getLocalFullMetaPath(file);
    storeFileProperties(props, metaPath);

}

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

private void validateFile(FileSystem fileSys, Path name1, Path name2, long crc) throws IOException {

    FileStatus stat1 = fileSys.getFileStatus(name1);
    FileStatus stat2 = fileSys.getFileStatus(name2);
    assertTrue(" Length of file " + name1 + " is " + stat1.getLen() + " is different from length of file "
            + name1 + " " + stat2.getLen(), stat1.getLen() == stat2.getLen());

    CRC32 newcrc = new CRC32();
    FSDataInputStream stm = fileSys.open(name2);
    final byte[] b = new byte[4192];
    int num = 0;/* w ww .ja v  a 2s  . co m*/
    while (num >= 0) {
        num = stm.read(b);
        if (num < 0) {
            break;
        }
        newcrc.update(b, 0, num);
    }
    stm.close();
    if (newcrc.getValue() != crc) {
        fail("CRC mismatch of files " + name1 + " with file " + name2);
    }
}

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

private long[] loadEndOfMergeMarker(long deltaWALId, byte[] row) {
    long[] marker = UIO.bytesLongs(row);
    if (marker[EOM_VERSION_INDEX] != 1) {
        return null;
    }//from w  ww. j a v  a  2  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
    if (marker[EOM_CHECKSUM_INDEX] != crC32.getValue()) {
        return null;
    }
    if (deltaWALId > -1 && marker[EOM_DELTA_WAL_ID_INDEX] >= deltaWALId) {
        return null;
    }
    return marker;

}

From source file:com.zimbra.cs.zimlet.ZimletUtil.java

private static long computeCRC32(File file) throws IOException {
    byte buf[] = new byte[32 * 1024];
    CRC32 crc = new CRC32();
    crc.reset();/*  ww  w  .  j  a v  a 2 s  . co  m*/
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(file);
        int bytesRead;
        while ((bytesRead = fis.read(buf)) != -1) {
            crc.update(buf, 0, bytesRead);
        }
        return crc.getValue();
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:com.example.google.play.apkx.SampleDownloaderActivity.java

/**
 * Go through each of the Expansion APK files and open each as a zip file.
 * Calculate the CRC for each file and return false if any fail to match.
 *
 * @return true if XAPKZipFile is successful
 *//*  w  ww. j  a  v  a 2s .co m*/
void validateXAPKZipFiles() {
    AsyncTask<Object, DownloadProgressInfo, Boolean> validationTask = new AsyncTask<Object, DownloadProgressInfo, Boolean>() {

        @Override
        protected void onPreExecute() {
            mDashboard.setVisibility(View.VISIBLE);
            mCellMessage.setVisibility(View.GONE);
            mStatusText.setText(R.string.text_verifying_download);
            mPauseButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mCancelValidation = true;
                }
            });
            mPauseButton.setText(R.string.text_button_cancel_verify);
            super.onPreExecute();
        }

        @Override
        protected Boolean doInBackground(Object... params) {
            for (XAPKFile xf : xAPKS) {
                String fileName = Helpers.getExpansionAPKFileName(SampleDownloaderActivity.this, xf.mIsMain,
                        xf.mFileVersion);
                if (!Helpers.doesFileExist(SampleDownloaderActivity.this, fileName, xf.mFileSize, false))
                    return false;
                fileName = Helpers.generateSaveFileName(SampleDownloaderActivity.this, fileName);
                ZipResourceFile zrf;
                byte[] buf = new byte[1024 * 256];
                try {
                    zrf = new ZipResourceFile(fileName);
                    ZipEntryRO[] entries = zrf.getAllEntries();
                    /**
                     * First calculate the total compressed length
                     */
                    long totalCompressedLength = 0;
                    for (ZipEntryRO entry : entries) {
                        totalCompressedLength += entry.mCompressedLength;
                    }
                    float averageVerifySpeed = 0;
                    long totalBytesRemaining = totalCompressedLength;
                    long timeRemaining;
                    /**
                     * Then calculate a CRC for every file in the
                     * Zip file, comparing it to what is stored in
                     * the Zip directory. Note that for compressed
                     * Zip files we must extract the contents to do
                     * this comparison.
                     */
                    for (ZipEntryRO entry : entries) {
                        if (-1 != entry.mCRC32) {
                            long length = entry.mUncompressedLength;
                            CRC32 crc = new CRC32();
                            DataInputStream dis = null;
                            try {
                                dis = new DataInputStream(zrf.getInputStream(entry.mFileName));

                                long startTime = SystemClock.uptimeMillis();
                                while (length > 0) {
                                    int seek = (int) (length > buf.length ? buf.length : length);
                                    dis.readFully(buf, 0, seek);
                                    crc.update(buf, 0, seek);
                                    length -= seek;
                                    long currentTime = SystemClock.uptimeMillis();
                                    long timePassed = currentTime - startTime;
                                    if (timePassed > 0) {
                                        float currentSpeedSample = (float) seek / (float) timePassed;
                                        if (0 != averageVerifySpeed) {
                                            averageVerifySpeed = SMOOTHING_FACTOR * currentSpeedSample
                                                    + (1 - SMOOTHING_FACTOR) * averageVerifySpeed;
                                        } else {
                                            averageVerifySpeed = currentSpeedSample;
                                        }
                                        totalBytesRemaining -= seek;
                                        timeRemaining = (long) (totalBytesRemaining / averageVerifySpeed);
                                        this.publishProgress(new DownloadProgressInfo(totalCompressedLength,
                                                totalCompressedLength - totalBytesRemaining, timeRemaining,
                                                averageVerifySpeed));
                                    }
                                    startTime = currentTime;
                                    if (mCancelValidation)
                                        return true;
                                }
                                if (crc.getValue() != entry.mCRC32) {
                                    Log.e(Constants.TAG, "CRC does not match for entry: " + entry.mFileName);
                                    Log.e(Constants.TAG, "In file: " + entry.getZipFileName());
                                    return false;
                                }
                            } finally {
                                if (null != dis) {
                                    dis.close();
                                }
                            }
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    return false;
                }
            }
            return true;
        }

        @Override
        protected void onProgressUpdate(DownloadProgressInfo... values) {
            onDownloadProgress(values[0]);
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(Boolean result) {
            if (result) {
                mDashboard.setVisibility(View.VISIBLE);
                mCellMessage.setVisibility(View.GONE);
                mStatusText.setText(R.string.text_validation_complete);
                mPauseButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        startMovie();
                    }
                });
                mPauseButton.setText(android.R.string.ok);
            } else {
                mDashboard.setVisibility(View.VISIBLE);
                mCellMessage.setVisibility(View.GONE);
                mStatusText.setText(R.string.text_validation_failed);
                mPauseButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
                mPauseButton.setText(android.R.string.cancel);
            }
            super.onPostExecute(result);
        }

    };
    validationTask.execute(new Object());
}