Example usage for java.util.zip Checksum update

List of usage examples for java.util.zip Checksum update

Introduction

In this page you can find the example usage for java.util.zip Checksum update.

Prototype

public void update(byte[] b, int off, int len);

Source Link

Document

Updates the current checksum with the specified array of bytes.

Usage

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

private void writeChecksum(Checksum csum, byte[] data, int off, int len) throws IOException {
    if (csum == null)
        return;//ww  w .j av  a  2 s  .c o m
    csum.reset();
    csum.update(data, off, len);
    long value = csum.getValue();
    // LOG.info("Writing checksum " + csum);
    writeInt((int) (value & 0xFFFFFFFF));
}

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

private void testChecksum(Checksum csum, int value, byte[] data, int off, int len) throws IOException {
    if (csum == null)
        return;/*w ww  . j a v  a2  s .  co  m*/
    csum.reset();
    csum.update(data, off, len);
    if (value != (int) csum.getValue())
        throw new IOException("Checksum failure: " + "Expected " + Integer.toHexString(value) + "; got "
                + Long.toHexString(csum.getValue()));
}

From source file:org.apache.zookeeper.server.persistence.TxnLogToolkit.java

public void dump(Scanner scanner) throws Exception {
    crcFixed = 0;/*from  w w w  .j  a va 2 s.  co m*/

    FileHeader fhdr = new FileHeader();
    fhdr.deserialize(logStream, "fileheader");
    if (fhdr.getMagic() != TXNLOG_MAGIC) {
        throw new TxnLogToolkitException(ExitCode.INVALID_INVOCATION.getValue(), "Invalid magic number for %s",
                txnLogFile.getName());
    }
    System.out.println("ZooKeeper Transactional Log File with dbid " + fhdr.getDbid()
            + " txnlog format version " + fhdr.getVersion());

    if (recoveryMode) {
        fhdr.serialize(recoveryOa, "fileheader");
        recoveryFos.flush();
        filePadding.setCurrentSize(recoveryFos.getChannel().position());
    }

    int count = 0;
    while (true) {
        long crcValue;
        byte[] bytes;
        try {
            crcValue = logStream.readLong("crcvalue");
            bytes = logStream.readBuffer("txnEntry");
        } catch (EOFException e) {
            System.out.println("EOF reached after " + count + " txns.");
            return;
        }
        if (bytes.length == 0) {
            // Since we preallocate, we define EOF to be an
            // empty transaction
            System.out.println("EOF reached after " + count + " txns.");
            return;
        }
        Checksum crc = new Adler32();
        crc.update(bytes, 0, bytes.length);
        if (crcValue != crc.getValue()) {
            if (recoveryMode) {
                if (!force) {
                    printTxn(bytes, "CRC ERROR");
                    if (askForFix(scanner)) {
                        crcValue = crc.getValue();
                        ++crcFixed;
                    }
                } else {
                    crcValue = crc.getValue();
                    printTxn(bytes, "CRC FIXED");
                    ++crcFixed;
                }
            } else {
                printTxn(bytes, "CRC ERROR");
            }
        }
        if (!recoveryMode || verbose) {
            printTxn(bytes);
        }
        if (logStream.readByte("EOR") != 'B') {
            throw new TxnLogToolkitException(ExitCode.UNEXPECTED_ERROR.getValue(),
                    "Last transaction was partial.");
        }
        if (recoveryMode) {
            filePadding.padFile(recoveryFos.getChannel());
            recoveryOa.writeLong(crcValue, "crcvalue");
            recoveryOa.writeBuffer(bytes, "txnEntry");
            recoveryOa.writeByte((byte) 'B', "EOR");
        }
        count++;
    }
}

From source file:com.yattatech.dbtc.facade.SystemFacade.java

private long calculateChecksum(String json) {
    final byte[] input = json.getBytes();
    final Checksum checksum = new CRC32();
    checksum.update(input, 0, input.length);
    final long checksumValue = checksum.getValue();
    if (Debug.isDebugable()) {
        Debug.d(TAG, "calculate checksum for json=" + json + "\nvalue=" + checksumValue);
    }/*from w ww . j  a v  a 2 s .c  o m*/
    return checksumValue;
}

From source file:com.yattatech.dbtc.facade.SystemFacade.java

private boolean isChecksumValid(long checksumValue, String json) {
    final byte[] input = json.getBytes();
    final Checksum checksum = new CRC32();
    checksum.update(input, 0, input.length);
    final long returned = checksum.getValue();
    if (Debug.isDebugable()) {
        Debug.d(TAG, "validate checksum for json=" + json + "\nexpected=" + checksumValue + "\nretrieve="
                + returned);/*www .j av a 2  s . c om*/
    }
    return checksumValue == returned;
}

From source file:com.asakusafw.runtime.util.cache.HadoopFileCacheRepository.java

private long computeChecksum(FileSystem fs, Path file) throws IOException {
    if (LOG.isDebugEnabled()) {
        LOG.debug(MessageFormat.format("Computing checksum: {0}", //$NON-NLS-1$
                file));/*from ww w. j  ava  2 s .  c  o m*/
    }
    Checksum checksum = new CRC32();
    byte[] buf = byteBuffers.get();
    try (FSDataInputStream input = fs.open(file)) {
        while (true) {
            int read = input.read(buf);
            if (read < 0) {
                break;
            }
            checksum.update(buf, 0, read);
        }
    }
    return checksum.getValue();
}

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

