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(ByteBuffer buffer) 

Source Link

Document

Updates the CRC-32 checksum with the bytes from the specified buffer.

Usage

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

public static long checkSum(byte[] bytes) {
    CRC32 crc = new CRC32();
    crc.update(bytes);
    return crc.getValue();

}

From source file:com.jhash.oimadmin.Utils.java

public static void createJarFileFromContent(Map<String, byte[]> content, String[] fileSequence,
        String jarFileName) {// w  ww .  j a  v  a  2 s . c  o  m
    logger.trace("createJarFileFromContent({},{})", content, jarFileName);
    logger.trace("Trying to create a new jar file");
    try (ZipOutputStream jarFileOutputStream = new ZipOutputStream(new FileOutputStream(jarFileName))) {
        jarFileOutputStream.setMethod(ZipOutputStream.STORED);
        for (String jarItem : fileSequence) {
            logger.trace("Processing item {}", jarItem);
            byte[] fileContent = content.get(jarItem);
            if (fileContent == null)
                throw new NullPointerException("Failed to locate content for file " + jarItem);
            JarEntry pluginXMLFileEntry = new JarEntry(jarItem);
            pluginXMLFileEntry.setTime(System.currentTimeMillis());
            pluginXMLFileEntry.setSize(fileContent.length);
            pluginXMLFileEntry.setCompressedSize(fileContent.length);
            CRC32 crc = new CRC32();
            crc.update(fileContent);
            pluginXMLFileEntry.setCrc(crc.getValue());
            jarFileOutputStream.putNextEntry(pluginXMLFileEntry);
            jarFileOutputStream.write(fileContent);
            jarFileOutputStream.closeEntry();
        }
    } catch (Exception exception) {
        throw new OIMAdminException("Failed to create the Jar file " + jarFileName, exception);
    }
}

From source file:org.danilopianini.io.FileUtilities.java

/**
 * Computes the CRC32 sum for a given file.
 * // w w  w .  ja v a  2 s.c om
 * @param f
 *            the file
 * @return the CRC32
 * @throws IOException
 *             if an I/O error occurs
 */
public static long fileCRC32sum(final File f) throws IOException {
    try (final InputStream is = new FileInputStream(f)) {
        final CRC32 crc = new CRC32();
        int val;
        do {
            val = is.read();
            crc.update(val);
        } while (val != -1);
        is.close();
        return crc.getValue();
    }
}

From source file:org.alfresco.repo.domain.node.ChildAssocEntity.java

/**
 * Find a CRC value for the full QName using UTF-8 conversion.
 * // w  w w  .ja v  a2  s. c  o  m
 * @param qname                 the association qname
 * @return                      Returns the CRC value (UTF-8 compatible)
 */
public static Long getQNameCrc(QName qname) {
    CRC32 crc = new CRC32();
    try {
        crc.update(qname.getNamespaceURI().getBytes("UTF-8"));
        crc.update(qname.getLocalName().getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("UTF-8 encoding is not supported");
    }
    return crc.getValue();

}

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

/**
 * @param apkName/*from   w ww. j a  v  a 2s  .  c o m*/
 * @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;
}

From source file:org.alfresco.repo.domain.node.ChildAssocEntity.java

/**
 * Find a CRC value for the association's child node name using UTF-8 conversion.
 * /*from w  ww.j  a  v  a  2 s.co  m*/
 * @param childNodeName         the child node name
 * @return                      Returns the CRC value (UTF-8 compatible)
 */
