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:org.apache.mnemonic.mapreduce.MneMapreduceBufferDataTest.java

@Test(enabled = true, dependsOnMethods = { "testWriteBufferData" })
public void testReadBufferData() throws Exception {
    long reccnt = 0L;
    long tsize = 0L;
    byte[] buf;/*from w  w w .j av a2s  .  com*/
    Checksum cs = new CRC32();
    cs.reset();
    File folder = new File(m_workdir.toString());
    File[] listfiles = folder.listFiles();
    for (int idx = 0; idx < listfiles.length; ++idx) {
        if (listfiles[idx].isFile()
                && listfiles[idx].getName().startsWith(MneConfigHelper.getBaseOutputName(m_conf, null))
                && listfiles[idx].getName().endsWith(MneConfigHelper.DEFAULT_FILE_EXTENSION)) {
            m_partfns.add(listfiles[idx].getName());
        }
    }
    Collections.sort(m_partfns); // keep the order for checksum
    for (int idx = 0; idx < m_partfns.size(); ++idx) {
        System.out.println(String.format("Verifying : %s", m_partfns.get(idx)));
        FileSplit split = new FileSplit(new Path(m_workdir, m_partfns.get(idx)), 0, 0L, new String[0]);
        InputFormat<NullWritable, MneDurableInputValue<DurableBuffer<?>>> inputFormat = new MneInputFormat<MneDurableInputValue<DurableBuffer<?>>, DurableBuffer<?>>();
        RecordReader<NullWritable, MneDurableInputValue<DurableBuffer<?>>> reader = inputFormat
                .createRecordReader(split, m_tacontext);
        MneDurableInputValue<DurableBuffer<?>> dbufval = null;
        while (reader.nextKeyValue()) {
            dbufval = reader.getCurrentValue();
            assert dbufval.getValue().getSize() == dbufval.getValue().get().capacity();
            dbufval.getValue().get().clear();
            buf = new byte[dbufval.getValue().get().capacity()];
            dbufval.getValue().get().get(buf);
            cs.update(buf, 0, buf.length);
            tsize += dbufval.getValue().getSize();
            ++reccnt;
        }
        reader.close();
    }
    AssertJUnit.assertEquals(m_reccnt, reccnt);
    AssertJUnit.assertEquals(m_totalsize, tsize);
    AssertJUnit.assertEquals(m_checksum, cs.getValue());
    System.out.println(String.format("The checksum of buffer is %d", m_checksum));
}

From source file:com.panet.imeta.core.row.ValueDataUtil.java

public static Long ChecksumCRC32(ValueMetaInterface metaA, Object dataA) {
    long checksum = 0;
    FileObject file = null;//from  www  .jav  a 2s. c o m
    try {
        file = KettleVFS.getFileObject(dataA.toString());
        CheckedInputStream cis = null;

        // Computer CRC32 checksum
        cis = new CheckedInputStream((FileInputStream) ((LocalFile) file).getInputStream(), new CRC32());
        byte[] buf = new byte[128];
        while (cis.read(buf) >= 0) {
        }

        checksum = cis.getChecksum().getValue();

    } catch (Exception e) {
    } finally {
        if (file != null)
            try {
                file.close();
            } catch (Exception e) {
            }
        ;
    }
    return checksum;
}

From source file:com.bitbreeds.webrtc.stun.StunMessage.java

/**
 * @return array of bytes representing message
 *///  w w  w  .  j  av  a 2  s .  co  m
