java.util.zip.CRC32 Java Examples

The following examples show how to use java.util.zip.CRC32. 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: BlobTest.java    From gemfirexd-oss with Apache License 2.0 7 votes vote down vote up
/**
 * Get the checksum of a stream, reading its contents entirely and closing it.
 */
private long getStreamCheckSum(InputStream in) throws Exception {
  CRC32 sum = new CRC32();

  byte[] buf = new byte[32 * 1024];

  for (;;) {
    int read = in.read(buf);
    if (read == -1) {
      break;
    }
    sum.update(buf, 0, read);
  }
  in.close();
  return sum.getValue();
}
 
Example #2
Source File: JarVersionCreator.java    From reladomo with Apache License 2.0 6 votes vote down vote up
private void writeVersionInfo(PrintWriter writer, CRC32 fullCrc) throws IOException
{
    if (fileChecksums.size() == 0) return;
    Collections.sort(fileChecksums);
    String currentPackage = null;
    for(int i=0;i<fileChecksums.size();i++)
    {
        String vPath = fileChecksums.get(i).vPath;
        fullCrc.update(vPath.getBytes("UTF8"));
        currentPackage = writePackage(writer, currentPackage, vPath);
        String filename = vPath.substring(currentPackage.length());
        writer.print(filename);
        writer.print(": ");
        String crc = Long.toHexString(fileChecksums.get(i).crc);
        writer.println(crc);
        fullCrc.update(crc.getBytes("UTF8"));
    }
}
 
Example #3
Source File: MneMapreduceChunkDataTest.java    From mnemonic with Apache License 2.0 6 votes vote down vote up
@Test(enabled = true)
public void testWriteChunkData() throws Exception {
  NullWritable nada = NullWritable.get();
  MneDurableOutputSession<DurableChunk<?>> sess =
      new MneDurableOutputSession<DurableChunk<?>>(m_tacontext, null,
          MneConfigHelper.DEFAULT_OUTPUT_CONFIG_PREFIX);
  MneDurableOutputValue<DurableChunk<?>> mdvalue =
      new MneDurableOutputValue<DurableChunk<?>>(sess);
  OutputFormat<NullWritable, MneDurableOutputValue<DurableChunk<?>>> outputFormat =
      new MneOutputFormat<MneDurableOutputValue<DurableChunk<?>>>();
  RecordWriter<NullWritable, MneDurableOutputValue<DurableChunk<?>>> writer =
      outputFormat.getRecordWriter(m_tacontext);
  DurableChunk<?> dchunk = null;
  Checksum cs = new CRC32();
  cs.reset();
  for (int i = 0; i < m_reccnt; ++i) {
    dchunk = genupdDurableChunk(sess, cs);
    Assert.assertNotNull(dchunk);
    writer.write(nada, mdvalue.of(dchunk));
  }
  m_checksum = cs.getValue();
  writer.close(m_tacontext);
  sess.close();
}
 
Example #4
Source File: JarProcessor.java    From shrinker with Apache License 2.0 6 votes vote down vote up
private ByteArrayOutputStream zipEntries(List<Pair<String, byte[]>> entryList) throws IOException {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream(8192);
    try (ZipOutputStream jar = new ZipOutputStream(buffer)) {
        jar.setMethod(ZipOutputStream.STORED);
        final CRC32 crc = new CRC32();
        for (Pair<String, byte[]> entry : entryList) {
            byte[] bytes = entry.second;
            final ZipEntry newEntry = new ZipEntry(entry.first);
            newEntry.setMethod(ZipEntry.STORED); // chose STORED method
            crc.reset();
            crc.update(entry.second);
            newEntry.setCrc(crc.getValue());
            newEntry.setSize(bytes.length);
            writeEntryToJar(newEntry, bytes, jar);
        }
        jar.flush();
    }
    return buffer;
}
 
Example #5
Source File: ZipGenerator.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
public void addMimeTypeFile(String statedPath, String actualPath) throws IOException  {
  //  byte data[] = new byte[BUFFER];
    CRC32 crc = new CRC32();
    
  //  FileInputStream fi = new FileInputStream(actualPath);
  //  BufferedInputStream origin = new BufferedInputStream(fi, BUFFER);
    out.setLevel(0);
    ZipEntry entry = new ZipEntry(statedPath);
    entry.setExtra(null);
    names.add(statedPath);
    String contents = "application/epub+zip";
    crc.update(contents.getBytes());
    entry.setCompressedSize(contents.length());
    entry.setSize(contents.length());
    entry.setCrc(crc.getValue());
    entry.setMethod(ZipEntry.STORED);
    out.putNextEntry(entry);
 //   int count;
//    while ((count = origin.read(data, 0, BUFFER)) != -1) {
//      out.write(data, 0, count);
//    }
  //  origin.close();
    out.write(contents.getBytes(),0,contents.length());
    out.setLevel(Deflater.BEST_COMPRESSION);
  }
 
