Java Code Examples for java.util.zip.Checksum#update()

The following examples show how to use java.util.zip.Checksum#update() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TransformerUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Compute the CRC-32 of the contents of a stream.
 * \r\n and \r are both normalized to \n for purposes of the calculation.
 */
private static String computeCrc32(InputStream is) throws IOException {
    Checksum crc = new CRC32();
    int last = -1;
    int curr;
    while ((curr = is.read()) != -1) {
        if (curr != '\n' && last == '\r') {
            crc.update('\n');
        }
        if (curr != '\r') {
            crc.update(curr);
        }
        last = curr;
    }
    if (last == '\r') {
        crc.update('\n');
    }
    int val = (int)crc.getValue();
    String hex = Integer.toHexString(val);
    while (hex.length() < 8) {
        hex = "0" + hex; // NOI18N
    }
    return hex;
}
 
Example 2
Source File: JoH.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isOldVersion(Context context) {
    try {
        final Signature[] pinfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES).signatures;
        if (pinfo.length == 1) {
            final Checksum s = new CRC32();
            final byte[] ba = pinfo[0].toByteArray();
            s.update(ba, 0, ba.length);
            if (s.getValue() == 2009579833) return true;
        }
    } catch (Exception e) {
        Log.d(TAG, "exception: " + e);
    }
    return false;
}
 
Example 3
Source File: JoH.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isOldVersion(Context context) {
    try {
        final Signature[] pinfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES).signatures;
        if (pinfo.length == 1) {
            final Checksum s = new CRC32();
            final byte[] ba = pinfo[0].toByteArray();
            s.update(ba, 0, ba.length);
            if (s.getValue() == 2009579833) return true;
        }
    } catch (Exception e) {
        Log.d(TAG, "exception: " + e);
    }
    return false;
}
 
Example 4
Source File: JoH.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isOldVersion(Context context) {
    try {
        final Signature[] pinfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES).signatures;
        if (pinfo.length == 1) {
            final Checksum s = new CRC32();
            final byte[] ba = pinfo[0].toByteArray();
            s.update(ba, 0, ba.length);
            if (s.getValue() == 2009579833) return true;
        }
    } catch (Exception e) {
        Log.d(TAG, "exception: " + e);
    }
    return false;
}
 
Example 5
Source File: GenSort.java    From hadoop with Apache License 2.0 5 votes vote down vote up
public static void outputRecords(OutputStream out,
                                 boolean useAscii,
                                 Unsigned16 firstRecordNumber,
                                 Unsigned16 recordsToGenerate,
                                 Unsigned16 checksum
                                 ) throws IOException {
  byte[] row = new byte[100];
  Unsigned16 recordNumber = new Unsigned16(firstRecordNumber);
  Unsigned16 lastRecordNumber = new Unsigned16(firstRecordNumber);
  Checksum crc = new PureJavaCrc32();
  Unsigned16 tmp = new Unsigned16();
  lastRecordNumber.add(recordsToGenerate);
  Unsigned16 ONE = new Unsigned16(1);
  Unsigned16 rand = Random16.skipAhead(firstRecordNumber);
  while (!recordNumber.equals(lastRecordNumber)) {
    Random16.nextRand(rand);
    if (useAscii) {
      generateAsciiRecord(row, rand, recordNumber);
    } else {
      generateRecord(row, rand, recordNumber);
    }
    if (checksum != null) {
      crc.reset();
      crc.update(row, 0, row.length);
      tmp.set(crc.getValue());
      checksum.add(tmp);
    }
    recordNumber.add(ONE);
    out.write(row);
  }
}
 
Example 6
Source File: ByteBufferUtils.java    From OpenRS with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Calculates the CRC32 checksum of the specified buffer.
 * 
 * @param buffer
 *            The buffer.
 * @return The CRC32 checksum.
 */
public static int getCrcChecksum(ByteBuffer buffer) {
	Checksum crc = new CRC32();
	for (int i = 0; i < buffer.limit(); i++) {
		crc.update(buffer.get(i));
	}
	return (int) crc.getValue();
}
 
