Package java.util.zip

Examples of java.util.zip.Checksum


     */
    @Test
    public void testReplicateRequest() throws Exception {
       
        // serialize the request
        Checksum csumAlgo = new CRC32();
        ReusableBuffer data = testEntry.serialize(csumAlgo);
        csumAlgo.reset();
       
        client.replicate(testLSN, data).get();
    }
View Full Code Here


                {
                    if (logger.isDebugEnabled())
                        logger.debug("Reading mutation at " + reader.getFilePointer());

                    long claimedCRC32;
                    Checksum checksum = new CRC32();
                    int serializedSize;
                    try
                    {
                        // any of the reads may hit EOF
                        serializedSize = reader.readInt();
                        // RowMutation must be at LEAST 10 bytes:
                        // 3 each for a non-empty Table and Key (including the 2-byte length from
                        // writeUTF/writeWithShortLength) and 4 bytes for column count.
                        // This prevents CRC by being fooled by special-case garbage in the file; see CASSANDRA-2128
                        if (serializedSize < 10)
                            break;
                        long claimedSizeChecksum = reader.readLong();
                        checksum.update(serializedSize);
                        if (checksum.getValue() != claimedSizeChecksum)
                            break; // entry wasn't synced correctly/fully.  that's ok.

                        if (serializedSize > bytes.length)
                            bytes = new byte[(int) (1.2 * serializedSize)];
                        reader.readFully(bytes, 0, serializedSize);
                        claimedCRC32 = reader.readLong();
                    }
                    catch(EOFException eof)
                    {
                        break; // last CL entry didn't get completely written.  that's ok.
                    }

                    checksum.update(bytes, 0, serializedSize);
                    if (claimedCRC32 != checksum.getValue())
                    {
                        // this entry must not have been fsynced.  probably the rest is bad too,
                        // but just in case there is no harm in trying them (since we still read on an entry boundary)
                        continue;
                    }
View Full Code Here

public class Adler32Test extends TestCase {

    static private int N_ITERS = 1000;

    public void testRandomAdler32ByteArray() {
  Checksum javaChecksum = new java.util.zip.Adler32();
  Checksum jeChecksum = new com.sleepycat.je.utilint.Adler32();
  Random rnd = new Random();
  for (int i = 0; i < N_ITERS; i++) {
      int nBytes = rnd.nextInt(65535);
      byte[] b = new byte[nBytes];
      rnd.nextBytes(b);
      javaChecksum.reset();
      jeChecksum.reset();
      javaChecksum.update(b, 0, nBytes);
      jeChecksum.update(b, 0, nBytes);
      assertEquals(javaChecksum.getValue(), jeChecksum.getValue());
  }
    }
View Full Code Here

      assertEquals(javaChecksum.getValue(), jeChecksum.getValue());
  }
    }

    public void xtestRandomAdler32ByteArrayPerformance() {
  Checksum javaChecksum = new java.util.zip.Adler32();
  Checksum jeChecksum = new com.sleepycat.je.utilint.Adler32();
  Random rnd = new Random();
  byte[][] baa = new byte[N_ITERS][];
  int[] lengths = new int[N_ITERS];
  long totalBytes = 0;
  for (int i = 0; i < N_ITERS; i++) {
View Full Code Here

  long endTime = System.currentTimeMillis();
  return (endTime - startTime);
    }

    public void testRandomAdler32SingleBytes() {
  Checksum javaChecksum = new java.util.zip.Adler32();
  Checksum jeChecksum = new com.sleepycat.je.utilint.Adler32();
  Random rnd = new Random();
  for (int i = 0; i < N_ITERS; i++) {
      int nBytes = rnd.nextInt(65535);
      javaChecksum.reset();
      jeChecksum.reset();
      for (int j = 0; j < nBytes; j++) {
    byte b = (byte) (rnd.nextInt(256) & 0xff);
    javaChecksum.update(b);
    jeChecksum.update(b);
      }
      assertEquals(javaChecksum.getValue(), jeChecksum.getValue());
  }
    }
View Full Code Here

    private ByteBuffer addPrevOffsetAndChecksum(ByteBuffer destBuffer,
                                                long lastOffset,
                                                int entrySize) {

        Checksum checksum = new Adler32();
           
        /* Add the prev pointer */
        destBuffer.position(HEADER_PREV_OFFSET);
        LogUtils.writeUnsignedInt(destBuffer, lastOffset);

        /* Now calculate the checksum and write it into the buffer. */
        checksum.update(destBuffer.array(), CHECKSUM_BYTES,
                        (entrySize - CHECKSUM_BYTES));
        destBuffer.position(0);
        LogUtils.putUnsignedInt(destBuffer, checksum.getValue());

        /* Leave this buffer ready for copying into another buffer. */
        destBuffer.position(0);

        return destBuffer;
View Full Code Here

            }

            byte data[] = new byte[size];
            reader.readFully(offset+BATCH_CONTROL_RECORD_SIZE, data);

            Checksum checksum = new Adler32();
            checksum.update(data, 0, data.length);

            if( expectedChecksum!=checksum.getValue() ) {
                return -1;
            }

        }
        return size;