Example #6
Source File: BlobBackupHelper.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private long checksum(byte[] buffer) {
    if (buffer != null) {
        try {
            CRC32 crc = new CRC32();
            ByteArrayInputStream bis = new ByteArrayInputStream(buffer);
            byte[] buf = new byte[4096];
            int nRead = 0;
            while ((nRead = bis.read(buf)) >= 0) {
                crc.update(buf, 0, nRead);
            }
            return crc.getValue();
        } catch (Exception e) {
            // whoops; fall through with an explicitly bogus checksum
        }
    }
    return -1;
}
 
Example #7
Source File: CreateKey.java    From JavaBase with MIT License 6 votes vote down vote up
private short getCRC(String s, int i, byte bytes[]) {
  CRC32 crc32 = new CRC32();
  if (s != null) {
    for (int j = 0; j < s.length(); j++) {
      char c = s.charAt(j);
      crc32.update(c);
    }
  }
  crc32.update(i);
  crc32.update(i >> 8);
  crc32.update(i >> 16);
  crc32.update(i >> 24);
  for (int k = 0; k < bytes.length - 2; k++) {
    byte byte0 = bytes[k];
    crc32.update(byte0);
  }
  return (short) (int) crc32.getValue();
}
 
Example #8
Source File: DownloadController.java    From airsonic with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Computes the CRC checksum for the given file.
 *
 * @param file The file to compute checksum for.
 * @return A CRC32 checksum.
 * @throws IOException If an I/O error occurs.
 */
private long computeCrc(File file) throws IOException {
    CRC32 crc = new CRC32();

    try (InputStream in = new FileInputStream(file)) {

        byte[] buf = new byte[8192];
        int n = in.read(buf);
        while (n != -1) {
            crc.update(buf, 0, n);
            n = in.read(buf);
        }

    }

    return crc.getValue();
}
 
Example #9
Source File: ZipUtils.java    From litchi with Apache License 2.0 6 votes vote down vote up
public static boolean compress(String srcFilePath, String destFilePath) {
	File src = new File(srcFilePath);
	if (!src.exists()) {
		throw new RuntimeException(srcFilePath + "not exist");
	}
	File zipFile = new File(destFilePath);
	try {
		FileOutputStream fos = new FileOutputStream(zipFile);
		CheckedOutputStream cos = new CheckedOutputStream(fos, new CRC32());
		ZipOutputStream zos = new ZipOutputStream(cos);
		String baseDir = "";
		compressbyType(src, zos, baseDir);
		zos.close();
		return true;
	} catch (Exception e) {
		LOGGER.error("", e);
	}
	return false;
}
 
Example #10
Source File: B7050028.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    URLConnection conn = B7050028.class.getResource("B7050028.class").openConnection();
    int len = conn.getContentLength();
    byte[] data = new byte[len];
    InputStream is = conn.getInputStream();
    is.read(data);
    is.close();
    conn.setDefaultUseCaches(false);
    File jar = File.createTempFile("B7050028", ".jar");
    jar.deleteOnExit();
    OutputStream os = new FileOutputStream(jar);
    ZipOutputStream zos = new ZipOutputStream(os);
    ZipEntry ze = new ZipEntry("B7050028.class");
    ze.setMethod(ZipEntry.STORED);
    ze.setSize(len);
    CRC32 crc = new CRC32();
    crc.update(data);
    ze.setCrc(crc.getValue());
    zos.putNextEntry(ze);
    zos.write(data, 0, len);
    zos.closeEntry();
    zos.finish();
    zos.close();
    os.close();
    System.out.println(new URLClassLoader(new URL[] {new URL("jar:" + jar.toURI() + "!/")}, ClassLoader.getSystemClassLoader().getParent()).loadClass(B7050028.class.getName()));
}
 
Example #11
Source File: SettingsBackupAgent.java    From Study_Android_Demo with Apache License 2.0 6 votes vote down vote up
private long writeIfChanged(long oldChecksum, String key, byte[] data,
        BackupDataOutput output) {
    CRC32 checkSummer = new CRC32();
    checkSummer.update(data);
    long newChecksum = checkSummer.getValue();
    if (oldChecksum == newChecksum) {
        return oldChecksum;
    }
    try {
        if (DEBUG_BACKUP) {
            Log.v(TAG, "Writing entity " + key + " of size " + data.length);
        }
        output.writeEntityHeader(key, data.length);
        output.writeEntityData(data, data.length);
    } catch (IOException ioe) {
        // Bail
    }
    return newChecksum;
}
 
