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.cassandra.db.commitlog.CommitLogReader.java

public CommitLogReader() {
    checksum = new CRC32();
    invalidMutations = new HashMap<>();
    buffer = new byte[4096];
}

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

@Test
public void findStreamingFile() throws Exception {
    FileStreamStateHandler rwd = new FileStreamStateHandler();

    File testFilesDir = new File(samplesDir, "/multiple-logs/");
    File[] testFiles = testFilesDir.listFiles((FilenameFilter) new WildcardFileFilter("orders*")); // NON-NLS
    FileAccessState newFAS = new FileAccessState();

    int count = 0;
    File fileToSearchFor = null;//from  ww  w . j  a  va  2 s  . c o  m
    int lineLastRead = 0;
    File fileWritten = null;
    for (File testFile : testFiles) {
        count++;
        FileReader in;
        LineNumberReader reader;

        Long fileCRC = rwd.getFileCrc(testFile);
        if (count == 2) {
            newFAS.currentFileCrc = fileCRC;
            fileToSearchFor = testFile;
        }

        in = new FileReader(testFile);
        reader = new LineNumberReader(in);
        reader.setLineNumber(0);
        String line = reader.readLine();
        int count2 = 0;
        while (line != null) {
            count2++;
            Checksum crcLine = new CRC32();
            final byte[] bytes4Line = line.getBytes();
            crcLine.update(bytes4Line, 0, bytes4Line.length);
            final long lineCRC = crcLine.getValue();
            final int lineNumber = reader.getLineNumber();
            System.out.println("for " + lineNumber + " line CRC is " + lineCRC); // NON-NLS
            if (count2 == 3) {
                newFAS.currentLineCrc = lineCRC;
                newFAS.currentLineNumber = lineNumber;
                newFAS.lastReadTime = System.currentTimeMillis();
                lineLastRead = lineNumber;
            }
            line = reader.readLine();
        }
        fileWritten = AbstractFileStreamStateHandler.writeState(newFAS, testFilesDir, "TestStream"); // NON-NLS
        Utils.close(reader);
    }

    final File findLastProcessed = rwd.findStreamingFile(newFAS, testFiles);
    assertEquals(fileToSearchFor, findLastProcessed);
    final int lineLastReadRecorded = rwd.checkLine(findLastProcessed, newFAS);
    assertEquals(lineLastRead, lineLastReadRecorded);
    fileWritten.delete();
}

From source file:com.threewks.thundr.util.Encoder.java

public Encoder crc32() {
    Checksum checksum = new CRC32();
    checksum.update(data, 0, data.length);
    long checksumValue = checksum.getValue();
    data = Long.toHexString(checksumValue).getBytes();
    return unhex();
}

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.
 * // w w w .ja v a  2s.c  o  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.trancecode.xproc.step.HashStepProcessor.java

@Override
protected void execute(final StepInput input, final StepOutput output) {
    final XdmNode sourceDocument = input.readNode(XProcPorts.SOURCE);
    final String value = input.getOptionValue(XProcOptions.VALUE);
    assert value != null;
    LOG.trace("value = {}", value);
    final String algorithm = input.getOptionValue(XProcOptions.ALGORITHM);
    assert algorithm != null;
    LOG.trace("algorithm = {}", algorithm);
    final String match = input.getOptionValue(XProcOptions.MATCH);
    assert match != null;
    LOG.trace("match = {}", match);
    final String version = input.getOptionValue(XProcOptions.VERSION);
    LOG.trace("version = {}", version);

    final String hashValue;
    if (StringUtils.equalsIgnoreCase("crc", algorithm)) {
        if ("32".equals(version) || version == null) {
            final CRC32 crc32 = new CRC32();
            crc32.update(value.getBytes());
            hashValue = Long.toHexString(crc32.getValue());
        } else {/*from  w ww . jav  a2  s . c o m*/
            throw XProcExceptions.xc0036(input.getLocation());
        }
    } else if (StringUtils.equalsIgnoreCase("md", algorithm)) {
        if (version == null || "5".equals(version)) {
            hashValue = DigestUtils.md5Hex(value);
        } else {
            throw XProcExceptions.xc0036(input.getLocation());
        }
    } else if (StringUtils.equalsIgnoreCase("sha", algorithm)) {
        if (version == null || "1".equals(version)) {
            hashValue = DigestUtils.shaHex(value);
        } else if ("256".equals(version)) {
            hashValue = DigestUtils.sha256Hex(value);
        } else if ("384".equals(version)) {
            hashValue = DigestUtils.sha384Hex(value);
        } else if ("512".equals(version)) {
            hashValue = DigestUtils.sha512Hex(value);
        } else {
            throw XProcExceptions.xc0036(input.getLocation());
        }
    } else {
        throw XProcExceptions.xc0036(input.getLocation());
    }

    final SaxonProcessorDelegate hashDelegate = new AbstractSaxonProcessorDelegate() {
        @Override
        public boolean startDocument(final XdmNode node, final SaxonBuilder builder) {
            return true;
        }

        @Override
        public void endDocument(final XdmNode node, final SaxonBuilder builder) {
        }

        @Override
        public EnumSet<NextSteps> startElement(final XdmNode element, final SaxonBuilder builder) {
            builder.text(hashValue);
            return EnumSet.noneOf(NextSteps.class);
        }

        @Override
        public void endElement(final XdmNode node, final SaxonBuilder builder) {
            builder.endElement();
        }

        @Override
        public void attribute(final XdmNode node, final SaxonBuilder builder) {
            builder.attribute(node.getNodeName(), hashValue);
        }

        @Override
        public void comment(final XdmNode node, final SaxonBuilder builder) {
            builder.comment(hashValue);
        }

        @Override
        public void processingInstruction(final XdmNode node, final SaxonBuilder builder) {
            builder.processingInstruction(node.getNodeName().getLocalName(), hashValue);
        }

        @Override
        public void text(final XdmNode node, final SaxonBuilder builder) {
            builder.text(hashValue);
        }
    };

    final SaxonProcessor hashProcessor = new SaxonProcessor(input.getPipelineContext().getProcessor(),
            SaxonProcessorDelegates.forXsltMatchPattern(input.getPipelineContext().getProcessor(), match,
                    input.getStep().getNode(), hashDelegate, new CopyingSaxonProcessorDelegate()));

    final XdmNode result = hashProcessor.apply(sourceDocument);
    output.writeNodes(XProcPorts.RESULT, result);
}

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