protected DurableBuffer<?> genupdDurableBuffer(MneDurableOutputSession<DurableBuffer<?>> s, Checksum cs) {
    DurableBuffer<?> ret = null;/*from  ww  w . j  av a  2  s  .  c  o  m*/
    int sz = m_rand.nextInt(1024 * 1024) + 1024 * 1024;
    ret = s.newDurableObjectRecord(sz);
    if (null != ret) {
        ret.get().clear();
        byte[] rdbytes = RandomUtils.nextBytes(sz);
        Assert.assertNotNull(rdbytes);
        ret.get().put(rdbytes);
        cs.update(rdbytes, 0, rdbytes.length);
        m_totalsize += sz;
    }
    return ret;
}

From source file:com.nridge.connector.common.con_com.crawl.CrawlQueue.java

private long nextCrawlId() {
    UUID uniqueId = UUID.randomUUID();
    byte idBytes[] = uniqueId.toString().getBytes();
    Checksum checksumValue = new CRC32();
    checksumValue.update(idBytes, 0, idBytes.length);

    return checksumValue.getValue();
}

From source file:com.splout.db.dnode.HttpFileExchanger.java

@Override
public void handle(HttpExchange exchange) throws IOException {
    DataInputStream iS = null;// ww  w .  j  ava  2  s .  com
    FileOutputStream writer = null;
    File dest = null;

    String tablespace = null;
    Integer partition = null;
    Long version = null;

    try {
        iS = new DataInputStream(new GZIPInputStream(exchange.getRequestBody()));
        String fileName = exchange.getRequestHeaders().getFirst("filename");
        tablespace = exchange.getRequestHeaders().getFirst("tablespace");
        partition = Integer.valueOf(exchange.getRequestHeaders().getFirst("partition"));
        version = Long.valueOf(exchange.getRequestHeaders().getFirst("version"));

        dest = new File(
                new File(tempDir,
                        DNodeHandler.getLocalStoragePartitionRelativePath(tablespace, partition, version)),
                fileName);

        // just in case, avoid copying the same file concurrently
        // (but we also shouldn't avoid this in other levels of the app)
        synchronized (currentTransfersMonitor) {
            if (currentTransfers.containsKey(dest.toString())) {
                throw new IOException("Incoming file already being transferred - " + dest);
            }
            currentTransfers.put(dest.toString(), new Object());
        }

        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        if (dest.exists()) {
            dest.delete();
        }

        writer = new FileOutputStream(dest);
        byte[] buffer = new byte[config.getInt(FetcherProperties.DOWNLOAD_BUFFER)];

        Checksum checkSum = new CRC32();

        // 1- Read file size
        long fileSize = iS.readLong();
        log.debug("Going to read file [" + fileName + "] of size: " + fileSize);
        // 2- Read file contents
        long readSoFar = 0;

        do {
            long missingBytes = fileSize - readSoFar;
            int bytesToRead = (int) Math.min(missingBytes, buffer.length);
            int read = iS.read(buffer, 0, bytesToRead);
            checkSum.update(buffer, 0, read);
            writer.write(buffer, 0, read);
            readSoFar += read;
            callback.onProgress(tablespace, partition, version, dest, fileSize, readSoFar);
        } while (readSoFar < fileSize);

        // 3- Read CRC
        long expectedCrc = iS.readLong();
        if (expectedCrc == checkSum.getValue()) {
            log.info("File [" + dest.getAbsolutePath() + "] received -> Checksum -- " + checkSum.getValue()
                    + " matches expected CRC [OK]");
            callback.onFileReceived(tablespace, partition, version, dest);
        } else {
            log.error("File received [" + dest.getAbsolutePath() + "] -> Checksum -- " + checkSum.getValue()
                    + " doesn't match expected CRC: " + expectedCrc);
            callback.onBadCRC(tablespace, partition, version, dest);
            dest.delete();
        }
    } catch (Throwable t) {
        log.error(t);
        callback.onError(t, tablespace, partition, version, dest);
        if (dest != null && dest.exists()
                && !t.getMessage().contains("Incoming file already being transferred")) {
            dest.delete();
        }
    } finally {
        if (writer != null) {
            writer.close();
        }
        if (iS != null) {
            iS.close();
        }
        if (dest != null) {
            currentTransfers.remove(dest.toString());
        }
    }
}

From source file:com.cisco.dvbu.ps.deploytool.services.RegressionManagerUtils.java

/**
 * append the checksum value for the entire query to the end of resource URL.
 *   Eliminate any double quote "\"" characters from the URL.
 * //  w  w  w . ja  v  a  2  s.  co  m
 * Examples:
 *   incoming from clause                                  outgoing result
 *   -----------------------                              ----------------
 *   CAT1.SCH1.ViewSales                              --> CAT1.SCH1.ViewSales_1717783081
 *   
 * @param query
 * @param resourceURL
 * @return resourceURL
 */
public static String appendUrlChecksum(String query, String resourceURL) {
    /* 2015-07-06 mtinius - Adding a checksum to the URL allows for unique identification of queries that invoke the same table. 
     * 2015-10-13 mtinius - Moved this code to a separate method from getTableUrl() as it was interfering with the FUNCTIONAL test.
     * */
    // Calculate the CRC for the string to produce a unique identifier
    Checksum checksum = new CRC32();
    long currentLineCheckSumValue = 0L;
    // Make sure there are no line feeds, carriage returns or double spaces in the query.
    String queryTmp = query.replace("\n", " ").replaceAll("\r", " ").trim().replaceAll("  ", " ");
    byte bytes[] = queryTmp.getBytes();
    checksum.reset();
    checksum.update(bytes, 0, bytes.length);
    currentLineCheckSumValue = checksum.getValue();

    // Rewrite the resource URL to include the query checksum value and make sure there are no double quote "\"" characters present.
    resourceURL = resourceURL.replaceAll("\"", "") + "_" + currentLineCheckSumValue;

    return resourceURL;
}