Example #12
Source File: ExternalFile.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * @return validator, that checks the CRC32 value and all message digest
 *         values, that can be verified by the runtime JRE. Unsupported
 *         message digest values will be ignored
 */
public MessageMultiValidator getValidator() {
    List<MessageValidator> validators = new ArrayList<>(2);
    validators.add(new MessageChecksumValidator(new CRC32(), getCrc32()));
    for(Entry<String,byte[]> entry: getMessageDigests().entrySet()) {
        try {
            validators.add(new MessageDigestValidator(
                MessageDigest.getInstance(entry.getKey()), entry.getValue()));
        } catch (NoSuchAlgorithmException ex) {
            LOG.log(Level.INFO,
                "Requested message digest {0} not found for {1}",
                new Object[] {entry.getKey(), getName()});
        }
    }
    return new MessageMultiValidator(validators);
}
 
Example #13
Source File: GunzipOutputStream.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
private void verify(CRC32 crc) throws IOException
{
    try (DataInputStream in = new DataInputStream(new ByteArrayInputStream(_trailerBytes)))
    {
        long crc32 = readLittleEndianLong(in);
        if (crc32 != crc.getValue())
        {
            throw new IOException("crc32 mismatch. Gzip-compressed data is corrupted");
        }
        long isize = readLittleEndianLong(in);
        if (isize != (inf.getBytesWritten() & SIZE_MASK))
        {
            throw new IOException("Uncompressed size mismatch. Gzip-compressed data is corrupted");
        }
    }
}
 
Example #14
Source File: FileUtils.java    From trellis with Apache License 2.0 6 votes vote down vote up
/**
 * Get a directory for a given resource identifier.
 * @param baseDirectory the base directory
 * @param identifier a resource identifier
 * @return a directory
 */
public static File getResourceDirectory(final File baseDirectory, final IRI identifier) {
    requireNonNull(baseDirectory, "The baseDirectory may not be null!");
    requireNonNull(identifier, "The identifier may not be null!");
    final String id = identifier.getIRIString();
    final StringJoiner joiner = new StringJoiner(separator);
    final CRC32 hasher = new CRC32();
    hasher.update(id.getBytes(UTF_8));
    final String intermediate = Long.toHexString(hasher.getValue());

    range(0, intermediate.length() / LENGTH).limit(MAX)
        .forEach(i -> joiner.add(intermediate.substring(i * LENGTH, (i + 1) * LENGTH)));

    joiner.add(new DigestUtils(ALGORITHM).digestAsHex(id));
    return new File(baseDirectory, joiner.toString());
}
 
Example #15
Source File: ZipFileSystem.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
EntryOutputStream(Entry e, OutputStream os)
    throws IOException
{
    super(os, getDeflater());
    if (e == null)
        throw new NullPointerException("Zip entry is null");
    this.e = e;
    crc = new CRC32();
}
 
Example #16
Source File: BigJar.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
long computeCRC(long minlength) {
    CRC32 crc = new CRC32();
    byte[] buffer = new byte[BUFFER_LEN];
    long count = getCount(minlength);
    for (long i = 0; i < count; i++) {
        crc.update(buffer);
    }
    return crc.getValue();
}
 
Example #17
Source File: HashUtil.java    From vjtools with Apache License 2.0 5 votes vote down vote up
/**
 * 对输入字符串进行crc32散列返回int, 返回值有可能是负数.
 * 
 * Guava也有crc32实现, 但返回值无法返回long,所以统一使用JDK默认实现
 */
public static int crc32AsInt(@NotNull byte[] input) {
	CRC32 crc32 = new CRC32();
	crc32.update(input);
	// CRC32 只是 32bit int,为了CheckSum接口强转成long,此处再次转回来
	return (int) crc32.getValue();
}
 
Example #18
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 #19
Source File: CrcTest.java    From oneops with Apache License 2.0 5 votes vote down vote up
/**
 * @param args
 */
public static void main(String[] args) {
	CRC32 crc = new CRC32();
	
	String s = "as;ldkasldkgj;alskdgj;alsdkgja;sldgkjq";
	crc.update(s.getBytes());
	System.out.println("crc: " + crc.getValue());

}
 
Example #20
Source File: UserProfileBean.java    From openzaly with Apache License 2.0 5 votes vote down vote up
public String getGlobalUserId() {
	if (StringUtils.isEmpty(this.globalUserId)) {
		String body = this.userIdPubk;
		String SHA1UserPubKey = new String(Hex.encodeHex(DigestUtils.sha1(body)));

		CRC32 c32 = new CRC32();
		c32.update(body.getBytes(), 0, body.getBytes().length);
		String CRC32UserPubKey = String.valueOf(c32.getValue());

		return SHA1UserPubKey + "-" + CRC32UserPubKey;
	}
	return this.globalUserId;
}
 
