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.mariotaku.twidere.util.TwitterContentUtils.java

@NonNull
public static ConsumerKeyType getOfficialKeyType(final Context context, final String consumerKey,
        final String consumerSecret) {
    if (context == null || consumerKey == null || consumerSecret == null) {
        return ConsumerKeyType.UNKNOWN;
    }//from ww w .java 2 s  . c o  m
    final String[] keySecrets = context.getResources()
            .getStringArray(R.array.values_official_consumer_secret_crc32);
    final String[] keyNames = context.getResources().getStringArray(R.array.types_official_consumer_secret);
    final CRC32 crc32 = new CRC32();
    final byte[] consumerSecretBytes = consumerSecret.getBytes(Charset.forName("UTF-8"));
    crc32.update(consumerSecretBytes, 0, consumerSecretBytes.length);
    final long value = crc32.getValue();
    crc32.reset();
    for (int i = 0, j = keySecrets.length; i < j; i++) {
        if (Long.parseLong(keySecrets[i], 16) == value) {
            return ConsumerKeyType.parse(keyNames[i]);
        }
    }
    return ConsumerKeyType.UNKNOWN;
}

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

/** Export to stream. */
// TODO: theStdout is kind of superfluous here, as you could just say that a
// null theFilePath means that
@CheckForNull/*  www  .  j  av a2 s  .  co  m*/
private static void exportToStream(final String theFilePath, final String theFormat, final String theTable,
        final boolean theStdout) throws Exception {
    final boolean compress = CMD_LN.hasOption(COMPRESS_OPT.getOpt());
    OutputStream os = null;
    final DBInfo info = dBInfo(FALSE);
    try {
        if (!theStdout)
            os = new FileOutputStream(theFilePath);
        else
            os = System.out;
        CRC32 checksum = null;
        OutputStream chesumStr;
        if (compress)
            chesumStr = new CRCGZipOutputStream(os);
        else if (theStdout)
            chesumStr = os;
        else {
            checksum = new CRC32();
            chesumStr = new CheckedOutputStream(os, checksum);
        }
        final PrintWriter ouputWriter = new PrintWriter(new OutputStreamWriter(chesumStr, "UTF-8"));

        final ClientLoader loader = ClientLoader.newInstance(info).outputFormat(theFormat)
                .outWriter(ouputWriter).verbose(VERBOSE).verboseStream(System.err);

        final BackendClient client = loader.loadClient();
        client.export(theTable, getRowFilter());

        if (!theStdout)
            writeCRC(theFilePath, chesumStr, compress, checksum);

    } finally {
        DBUtils.closeQuitely(os);
    }
}

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

/**
 * crc32php64bitlong/*from  ww w  .  j a  va2  s.c  o  m*/
 */
public static long crc32AsLong(byte[] input) {
    CRC32 crc32 = new CRC32();
    crc32.update(input);
    return crc32.getValue();
}

From source file:org.apache.mnemonic.mapred.MneMapredBufferDataTest.java

