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:eionet.webq.service.RemoteFileServiceImpl.java

/**
 * Calculates crc32 checksum./* ww  w. jav a  2 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:com.littcore.io.util.ZipUtils.java

/**
 * ?/*from  ww  w  . jav  a2s .  co m*/
 * @param files 
 * @param targetFileNamePath 
 * @throws IOException
 */
public static void batchZip(File[] files, String targetFileNamePath) throws IOException {
    //?
    CheckedOutputStream cs = new CheckedOutputStream(new FileOutputStream(targetFileNamePath), new CRC32());
    //zip?
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(cs));
    File srcFile = null;
    for (int i = 0; i < files.length; i++) {
        srcFile = files[i];
        out.putNextEntry(new ZipEntry(srcFile.getName()));
        FileInputStream in = new FileInputStream(srcFile);
        int len;
        byte[] buf = new byte[BUFFERED_SIZE];
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        out.closeEntry();
        in.close();
    }
    out.close();
}

From source file:com.haulmont.cuba.core.sys.logging.LogArchiver.java

private static ArchiveEntry newTailArchive(String name, byte[] tail) {
    ZipArchiveEntry zipEntry = new ZipArchiveEntry(name);
    zipEntry.setSize(tail.length);// w w  w.ja  va 2  s.co  m
    zipEntry.setCompressedSize(zipEntry.getSize());
    CRC32 crc32 = new CRC32();
    crc32.update(tail);
    zipEntry.setCrc(crc32.getValue());
    return zipEntry;
}

From source file:org.anarres.lzo.LzopOutputStream.java

/**
 * Constructs a new LzopOutputStream./*  www  . j  a v a  2  s  . c om*/
 *
 * I recommend limiting flags to the following unless you REALLY know what
 * you are doing:
 * <ul>
 * <li>{@link LzopConstants#F_ADLER32_C}</li>
 * <li>{@link LzopConstants#F_ADLER32_D}</li>
 * <li>{@link LzopConstants#F_CRC32_C}</li>
 * <li>{@link LzopConstants#F_CRC32_D}</li>
 * </ul>
 */
public LzopOutputStream(OutputStream out, LzoCompressor compressor, int inputBufferSize, long flags)
        throws IOException {
    super(out, compressor, inputBufferSize);
    this.flags = flags;
    this.c_crc32_c = ((flags & LzopConstants.F_CRC32_C) == 0) ? null : new CRC32();
    this.c_crc32_d = ((flags & LzopConstants.F_CRC32_D) == 0) ? null : new CRC32();
    this.c_adler32_c = ((flags & LzopConstants.F_ADLER32_C) == 0) ? null : new Adler32();
    this.c_adler32_d = ((flags & LzopConstants.F_ADLER32_D) == 0) ? null : new Adler32();
    writeLzopHeader();
}

From source file:com.nridge.core.base.std.FilUtl.java

/**
 * Generates a unique path and file name combination based on the parameters
 * provided./*  w ww  .ja v  a 2s  .c  om*/
 *
 * @param aPathName Path name.
 * @param aFilePrefix File name prefix (appended with random id)
 * @param aFileExtension File name extension.
 *
 * @return A unique path and file name combination.
 */
static public String generateUniquePathFileName(String aPathName, String aFilePrefix, String aFileExtension) {
    String pathFileName;

    if (StringUtils.isNotEmpty(aPathName))
        pathFileName = String.format("%s%c%s", aPathName, File.separatorChar, aFilePrefix);
    else
        pathFileName = aFilePrefix;

    // http://www.javapractices.com/topic/TopicAction.do?Id=56

    UUID uniqueId = UUID.randomUUID();
    byte idBytes[] = uniqueId.toString().getBytes();
    Checksum checksumValue = new CRC32();
    checksumValue.update(idBytes, 0, idBytes.length);
    long longValue = checksumValue.getValue();

    if (StringUtils.startsWith(aFileExtension, "."))
        return String.format("%s_%d%s", pathFileName, longValue, aFileExtension);
    else
        return String.format("%s_%d.%s", pathFileName, longValue, aFileExtension);
}

From source file:com.blockwithme.longdb.tools.Utils.java

/** Get checksum CRC32 for the file content.
 * /*from w  w  w  .ja  va 2 s.co m*/
 * @param theFile
 *        the file
 * @param isCommpressed
 *        true if commpressed
 * @return the checksum (CRC32) of the file.
 * @throws Exception */
@SuppressWarnings("resource")
public static long getCRC32(final File theFile, final boolean isCommpressed) throws Exception {
    CheckedInputStream cis = null;
    try {
        final CRC32 checksum = new CRC32();
        // TODO: Would a buffered input stream make this faster?
        cis = new CheckedInputStream((isCommpressed ? new GZIPInputStream(new FileInputStream(theFile))
                : new FileInputStream(theFile)), checksum);
        final byte[] tempBuf = new byte[ONE_K];
        while (cis.read(tempBuf) >= 0)
            ; // just read the full stream. // $codepro.audit.disable
        return checksum.getValue();
    } finally {
        if (cis != null)
            cis.close();
    }
}

From source file:com.indeed.lsmtree.recordlog.BasicRecordFile.java

private Option<E> readAndCheck(long address, MutableLong nextElementStart) throws IOException {
    if (address + 4 > memory.length()) {
        throw new ConsistencyException("not enough bytes in file");
    }/*from  w  ww.  j  a va2s .  com*/
    final int length = memory.getInt(address);
    if (length < 0) {
        return Option.none();
    }
    if (address + 8 > memory.length()) {
        throw new ConsistencyException("not enough bytes in file");
    }
    if (address + 8 + length > memory.length()) {
        throw new ConsistencyException("not enough bytes in file");
    }
    final int checksum = memory.getInt(address + 4);
    MemoryDataInput in = new MemoryDataInput(memory);
    in.seek(address + 8);
    CRC32 crc32 = new CRC32();
    crc32.update(CRC_SEED);
    byte[] bytes = new byte[length];
    in.readFully(bytes);
    crc32.update(bytes);
    if ((int) crc32.getValue() != checksum) {
        throw new ConsistencyException("checksum for record does not match: expected " + checksum + " actual "
                + (int) crc32.getValue());
    }
    E ret = serializer.read(ByteStreams.newDataInput(bytes));
    if (nextElementStart != null)
        nextElementStart.setValue(address + 8 + length);
    return Option.some(ret);
}

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

/**
 * {@inheritDoc}//  www  .  j av a 2 s  .  com
 */
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:com.googlecode.flyway.core.migration.sql.SqlMigration.java

/**
 * Calculates the checksum of this sql script.
 *
 * @return The crc-32 checksum of the script.
 *//*from   ww w  . j av a2s  .co m*/
private int calculateChecksum(String sql) {
    final CRC32 crc32 = new CRC32();
    crc32.update(sql.getBytes());
    return (int) crc32.getValue();
}

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);
}