Example #21
Source File: NukkitRandom.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void setSeed(long seeds) {
    CRC32 crc32 = new CRC32();
    ByteBuffer buffer = ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN);
    buffer.putInt((int) seeds);
    crc32.update(buffer.array());
    this.seed = crc32.getValue();
}
 
Example #22
Source File: UnsExt8.java    From HaloDB with Apache License 2.0 5 votes vote down vote up
long crc32(long address, long offset, long len) {
    CRC32 crc = new CRC32();
    crc.update(Uns.directBufferFor(address, offset, len, true));
    long h = crc.getValue();
    h |= h << 32;
    return h;
}
 
Example #23
Source File: ShortWrite.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a checksum on the remaining bytes in the given buffers.
 */
static long computeChecksum(ByteBuffer... bufs) {
    CRC32 crc32 = new CRC32();
    for (int i=0; i<bufs.length; i++)
        crc32.update(bufs[i]);
    return crc32.getValue();
}
 
Example #24
Source File: JoH.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static boolean checkChecksum(byte[] bytes) {
    if ((bytes == null) || (bytes.length < 4)) return false;
    final CRC32 crc = new CRC32();
    crc.update(bytes, 0, bytes.length - 4);
    final long buffer_crc = UnsignedInts.toLong(ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getInt(bytes.length - 4));
    return buffer_crc == crc.getValue();
}
 
Example #25
Source File: PngResource.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private boolean verifyCRC(byte[] type, byte[] data, long crc) {
	CRC32 crc32 = new CRC32();
	crc32.update(type);
	crc32.update(data);
	long crcVal = crc32.getValue();
	return (crcVal == crc);
}
 
Example #26
Source File: QiniuAccessor.java    From java-unified-sdk with Apache License 2.0 5 votes vote down vote up
private void validateCrc32Value(QiniuBlockResponseData respData, byte[] data, int offset, int nextChunkSize)
        throws AVException {
  CRC32 crc32 = new CRC32();
  crc32.update(data,offset,nextChunkSize);
  long localCRC32 = crc32.getValue();
  if(respData!=null && respData.crc32 != localCRC32){
    throw  new AVException(AVException.OTHER_CAUSE,"CRC32 validation failure for chunk upload");
  }
}
 
Example #27
Source File: Record.java    From HaloDB with Apache License 2.0 5 votes vote down vote up
private long computeCheckSum(byte[] header) {
    CRC32 crc32 = new CRC32();

    // compute checksum with all but the first header element, key and value.
    crc32.update(header, Header.CHECKSUM_OFFSET + Header.CHECKSUM_SIZE, Header.HEADER_SIZE-Header.CHECKSUM_SIZE);
    crc32.update(key);
    crc32.update(value);
    return crc32.getValue();
}
 
Example #28
Source File: T6836682.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
static long computeCRC(long minlength) {
    CRC32 crc = new CRC32();
    byte[] buffer = new byte[BUFFER_LEN];
    long count = getCount(minlength);
    for (long i = 0; i < count; i++) {
        crc.update(buffer);
    }
    return crc.getValue();
}
 
Example #29
Source File: CrcUtilIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private long calculateFileCrc(File file) throws IOException {
  try(InputStream inputStream = new FileInputStream(file)) {
    byte[] buffer = new byte[InternalZipConstants.BUFF_SIZE];
    int readLen = -1;
    CRC32 crc32 = new CRC32();
    while((readLen = inputStream.read(buffer)) != -1) {
      crc32.update(buffer, 0, readLen);
    }
    return crc32.getValue();
  }
}
 
Example #30
Source File: CrcUtil.java    From zip4j with Apache License 2.0 5 votes vote down vote up
public static long computeFileCrc(File inputFile, ProgressMonitor progressMonitor) throws IOException {

    if (inputFile == null || !inputFile.exists() || !inputFile.canRead()) {
      throw new ZipException("input file is null or does not exist or cannot read. " +
          "Cannot calculate CRC for the file");
    }

    byte[] buff = new byte[BUF_SIZE];
    CRC32 crc32 = new CRC32();

    try(InputStream inputStream = new FileInputStream(inputFile)) {
      int readLen;
      while ((readLen = inputStream.read(buff)) != -1) {
        crc32.update(buff, 0, readLen);

        if (progressMonitor != null) {
          progressMonitor.updateWorkCompleted(readLen);
          if (progressMonitor.isCancelAllTasks()) {
            progressMonitor.setResult(ProgressMonitor.Result.CANCELLED);
            progressMonitor.setState(ProgressMonitor.State.READY);
            return 0;
          }
        }
      }
      return crc32.getValue();
    }
  }