public LzopInputStream(InputStream in) throws IOException {
    super(in, new LzoDecompressor1x());
    this.flags = readHeader();
    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();
    this.eof = false;
    // logState();
}

From source file:com.espertech.esper.core.context.mgr.ContextControllerHashedGetterCRC32Serialized.java

public Object get(EventBean eventBean) throws PropertyAccessException {
    EventBean[] events = new EventBean[] { eventBean };

    Object[] parameters = new Object[evaluators.length];
    for (int i = 0; i < serializers.length; i++) {
        parameters[i] = evaluators[i].evaluate(events, true, null);
    }/* w  w w .  j a  v  a 2 s  .c  o  m*/

    byte[] bytes;
    try {
        bytes = SerializerFactory.serialize(serializers, parameters);
    } catch (IOException e) {
        log.error("Exception serializing parameters for computing consistent hash for statement '"
                + statementName + "': " + e.getMessage(), e);
        bytes = new byte[0];
    }

    CRC32 crc = new CRC32();
    crc.update(bytes);
    long value = crc.getValue() % granularity;

    int result = (int) value;
    if (result >= 0) {
        return result;
    }
    return -result;
}

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

@Test(dependsOnMethods = { "testGenChunkBuffers" })
public void testCheckChunkBuffers() {
    Checksum cs = new CRC32();
    cs.reset();/*from  www . j a va 2 s  . c om*/
    NonVolatileMemAllocator act = new NonVolatileMemAllocator(
            Utils.getNonVolatileMemoryAllocatorService("pmalloc"), 1L, "./pmchunkbuffertest.dat", 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;
        }
    });
    DurableChunk<NonVolatileMemAllocator> mch;
    mch = act.retrieveChunk(act.getHandler(m_keyid));
    Assert.assertNotNull(mch);
    long bufcnt = mch.getSize() / m_bufsize;

    ChunkBuffer ckbuf;
    byte[] buf;
    for (long idx = 0; idx < bufcnt; ++idx) {
        ckbuf = mch.getChunkBuffer(idx * m_bufsize, m_bufsize);
        Assert.assertNotNull(ckbuf);
        buf = new byte[m_bufsize];
        ckbuf.get().clear();
        ckbuf.get().get(buf);
        cs.update(buf, 0, buf.length);
    }
    act.close();

    Assert.assertEquals(m_checksum, cs.getValue());
    Assert.assertEquals(m_count, bufcnt);
    System.out.println(
            String.format("The checksum of chunk buffers are %d, Total count is %d", m_checksum, m_count));
}

From source file:org.ambiance.codec.YEncDecoder.java

/**
 * Decode a single file/*from  w w  w . j  ava2  s.  c o m*/
 */
public void decode(File input) throws DecoderException {
    try {
        YEncFile yencFile = new YEncFile(input);

        // Get the output file
        StringBuffer outputName = new StringBuffer(outputDirName);
        outputName.append(File.separator);
        outputName.append(yencFile.getHeader().getName());
        RandomAccessFile output = new RandomAccessFile(outputName.toString(), "rw");

        // Place the pointer to the begining of data to decode
        long pos = yencFile.getDataBegin();
        yencFile.getInput().seek(pos);

        // Bufferise the file
        // TODO - A Amliorer
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while (pos < yencFile.getDataEnd()) {
            baos.write(yencFile.getInput().read());
            pos++;
        }

        byte[] buff = decoder.decode(baos.toByteArray());

        // Write and close output
        output.write(buff);
        output.close();

        // Warn if CRC32 check is not OK
        CRC32 crc32 = new CRC32();
        crc32.update(buff);
        if (!yencFile.getTrailer().getCrc32().equals(Long.toHexString(crc32.getValue()).toUpperCase()))
            throw new DecoderException("Error in CRC32 check.");

    } catch (YEncException ye) {
        throw new DecoderException("Input file is not a YEnc file or contains error : " + ye.getMessage());
    } catch (FileNotFoundException fnfe) {
        throw new DecoderException("Enable to create output file : " + fnfe.getMessage());
    } catch (IOException ioe) {
        throw new DecoderException("Enable to read input file : " + ioe.getMessage());
    }
}

From source file:eu.semlibproject.annotationserver.managers.UtilsManager.java

/**
 * Compute the CRC32 checksum of a String. This can be useful to
 * short an URL.//from   w w w.  j  a v a2  s.  c  o  m
 * 
 * @param text  the text from which the checksum will be computed
 * @return      the CRC32 checksum
 */
public String CRC32(String text) {
    CRC32 checksumer = new CRC32();
    checksumer.update(text.getBytes());
    String finalhash = Long.toHexString(checksumer.getValue());
    // correctly format the finalHash (e.g. number starting with 00 that was stripped)
    return StringUtils.leftPad(finalhash, 8, "0");
}