Example usage for java.util.zip CRC32 getValue

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

Introduction

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

Prototype

@Override
public long getValue() 

Source Link

Document

Returns CRC-32 value.

Usage

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

static public void corruptBlocksInDirectory(Configuration conf, Path srcDir, long[] crcs,
        Integer[] listBlockNumToCorrupt, FileSystem fileSys, MiniDFSCluster cluster, boolean validate,
        boolean reportBadBlocks) throws IOException {
    long[] lengths = new long[crcs.length];
    // Get all block Info;
    ArrayList<BlockInfo> blocks = new ArrayList<BlockInfo>();
    List<FileStatus> lfs = RaidNode.listDirectoryRaidFileStatus(conf, fileSys, srcDir);
    assertNotNull(lfs);/*from  w  ww  .j a v  a 2  s  .  com*/
    for (int fid = 0; fid < lfs.size(); fid++) {
        FileStatus fsStat = lfs.get(fid);
        long numBlock = RaidNode.getNumBlocks(fsStat);
        for (int bid = 0; bid < numBlock; bid++) {
            blocks.add(new BlockInfo(fid, bid));
        }
        lengths[fid] = fsStat.getLen();
    }
    HashSet<Integer> affectedFiles = new HashSet<Integer>();
    HashSet<Integer> affectedBlocks = new HashSet<Integer>();
    // corrupt blocks
    for (int blockNumToCorrupt : listBlockNumToCorrupt) {
        if (blockNumToCorrupt >= blocks.size()) {
            continue;
        }
        BlockInfo bi = null;
        int blockIndex = blockNumToCorrupt;
        if (blockNumToCorrupt < 0) {
            blockIndex = blocks.size() + blockNumToCorrupt;
            if (blockIndex < 0) {
                continue;
            }
        }
        if (affectedBlocks.contains(blockIndex)) {
            continue;
        }
        affectedBlocks.add(blockIndex);
        bi = blocks.get(blockIndex);
        FileStatus srcFileFs = lfs.get(bi.fileIdx);
        Path srcFile = srcFileFs.getPath();
        LOG.info("Corrupt block " + bi.blockId + " of file " + srcFile);
        LocatedBlocks locations = RaidDFSUtil.getBlockLocations((DistributedFileSystem) fileSys,
                srcFile.toUri().getPath(), 0L, srcFileFs.getLen());
        TestRaidDfs.corruptBlock(srcFile, locations.get(bi.blockId).getBlock(), NUM_DATANODES, true, cluster);
        if (reportBadBlocks) {
            cluster.getNameNode().reportBadBlocks(new LocatedBlock[] { locations.get(bi.blockId) });
        }
        affectedFiles.add(bi.fileIdx);
    }
    // validate files
    if (validate) {
        DistributedRaidFileSystem raidfs = getRaidFS(fileSys, conf);
        for (Integer fid : affectedFiles) {
            FileStatus stat = lfs.get(fid);
            assertTrue(TestRaidDfs.validateFile(raidfs, stat.getPath(), lengths[fid], crcs[fid]));
            // test readFully
            byte[] filebytes = new byte[(int) stat.getLen()];
            FSDataInputStream stm = raidfs.open(stat.getPath());
            stm.readFully(0, filebytes);
            CRC32 crc = new CRC32();
            crc.update(filebytes, 0, filebytes.length);
            assertEquals(crcs[fid], crc.getValue());
        }
    }
}

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

/**
 * Computes the CRC32 sum for a given file.
 * //  www  .  j a va2 s .  c  o m
 * @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:com.utdallas.s3lab.smvhunter.monkey.MonkeyMe.java

/**
 * @param apkName// ww  w .j  a v  a2s . com
 * @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:com.blockwithme.longdb.tools.Utils.java

/** Get checksum CRC32 for the file content.
 * /*from www.  j  a  v  a2 s . c om*/
 * @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:JarUtil.java

/**
 * @param entry/*from   www .  java  2  s  .  c o 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:guru.benson.pinch.Pinch.java

/**
 * Extract all ZipEntries from the ZIP central directory.
 *
 * @param buf//from  ww  w . jav a  2s .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);//  w ww  . j  a  v  a 2s.  c  o  m
        stm.write(b);
        crc.update(b);
    }
    stm.close();
    return crc.getValue();
}

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:org.alfresco.repo.domain.node.ChildAssocEntity.java

/**
 * Find a CRC value for the full QName using UTF-8 conversion.
 * /*from w  ww . j  a v a2 s .  com*/
 * @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.blockwithme.longdb.tools.DBTool.java

/** Write CRC value in the meta-data file */
private static void writeCRC(final String theOutFile, final OutputStream theBOStrm, final boolean isCompressed,
        final CRC32 theChecksum) throws IOException {
    final Long chksum = isCompressed ? ((CRCGZipOutputStream) theBOStrm).getCRC().getValue()
            : theChecksum.getValue();
    final File crcFile = new File((new File(theOutFile)).getAbsolutePath() // $codepro.audit.disable
            // com.instantiations.assist.eclipse.analysis.pathManipulation
            + ".crc32");
    FileUtils.writeStringToFile(crcFile, Long.toHexString(chksum));
}