public byte[] toBytes() {
    final List<byte[]> bt = new ArrayList<>();

    attributeSet.values().stream().forEach(i -> bt.add(i.toBytes()));

    StunHeader hd1 = withIntegrity ? header.updateMessageLength(header.getMessageLength() + 24) : header;
    if (withIntegrity) {
        byte[] macData = SignalUtil.joinBytesArrays(hd1.toBytes(), SignalUtil.joinBytesArrays(bt));

        byte[] mac = SignalUtil.hmacSha1(macData, password.getBytes());

        StunAttribute integrity = new StunAttribute(StunAttributeTypeEnum.MESSAGE_INTEGRITY, mac);
        bt.add(integrity.toBytes());
    }

    StunHeader hd2 = withFingerprint ? hd1.updateMessageLength(hd1.getMessageLength() + 8) : hd1;
    if (withFingerprint) {
        //Generate fingerprint
        final CRC32 crc = new CRC32();
        crc.update(hd2.toBytes());
        bt.forEach(crc::update);
        byte[] xorCRC32 = SignalUtil.xor(SignalUtil.fourBytesFromInt((int) crc.getValue()),
                new byte[] { 0x53, 0x54, 0x55, 0x4e });

        StunAttribute finger = new StunAttribute(StunAttributeTypeEnum.FINGERPRINT, xorCRC32);
        bt.add(finger.toBytes());
    }

    return SignalUtil.joinBytesArrays(hd2.toBytes(), SignalUtil.joinBytesArrays(bt));
}

From source file:org.openossad.util.core.row.ValueDataUtil.java

public static Long ChecksumCRC32(ValueMetaInterface metaA, Object dataA) {
    long checksum = 0;
    FileObject file = null;//from w w  w.  j  av a2s .  c o  m
    try {
        file = OpenDESIGNERVFS.getFileObject(dataA.toString());
        CheckedInputStream cis = null;

        // Computer CRC32 checksum
        cis = new CheckedInputStream((FileInputStream) ((LocalFile) file).getInputStream(), new CRC32());
        byte[] buf = new byte[128];
        while (cis.read(buf) >= 0) {
        }

        checksum = cis.getChecksum().getValue();

    } catch (Exception e) {
    } finally {
        if (file != null)
            try {
                file.close();
            } catch (Exception e) {
            }
        ;
    }
    return checksum;
}

From source file:com.dcits.govsbu.southernbase.baseproject2.modules.utils.Digests.java

/**
 * crc32php64bitlong/*from w w  w. j a v  a2  s  .  c  o  m*/
 */
public static long crc32AsLong(String input, Charset charset) {
    CRC32 crc32 = new CRC32();
    crc32.update(input.getBytes(charset));
    return crc32.getValue();
}

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

private long createOldFile(FileSystem fileSys, Path name, int repl, int numBlocks, long blocksize)
        throws IOException {
    CRC32 crc = new CRC32();
    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  w  w .  j a va2 s.c o  m
        stm.write(b);
        crc.update(b);
    }
    stm.close();
    return crc.getValue();
}

From source file:com.jkoolcloud.tnt4j.streams.configure.state.AbstractFileStreamStateHandler.java

/**
 * Check the line CRC.// w  ww.  j av a 2 s.  c o  m
 *
 * @param line
 *            line to check
 * @param crc
 *            CRC to match
 *
 * @return {@code true} if matched
 */
private static boolean checkCrc(String line, Long crc) {
    if (line == null || crc == null) {
        return false;
    }

    Checksum crcLine = new CRC32();
    final byte[] bytes4Line = line.getBytes();
    crcLine.update(bytes4Line, 0, bytes4Line.length);
    final long lineCRC = crcLine.getValue();
    return lineCRC == crc;
}

From source file:uk.ac.cam.cl.dtg.isaac.dos.eventbookings.PgEventBookings.java

/**
 * Acquire a globally unique database lock.
 * This method will block until the lock is released.
 * Any locks must be released manually.//ww  w .  j  a  va  2 s  .com
 *
 * @param resourceId - the unique id for the object to be locked.
 */
@Override
public void acquireDistributedLock(final String resourceId) throws SegueDatabaseException {
    // generate 32 bit CRC based on table id and resource id so that is is more likely to be unique globally.
    CRC32 crc = new CRC32();
    crc.update((TABLE_NAME + resourceId).getBytes());

    // acquire lock
    try (Connection conn = ds.getDatabaseConnection()) {
        PreparedStatement pst;
        pst = conn.prepareStatement("SELECT pg_advisory_lock(?)");
        pst.setLong(1, crc.getValue());
        log.debug(String.format("Acquiring advisory lock on %s (%s)", TABLE_NAME + resourceId, crc.getValue()));
        pst.executeQuery();
    } catch (SQLException e) {
        String msg = String.format("Unable to acquire lock for event (%s).", resourceId);
        log.error(msg);
        throw new SegueDatabaseException(msg);
    }
    log.debug(String.format("Acquired advisory lock on %s (%s)", TABLE_NAME + resourceId, crc.getValue()));
}