public static Long getChildNodeNameCrc(String childNodeName) {
    CRC32 crc = new CRC32();
    try {
        // https://issues.alfresco.com/jira/browse/ALFCOM-1335
        crc.update(childNodeName.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("UTF-8 encoding is not supported");
    }
    return crc.getValue();
}

From source file:guru.benson.pinch.Pinch.java

/**
 * Extract all ZipEntries from the ZIP central directory.
 *
 * @param buf//from   ww w .jav  a 2 s  . c o m
 *     The byte buffer containing the ZIP central directory.
 *
 * @return A list with all ZipEntries.
 */
private static ArrayList<ExtendedZipEntry> parseHeaders(ByteBuffer buf) {
    ArrayList<ExtendedZipEntry> zeList = new ArrayList<ExtendedZipEntry>();

    buf.order(ByteOrder.LITTLE_ENDIAN);

    int offset = 0;

    while (offset < buf.limit() - ZipConstants.CENHDR) {
        short fileNameLen = buf.getShort(offset + ZipConstants.CENNAM);
        short extraFieldLen = buf.getShort(offset + ZipConstants.CENEXT);
        short fileCommentLen = buf.getShort(offset + ZipConstants.CENCOM);

        String fileName = new String(buf.array(), offset + ZipConstants.CENHDR, fileNameLen);

        ExtendedZipEntry zeGermans = new ExtendedZipEntry(fileName);

        zeGermans.setMethod(buf.getShort(offset + ZipConstants.CENHOW));

        CRC32 crc = new CRC32();
        crc.update(buf.getInt(offset + ZipConstants.CENCRC));
        zeGermans.setCrc(crc.getValue());

        zeGermans.setCompressedSize(buf.getInt(offset + ZipConstants.CENSIZ));
        zeGermans.setSize(buf.getInt(offset + ZipConstants.CENLEN));
        zeGermans.setInternalAttr(buf.getShort(offset + ZipConstants.CENATT));
        zeGermans.setExternalAttr(buf.getShort(offset + ZipConstants.CENATX));
        zeGermans.setOffset((long) buf.getInt(offset + ZipConstants.CENOFF));

        zeGermans.setExtraLength(extraFieldLen);

        zeList.add(zeGermans);
        offset += ZipConstants.CENHDR + fileNameLen + extraFieldLen + fileCommentLen;
    }

    return zeList;
}

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

private static long createTestFile(FileSystem fileSys, Path name, int repl, int numBlocks, long blocksize)
        throws IOException {
    CRC32 crc = new CRC32();
    Random rand = new Random();
    FSDataOutputStream stm = fileSys.create(name, true, fileSys.getConf().getInt("io.file.buffer.size", 4096),
            (short) repl, blocksize);
    // fill random data into file
    final byte[] b = new byte[(int) blocksize];
    for (int i = 0; i < numBlocks; i++) {
        rand.nextBytes(b);/* ww  w  . j  a  va  2  s . com*/
        stm.write(b);
        crc.update(b);
    }
    stm.close();
    return crc.getValue();
}

From source file:org.apache.hadoop.raid.tools.FastFileCheck.java

/**
 * Verify the certain offset of a file.//  w ww  .j a  v  a2s . c o  m
 */
private static boolean verifyFile(Configuration conf, FileSystem srcFs, FileSystem parityFs, FileStatus stat,
        Path parityPath, Codec codec, long blockOffset, Progressable reporter)
        throws IOException, InterruptedException {
    Path srcPath = stat.getPath();
    LOG.info("Verify file: " + srcPath + " at offset: " + blockOffset);
    int limit = (int) Math.min(stat.getBlockSize(), DEFAULT_VERIFY_LEN);
    if (reporter == null) {
        reporter = RaidUtils.NULL_PROGRESSABLE;
    }

    // try to decode.
    Decoder decoder = new Decoder(conf, codec);
    if (codec.isDirRaid) {
        decoder.connectToStore(srcPath);
    }

    List<Long> errorOffsets = new ArrayList<Long>();
    // first limit bytes
    errorOffsets.add(blockOffset);
    long left = Math.min(stat.getBlockSize(), stat.getLen() - blockOffset);
    if (left > limit) {
        // last limit bytes
        errorOffsets.add(blockOffset + left - limit);
        // random limit bytes.
        errorOffsets.add(blockOffset + rand.nextInt((int) (left - limit)));
    }

    byte[] buffer = new byte[limit];
    FSDataInputStream is = srcFs.open(srcPath);
    try {
        for (long errorOffset : errorOffsets) {
            is.seek(errorOffset);
            is.read(buffer);
            // calculate the oldCRC.
            CRC32 oldCrc = new CRC32();
            oldCrc.update(buffer);

            CRC32 newCrc = new CRC32();
            DecoderInputStream stream = decoder.new DecoderInputStream(RaidUtils.NULL_PROGRESSABLE, limit,
                    stat.getBlockSize(), errorOffset, srcFs, srcPath, parityFs, parityPath, null, null, false);
            try {
                stream.read(buffer);
                newCrc.update(buffer);
                if (oldCrc.getValue() != newCrc.getValue()) {
                    LogUtils.logFileCheckMetrics(LOGRESULTS.FAILURE, codec, srcPath, srcFs, errorOffset, limit,
                            null, reporter);
                    LOG.error("mismatch crc, old " + oldCrc.getValue() + ", new " + newCrc.getValue()
                            + ", for file: " + srcPath + " at offset " + errorOffset + ", read limit " + limit);
                    return false;
                }
            } finally {
                reporter.progress();
                if (stream != null) {
                    stream.close();
                }
            }
        }
        return true;
    } finally {
        is.close();
    }
}

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 {//from   w ww .j ava 2s .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);
    }

}