View Full Code Here

                // Now we can fill in the batch control record properly.
                buff.reset();
                buff.skip(5+Journal.BATCH_CONTROL_RECORD_MAGIC.length);
                buff.writeInt(sequence.getLength()-Journal.BATCH_CONTROL_RECORD_SIZE);
                if( journal.isChecksum() ) {
                  Checksum checksum = new Adler32();
                  checksum.update(sequence.getData(), sequence.getOffset()+Journal.BATCH_CONTROL_RECORD_SIZE, sequence.getLength()-Journal.BATCH_CONTROL_RECORD_SIZE);
                  buff.writeLong(checksum.getValue());
                }

                // Now do the 1 big write.
                file.seek(wb.offset);
                if (maxStat > 0) {
View Full Code Here

                // Using Adler-32 instead of CRC-32 because it's much faster and
                // it's
                // weakness for short messages with few hundred bytes is not a
                // factor in this case since we know
                // our write batches are going to much larger.
                Checksum checksum = new Adler32();
                for (PageWrite w : batch) {
                    try {
                        checksum.update(w.diskBound, 0, pageSize);
                    } catch (Throwable t) {
                        throw IOExceptionSupport.create(
                                "Cannot create recovery file. Reason: " + t, t);
                    }
                }

                // Can we shrink the recovery buffer??
                if (recoveryPageCount > recoveryFileMaxPageCount) {
                    int t = Math.max(recoveryFileMinPageCount, batch.size());
                    recoveryFile.setLength(recoveryFileSizeForPages(t));
                }

                // Record the page writes in the recovery buffer.
                recoveryFile.seek(0);
                // Store the next tx id...
                recoveryFile.writeLong(nextTxid.get());
                // Store the checksum for thw write batch so that on recovery we
                // know if we have a consistent
                // write batch on disk.
                recoveryFile.writeLong(checksum.getValue());
                // Write the # of pages that will follow
                recoveryFile.writeInt(batch.size());

                // Write the pages.
                recoveryFile.seek(RECOVERY_FILE_HEADER_SIZE);
View Full Code Here

        long nextTxId = recoveryFile.readLong();
        long expectedChecksum = recoveryFile.readLong();
        int pageCounter = recoveryFile.readInt();
       
        recoveryFile.seek(RECOVERY_FILE_HEADER_SIZE);
        Checksum checksum = new Adler32();
        LinkedHashMap<Long, byte[]> batch = new LinkedHashMap<Long, byte[]>();
        try {
            for (int i = 0; i < pageCounter; i++) {
                long offset = recoveryFile.readLong();
                byte []data = new byte[pageSize];
                if( recoveryFile.read(data, 0, pageSize) != pageSize ) {
                    // Invalid recovery record, Could not fully read the data". Probably due to a partial write to the recovery buffer
                    return nextTxId;
                }
                checksum.update(data, 0, pageSize);
                batch.put(offset, data);
            }
        } catch (Exception e) {
            // If an error occurred it was cause the redo buffer was not full written out correctly.. so don't redo it.
            // as the pages should still be consistent.
            LOG.debug("Redo buffer was not fully intact: ", e);
            return nextTxId;
        }
       
        recoveryPageCount = pageCounter;
       
        // If the checksum is not valid then the recovery buffer was partially written to disk.
        if( checksum.getValue() != expectedChecksum ) {
            return nextTxId;
        }
       
        // Re-apply all the writes in the recovery buffer.
        for (Map.Entry<Long, byte[]> e : batch.entrySet()) {
View Full Code Here

TOP

Related Classes of java.util.zip.Checksum

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.