From source file:org.apache.mnemonic.DurablePersonNGTest.java

@Test(dependsOnMethods = { "testGenPeople" })
public void testCheckPeople() throws RetrieveDurableEntityError {
    NonVolatileMemAllocator act = new NonVolatileMemAllocator(
            Utils.getNonVolatileMemoryAllocatorService("pmalloc"), 1024 * 1024 * 8, "./pobj_person.dat", false);
    act.setBufferReclaimer(new Reclaim<ByteBuffer>() {
        @Override//w  w  w .java 2  s.  c om
        public boolean reclaim(ByteBuffer mres, Long sz) {
            System.out.println(String.format("Reclaim Memory Buffer: %X  Size: %s",
                    System.identityHashCode(mres), null == sz ? "NULL" : sz.toString()));
            return false;
        }
    });
    act.setChunkReclaimer(new Reclaim<Long>() {
        @Override
        public boolean reclaim(Long mres, Long sz) {
            System.out.println(String.format("Reclaim Memory Chunk: %X  Size: %s",
                    System.identityHashCode(mres), null == sz ? "NULL" : sz.toString()));
            return false;
        }
    });

    Checksum pic_cs = new CRC32();
    pic_cs.reset();
    Checksum fp_cs = new CRC32();
    fp_cs.reset();
    long size;
    byte[] buf;

    long val;
    for (long i = 0; i < cKEYCAPACITY; ++i) {
        System.out.printf("----------Key %d--------------\n", i);
        val = act.getHandler(i);
        if (0L == val) {
            break;
        }
        Person<Integer> person = PersonFactory.restore(act, val, true);
        while (null != person) {
            person.testOutput();
            person.getPicture().get().clear();
            buf = new byte[person.getPicture().get().capacity()];
            person.getPicture().get().get(buf);
            pic_cs.update(buf, 0, buf.length);
            byte b;
            for (int j = 0; j < person.getPreference().getSize(); ++j) {
                b = unsafe.getByte(person.getPreference().get() + j);
                fp_cs.update(b);
            }
            person = person.getMother();
        }
    }

    act.close();
    Assert.assertEquals(pic_cs.getValue(), pic_checksum);
    Assert.assertEquals(fp_cs.getValue(), fp_checksum);
}

From source file:net.sourceforge.jaulp.file.checksum.ChecksumUtils.java

/**
 * Gets the checksum from the given file. If the flag crc is true than the CheckedInputStream is
 * constructed with an instance of <code>java.util.zip.CRC32</code> otherwise with an instance
 * of <code>java.util.zip.Adler32</code>.
 *
 * @param file//from w ww  .j  av  a2 s  .  c  om
 *            The file The file from what to get the checksum.
 * @param crc
 *            The crc If the flag crc is true than the CheckedInputStream is constructed with an
 *            instance of {@link java.util.zip.CRC32} object otherwise it is constructed with an
 *            instance of
 * @return The checksum from the given file as long.
 * @throws FileNotFoundException
 *             Is thrown if the file is not found.
 * @throws IOException
 *             Signals that an I/O exception has occurred. {@link java.util.zip.CRC32} object
 *             otherwise it is constructed with an instance of {@link java.util.zip.Adler32}
 *             object. {@link java.util.zip.Adler32} object.
 */
public static long getChecksum(File file, boolean crc) throws FileNotFoundException, IOException {
    CheckedInputStream cis = null;
    if (crc) {
        cis = new CheckedInputStream(new FileInputStream(file), new CRC32());
    } else {
        cis = new CheckedInputStream(new FileInputStream(file), new Adler32());
    }
    int length = (int) file.length();
    byte[] buffer = new byte[length];
    long checksum = 0;
    while (cis.read(buffer) >= 0) {
        checksum = cis.getChecksum().getValue();
    }
    checksum = cis.getChecksum().getValue();
    StreamUtils.closeInputStream(cis);
    return checksum;
}