@Test(enabled = true, dependsOnMethods = { "testWriteBufferData" })
public void testReadBufferData() throws Exception {
    long reccnt = 0L;
    long tsize = 0L;
    byte[] buf;/*from  w  w w. j a  v  a2 s . c  o m*/
    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
                .getRecordReader((InputSplit) split, m_conf, null);
        NullWritable dbufkey = reader.createKey();
        MneDurableInputValue<DurableBuffer<?>> dbufval = null;
        while (true) {
            dbufval = reader.createValue();
            if (reader.next(dbufkey, dbufval)) {
                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;
            } else {
                break;
            }
        }
        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:org.apache.mnemonic.collections.DurableArrayNGTest.java

@Test(enabled = true)
public void testGetSetArrayBuffer() {
    DurableType gtypes[] = { DurableType.BUFFER };
    int capacity = 10;
    DurableArray<DurableBuffer> array = DurableArrayFactory.create(m_act, null, gtypes, capacity, false);

    Long handler = array.getHandler();
    long bufVal;/* w  ww.java2  s .c  o  m*/

    Checksum bufferCheckSum = new CRC32();
    bufferCheckSum.reset();

    for (int i = 0; i < capacity; i++) {
        array.set(i, genuptBuffer(m_act, bufferCheckSum, genRandSize()));
    }
    bufVal = bufferCheckSum.getValue();

    bufferCheckSum.reset();

    for (int i = 0; i < capacity; i++) {
        DurableBuffer<NonVolatileMemAllocator> db = array.get(i);
        Assert.assertNotNull(db);
        byte buf[] = new byte[db.get().capacity()];
        db.get().get(buf);
        bufferCheckSum.update(buf, 0, buf.length);
        db.get().clear();
    }
    Assert.assertEquals(bufferCheckSum.getValue(), bufVal);
    bufferCheckSum.reset();

    DurableArray<DurableBuffer> restoredArray = DurableArrayFactory.restore(m_act, null, gtypes, handler,
            false);
    for (int i = 0; i < capacity; i++) {
        DurableBuffer<NonVolatileMemAllocator> db = restoredArray.get(i);
        Assert.assertNotNull(db);
        byte buf[] = new byte[db.get().capacity()];
        db.get().get(buf);
        bufferCheckSum.update(buf, 0, buf.length);
        db.get().clear();
    }
    Assert.assertEquals(bufferCheckSum.getValue(), bufVal);

    bufferCheckSum.reset();
    Iterator<DurableBuffer> itr = restoredArray.iterator();
    int val = 0;
    while (itr.hasNext()) {
        DurableBuffer<NonVolatileMemAllocator> db = itr.next();
        Assert.assertNotNull(db);
        byte buf[] = new byte[db.get().capacity()];
        db.get().get(buf);
        bufferCheckSum.update(buf, 0, buf.length);
        db.get().clear();
        val++;
    }
    Assert.assertEquals(val, capacity);
    Assert.assertEquals(bufferCheckSum.getValue(), bufVal);

    restoredArray.destroy();
}

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

/**
 * Reads data from multiple streams in parallel and puts the data in a queue.
 * @param streams The input streams to read from.
 * @param bufSize The amount of data to read from each stream in each go.
 * @param numThreads Number of threads to use for parallelism.
 * @param boundedBuffer The queue to place the results in.
 *//*  ww w  . ja v a  2 s  .  c  o  m*/

public ParallelStreamReader(Progressable reporter, InputStream[] streams, int bufSize, int numThreads,
        int boundedBufferCapacity, long maxBytesPerStream, boolean computeChecksum, OutputStream[] outs)
        throws IOException {
    this.reporter = reporter;
    this.computeChecksum = computeChecksum;
    this.streams = new InputStream[streams.length];
    this.endOffsets = new long[streams.length];
    if (computeChecksum) {
        this.checksums = new CRC32[streams.length];
    }
    this.outs = outs;
    for (int i = 0; i < streams.length; i++) {
        this.streams[i] = streams[i];
        if (this.streams[i] instanceof DFSDataInputStream) {
            DFSDataInputStream stream = (DFSDataInputStream) this.streams[i];
            // in directory raiding, the block size for each input stream 
            // might be different, so we need to determine the endOffset of
            // each stream by their own block size.
            List<LocatedBlock> blocks = stream.getAllBlocks();
            if (blocks.size() == 0) {
                this.endOffsets[i] = Long.MAX_VALUE;
                if (computeChecksum) {
                    this.checksums[i] = null;
                }
            } else {
                long blockSize = blocks.get(0).getBlockSize();
                this.endOffsets[i] = stream.getPos() + blockSize;
                if (computeChecksum) {
                    this.checksums[i] = new CRC32();
                }
            }
        } else {
            this.endOffsets[i] = Long.MAX_VALUE;
            if (computeChecksum) {
                this.checksums[i] = null;
            }
        }
        streams[i] = null; // Take over ownership of streams.
    }
    this.bufSize = bufSize;
    this.boundedBuffer = new ArrayBlockingQueue<ReadResult>(boundedBufferCapacity);
    if (numThreads > streams.length) {
        this.numThreads = streams.length;
    } else {
        this.numThreads = numThreads;
    }
    this.remainingBytesPerStream = maxBytesPerStream;
    this.slots = new Semaphore(this.numThreads);
    ThreadFactory ParallelStreamReaderFactory = new ThreadFactoryBuilder()
            .setNameFormat("ParallelStreamReader-read-pool-%d").build();
    this.readPool = Executors.newFixedThreadPool(this.numThreads, ParallelStreamReaderFactory);
    this.mainThread = new MainThread();
    mainThread.setName("ParallelStreamReader-main");
}

From source file:it.jnrpe.net.JNRPEProtocolPacket.java

/**
 * Updates the CRC value.//w  w w .  j a  va2s . c om
 */
void updateCRC() {
    setCRC(0);
    CRC32 crcAlg = new CRC32();
    crcAlg.update(toByteArray());

    setCRC((int) crcAlg.getValue());
}

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

/**
 * crc32php64bitlong/*  w  w  w  . ja  v  a 2 s. c o  m*/
 */
public static long crc32AsLong(String input) {
    CRC32 crc32 = new CRC32();
    crc32.update(input.getBytes(Charsets.UTF8));
    return crc32.getValue();
}

From source file:com.monitor.baseservice.utils.XCodeUtil.java

public static byte[] crcUnsigned(byte[] str, byte[] sign) {
    CRC32 crc = new CRC32();
    crc.update(str);/*  w w  w  .  j  a v a2 s .  co  m*/
    crc.update(sign);

    long value = crc.getValue();
    if (value < 0) {
        value = 0xFFFFFFFF & value;
    }
    return longToByteArray(value);
}

From source file:com.zimbra.cs.util.ZipUtil.java

/**
 * Use InfoZIP Unicode Extra Fields (if present) to set the filename
 *///from w w  w  .  j a v  a2  s  .c  o  m
private static String getNameFromUnicodeExtraPathIfPresent(ZipArchiveEntry zae) {
    UnicodePathExtraField unicodePathExtraField = (UnicodePathExtraField) zae
            .getExtraField(UnicodePathExtraField.UPATH_ID);
    if (null == unicodePathExtraField) {
        return null;
    }
    CRC32 crc32 = new CRC32();
    crc32.update(zae.getRawName());
    long origCRC32 = crc32.getValue();

    if (origCRC32 == unicodePathExtraField.getNameCRC32()) {
        String val = convertBytesIfPossible(unicodePathExtraField.getUnicodeName(), StandardCharsets.UTF_8);
        if (null != val) {
            ZimbraLog.misc.debug("ZipUtil name '%s' from unicodeExtraPath", val);
        }
        return val;
    }
    return null;
}