类java.util.zip.CRC32源码实例Demo

下面列出了怎么用java.util.zip.CRC32的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: netbeans   文件: ExternalFile.java
/**
 * @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);
}
 
源代码2 项目: gemfirexd-oss   文件: BlobTest.java
/**
 * 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();
}
 
源代码3 项目: JavaBase   文件: CreateKey.java
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();
}
 
源代码4 项目: org.hl7.fhir.core   文件: ZipGenerator.java
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);
  }
 
源代码5 项目: mnemonic   文件: MneMapreduceChunkDataTest.java
@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();
}
 
源代码6 项目: android_9.0.0_r45   文件: BlobBackupHelper.java
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;
}
 
源代码7 项目: litchi   文件: ZipUtils.java
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;
}
 
源代码8 项目: qpid-broker-j   文件: GunzipOutputStream.java
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");
        }
    }
}
 
源代码9 项目: trellis   文件: FileUtils.java
/**
 * 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());
}
 
源代码10 项目: airsonic   文件: DownloadController.java
/**
 * 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();
}
 
源代码11 项目: reladomo   文件: JarVersionCreator.java
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"));
    }
}
 
源代码12 项目: shrinker   文件: JarProcessor.java
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;
}
 
源代码13 项目: jdk8u60   文件: B7050028.java
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()));
}
 
源代码14 项目: Study_Android_Demo   文件: SettingsBackupAgent.java
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;
}
 
源代码15 项目: wind-im   文件: UserProfileBean.java
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;
}
 
源代码16 项目: wind-im   文件: UserIdUtils.java
public static String getV1GlobalUserId(String userIdPubk) {
	String body = userIdPubk;
	String SHA1UserPubKey = HashCrypto.SHA1(body);
	CRC32 c32 = new CRC32();
	c32.update(body.getBytes(), 0, body.getBytes().length);
	String CRC32UserPubKey = String.valueOf(c32.getValue());
	return SHA1UserPubKey + "-" + CRC32UserPubKey;
}
 
@Test
public void testCrc32Checksum() throws IOException {
    CRC32 crc32 = new CRC32();
    crc32.update(TEST_DATA.getBytes());
    long expectedCRC32Checksum = crc32.getValue();
    Crc32ChecksumCalculatingInputStream crc32InputStream =
            new Crc32ChecksumCalculatingInputStream(new ByteArrayInputStream(TEST_DATA.getBytes()));
    while (crc32InputStream.read() != -1) {
        ;
    }
    assertEquals(expectedCRC32Checksum, crc32InputStream.getCrc32Checksum());
}
 
源代码18 项目: openjdk-8   文件: ShortWrite.java
/**
 * 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();
}
 
源代码19 项目: ratel   文件: BrutIO.java
public static CRC32 calculateCrc(InputStream input) throws IOException {
    CRC32 crc = new CRC32();
    int bytesRead;
    byte[] buffer = new byte[8192];
    while((bytesRead = input.read(buffer)) != -1) {
        crc.update(buffer, 0, bytesRead);
    }
    return crc;
}
 
/**
 * Encodes string.
 * @param txt string to encode
 * @return encoded string or <code>null</code> if error encoding string
 */
public static String encode(String txt) {
  txt = StringUtils.defaultIfEmpty(txt, "");
  try {
    CRC32 crC32 = new CRC32();
    crC32.update(txt.getBytes("UTF-8"));
    long crc = crC32.getValue();
    String crctxt = String.format("%10d%s", crc, txt);
    Base64.Encoder encoder = Base64.getEncoder();
    return encoder.encodeToString(crctxt.getBytes("UTF-8"));
  } catch (UnsupportedEncodingException ex) {
    return null;
  }
}
 
源代码21 项目: distributedlog   文件: ProtocolUtils.java
/**
 * Generate crc32 for WriteOp.
 */
public static Long writeOpCRC32(String stream, byte[] payload) {
    CRC32 crc = requestCRC.get();
    try {
        crc.update(stream.getBytes(UTF_8));
        crc.update(payload);
        return crc.getValue();
    } finally {
        crc.reset();
    }
}
 
源代码22 项目: netcdf-java   文件: Grib1SectionGridDefinition.java
/**
 * Calculate the CRC of the entire byte array
 *
 * @return CRC of the entire byte array
 */