Example 7
Source File: GenSort.java    From big-c with Apache License 2.0 5 votes vote down vote up
public static void outputRecords(OutputStream out,
                                 boolean useAscii,
                                 Unsigned16 firstRecordNumber,
                                 Unsigned16 recordsToGenerate,
                                 Unsigned16 checksum
                                 ) throws IOException {
  byte[] row = new byte[100];
  Unsigned16 recordNumber = new Unsigned16(firstRecordNumber);
  Unsigned16 lastRecordNumber = new Unsigned16(firstRecordNumber);
  Checksum crc = new PureJavaCrc32();
  Unsigned16 tmp = new Unsigned16();
  lastRecordNumber.add(recordsToGenerate);
  Unsigned16 ONE = new Unsigned16(1);
  Unsigned16 rand = Random16.skipAhead(firstRecordNumber);
  while (!recordNumber.equals(lastRecordNumber)) {
    Random16.nextRand(rand);
    if (useAscii) {
      generateAsciiRecord(row, rand, recordNumber);
    } else {
      generateRecord(row, rand, recordNumber);
    }
    if (checksum != null) {
      crc.reset();
      crc.update(row, 0, row.length);
      tmp.set(crc.getValue());
      checksum.add(tmp);
    }
    recordNumber.add(ONE);
    out.write(row);
  }
}
 
Example 8
Source File: DurableHashSetNGTest.java    From mnemonic with Apache License 2.0 5 votes vote down vote up
protected DurableBuffer<NonVolatileMemAllocator>
    genuptBuffer(NonVolatileMemAllocator act, Checksum cs, int size) {
  DurableBuffer<NonVolatileMemAllocator> ret = null;
  ret = act.createBuffer(size, false);
  if (null == ret) {
    throw new OutOfHybridMemory("Create Durable Buffer Failed.");
  }
  ret.get().clear();
  byte[] rdbytes = RandomUtils.nextBytes(size);
  Assert.assertNotNull(rdbytes);
  ret.get().put(rdbytes);
  cs.update(rdbytes, 0, rdbytes.length);
  ret.get().clear();
  return ret;
}
 
Example 9
Source File: Checksums.java    From feeyo-redisproxy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void updateLong(Checksum checksum, long input) {
    checksum.update((byte) (input >> 56));
    checksum.update((byte) (input >> 48));
    checksum.update((byte) (input >> 40));
    checksum.update((byte) (input >> 32));
    checksum.update((byte) (input >> 24));
    checksum.update((byte) (input >> 16));
    checksum.update((byte) (input >> 8));
    checksum.update((byte) input /* >> 0 */);
}
 
Example 10
Source File: ChecksumBase.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void testDirectByteBuffer(Checksum checksum, long expected) {
    checksum.reset();
    ByteBuffer bb = ByteBuffer.allocateDirect(BYTES_123456789.length);
    bb.put(BYTES_123456789);
    bb.rewind();
    checksum.update(bb);
    checkChecksum(checksum, expected);
}
 
Example 11
Source File: GenSort.java    From pravega-samples with Apache License 2.0 5 votes vote down vote up
public static void outputRecords(OutputStream out,
                                 boolean useAscii,
                                 Unsigned16 firstRecordNumber,
                                 Unsigned16 recordsToGenerate,
                                 Unsigned16 checksum
                                 ) throws IOException {
  byte[] row = new byte[100];
  Unsigned16 recordNumber = new Unsigned16(firstRecordNumber);
  Unsigned16 lastRecordNumber = new Unsigned16(firstRecordNumber);
  Checksum crc = new PureJavaCrc32();
  Unsigned16 tmp = new Unsigned16();
  lastRecordNumber.add(recordsToGenerate);
  Unsigned16 ONE = new Unsigned16(1);
  Unsigned16 rand = Random16.skipAhead(firstRecordNumber);
  while (!recordNumber.equals(lastRecordNumber)) {
    Random16.nextRand(rand);
    if (useAscii) {
      generateAsciiRecord(row, rand, recordNumber);
    } else {
      generateRecord(row, rand, recordNumber);
    }
    if (checksum != null) {
      crc.reset();
      crc.update(row, 0, row.length);
      tmp.set(crc.getValue());
      checksum.add(tmp);
    }
    recordNumber.add(ONE);
    out.write(row);
  }
}
 
Example 12
Source File: CRCTest.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
private static void updateSerialSlow(Checksum crc, byte[] b, int start, int length) {
    for (int i = 0; i < length; i++)
        crc.update(b[i+start]);
    crc.getValue();
}
 
Example 13
Source File: CRCTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private static void updateSerial(Checksum crc, byte[] b, int start, int length) {
    for (int i = 0; i < length; i++)
        crc.update(b[i+start]);
}
 
Example 14
Source File: CRCTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private static void updateSerialSlow(Checksum crc, byte[] b, int start, int length) {
    for (int i = 0; i < length; i++)
        crc.update(b[i+start]);
    crc.getValue();
}
 
Example 15
Source File: CRCTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static void updateSerial(Checksum crc, byte[] b, int start, int length) {
    for (int i = 0; i < length; i++)
        crc.update(b[i+start]);
}
 
Example 16
Source File: Book.java    From BookyMcBookface with GNU General Public License v3.0 4 votes vote down vote up
private static long crc32(String input) {
    byte[] bytes = input.getBytes();
    Checksum checksum = new CRC32();
    checksum.update(bytes, 0, bytes.length);
    return checksum.getValue();
}
 