public long calcCRC() {
  long crc;
  if (rawData == null)
    crc = predefinedGridDefinitionCenter << 16 + predefinedGridDefinition;
  else {
    CRC32 crc32 = new CRC32();
    crc32.update(rawData);
    crc = crc32.getValue();
  }
  return crc;
}
 
源代码23 项目: openjdk-jdk9   文件: BigJar.java
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();
}
 
源代码24 项目: tac   文件: TacCompressUtils.java
/**
 * compress
 *
 * @param srcFile
 * @param destFile
 * @throws Exception
 */
public static void compress(File srcFile, File destFile) throws Exception {

    // CRC32 check
    CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream(
        destFile), new CRC32());

    ZipOutputStream zos = new ZipOutputStream(cos);
    zos.setComment(new String("comment"));
    compress(srcFile, zos, BASE_DIR);

    zos.flush();
    zos.close();
}
 
源代码25 项目: distributedlog   文件: ProtocolUtils.java
/**
 * Generate crc32 for WriteOp.
 */
public static Long writeOpCRC32(String stream, ByteBuf data) {
    CRC32 crc = requestCRC.get();
    try {
        crc.update(stream.getBytes(UTF_8));
        crc.update(data.nioBuffer());
        return crc.getValue();
    } finally {
        crc.reset();
    }
}
 
源代码26 项目: Trebuchet   文件: LauncherBackupHelper.java
/** Compute the checksum over the important bits of a key. */
private long checkKey(Key key) {
    CRC32 checksum = new CRC32();
    checksum.update(key.type);
    checksum.update((int) (key.id & 0xffff));
    checksum.update((int) ((key.id >> 32) & 0xffff));
    if (!TextUtils.isEmpty(key.name)) {
        checksum.update(key.name.getBytes());
    }
    return checksum.getValue();
}
 
源代码27 项目: Trebuchet   文件: LauncherBackupHelper.java
/** Wrap a proto in a CheckedMessage and compute the checksum. */
private byte[] writeCheckedBytes(MessageNano proto) {
    CheckedMessage wrapper = new CheckedMessage();
    wrapper.payload = MessageNano.toByteArray(proto);
    CRC32 checksum = new CRC32();
    checksum.update(wrapper.payload);
    wrapper.checksum = checksum.getValue();
    return MessageNano.toByteArray(wrapper);
}
 
源代码28 项目: swblocks-decisiontree   文件: FilePersister.java
private void writeZippedFile(final DecisionTreeRuleSet ruleSet) {
    EhSupport.ensureArg(ruleSet != null, "Cannot persist a null ruleset");
    EhSupport.propagate(() -> {
        try (
                final OutputStream stream = new BufferedOutputStream(
                        Files.newOutputStream(Paths.get(this.fileName), StandardOpenOption.CREATE));
                final CheckedOutputStream checksum = new CheckedOutputStream(stream, new CRC32());
                final ZipOutputStream zipOutputStream = new ZipOutputStream(checksum)) {
            final ZipEntry entry = new ZipEntry(ruleSet.getName() + ".json");
            zipOutputStream.putNextEntry(entry);
            this.serialiser.serialiseRuleSet(zipOutputStream, ruleSet);
        }
    });
}
 
源代码29 项目: waltz   文件: Partition.java
int checksum() throws IOException {
    CRC32 crc32 = new CRC32();

    for (Segment segment : segments) {
        segment.checksum(crc32);
    }

    return (int) crc32.getValue();
}
 
源代码30 项目: big-c   文件: TestIndexCache.java
private static void writeFile(FileSystem fs, Path f, long fill, int parts)
    throws IOException {
  FSDataOutputStream out = fs.create(f, false);
  CheckedOutputStream iout = new CheckedOutputStream(out, new CRC32());
  DataOutputStream dout = new DataOutputStream(iout);
  for (int i = 0; i < parts; ++i) {
    for (int j = 0; j < MapTask.MAP_OUTPUT_INDEX_RECORD_LENGTH / 8; ++j) {
      dout.writeLong(fill);
    }
  }
  out.writeLong(iout.getChecksum().getValue());
  dout.close();
}
 
 类所在包
 同包方法