Example 17
Source File: BlockReadBuffer.java    From zbus-server with MIT License 4 votes vote down vote up
public static long calcChecksum(byte[] data){
	Checksum crc = new CRC32();
	crc.update(data, 0, data.length);
	return crc.getValue();
}
 
Example 18
Source File: ZooLog.java    From zooadmin with MIT License 4 votes vote down vote up
/**
 * 读取多行日志
 * @param total 读取的行数
 * @return
 * @throws IOException
 */
public String getLastLog(int total) throws IOException {
    StringBuilder sb=new StringBuilder(1024);
    FileInputStream fis = new FileInputStream(this.logFile);
    BinaryInputArchive logStream = BinaryInputArchive.getArchive(fis);
    FileHeader fhdr = new FileHeader();
    fhdr.deserialize(logStream, "fileheader");

    if (fhdr.getMagic() != FileTxnLog.TXNLOG_MAGIC) {
        return "Invalid magic number for " + logFile;
    }
    sb.append("ZooKeeper Transactional Log File with dbid "
            + fhdr.getDbid() + " txnlog format version "
            + fhdr.getVersion()+"\r\n");
    int count=0;
    while (count<total) {
        long crcValue;
        byte[] bytes;
        try {
            crcValue = logStream.readLong("crcvalue");
            bytes = logStream.readBuffer("txnEntry");
        } catch (EOFException e) {
            sb.append("EOF reached after " + count + " txns.\r\n");
            break;
        }
        if (bytes.length == 0) {
            // Since we preallocate, we define EOF to be an
            // empty transaction
            sb.append("EOF reached after " + count + " txns.\r\n");
            break;
        }
        Checksum crc = new Adler32();
        crc.update(bytes, 0, bytes.length);
        if (crcValue != crc.getValue()) {
            throw new IOException("CRC doesn't match " + crcValue +
                    " vs " + crc.getValue());
        }
        TxnHeader hdr = new TxnHeader();
        Record txn = SerializeUtils.deserializeTxn(bytes, hdr);
        sb.append(DateFormat.getDateTimeInstance(DateFormat.SHORT,
                DateFormat.LONG).format(new Date(hdr.getTime()))
                + " session 0x"
                + Long.toHexString(hdr.getClientId())
                + " cxid 0x"
                + Long.toHexString(hdr.getCxid())
                + " zxid 0x"
                + Long.toHexString(hdr.getZxid())
                + " " + ZooLog.op2String(hdr.getType()) + " " + txn+"\r\n");
        if (logStream.readByte("EOR") != 'B') {
            sb.append("Last transaction was partial.");
        }
        count++;
    }
    return sb.toString();
}
 
Example 19
Source File: MneMapredChunkDataTest.java    From mnemonic with Apache License 2.0 4 votes vote down vote up
@Test(enabled = true, dependsOnMethods = {"testWriteChunkData"})
public void testReadChunkData() throws Exception {
  List<String> partfns = new ArrayList<String>();
  long reccnt = 0L;
  long tsize = 0L;
  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)) {
      partfns.add(listfiles[idx].getName());
    }
  }
  Collections.sort(partfns); // keep the order for checksum
  for (int idx = 0; idx < partfns.size(); ++idx) {
    System.out.println(String.format("Verifying : %s", partfns.get(idx)));
    FileSplit split = new FileSplit(
            new Path(m_workdir, partfns.get(idx)), 0, 0L, new String[0]);
    InputFormat<NullWritable, MneDurableInputValue<DurableChunk<?>>> inputFormat =
            new MneInputFormat<MneDurableInputValue<DurableChunk<?>>, DurableChunk<?>>();
    RecordReader<NullWritable, MneDurableInputValue<DurableChunk<?>>> reader =
            inputFormat.getRecordReader(split, m_conf, null);
    MneDurableInputValue<DurableChunk<?>> dchkval = null;
    NullWritable dchkkey = reader.createKey();
    while (true) {
      dchkval = reader.createValue();
      if (reader.next(dchkkey, dchkval)) {
        byte b;
        for (int j = 0; j < dchkval.getValue().getSize(); ++j) {
          b = unsafe.getByte(dchkval.getValue().get() + j);
          cs.update(b);
        }
        tsize += dchkval.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 chunk is %d", m_checksum));
}
 
Example 20
Source File: DoubleCopy.java    From journalkeeper with Apache License 2.0 4 votes vote down vote up
private long getChecksum(byte[] data) {
    Checksum crc = new CRC32();
    crc.update(data, 0, data.length);
    return crc.getValue();
}