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

The following examples show how to use java.util.zip.CRC32#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: NonIncrementalJarDexArchiveHook.java    From atlas with Apache License 2.0 6 votes vote down vote up
@Override
public void addFile(@NonNull String relativePath, byte[] bytes, int offset, int end)
        throws IOException {
    Preconditions.checkNotNull(jarOutputStream, "Archive is not writeable : %s", targetPath);
    // Need to pre-compute checksum for STORED (uncompressed) entries)
    CRC32 checksum = new CRC32();
    checksum.update(bytes, offset, end);

    ZipEntry zipEntry = new ZipEntry(relativePath);
    zipEntry.setLastModifiedTime(ZERO_TIME);
    zipEntry.setLastAccessTime(ZERO_TIME);
    zipEntry.setCreationTime(ZERO_TIME);
    zipEntry.setCrc(checksum.getValue());
    zipEntry.setSize(end - offset);
    zipEntry.setCompressedSize(end - offset);
    zipEntry.setMethod(ZipEntry.STORED);

    jarOutputStream.putNextEntry(zipEntry);
    jarOutputStream.write(bytes, offset, end);
    jarOutputStream.flush();
    jarOutputStream.closeEntry();
}
 
Example 2
Source File: UpdateTracking.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static long getFileCRC(File file) throws IOException {
    BufferedInputStream bsrc = null;
    CRC32 crc = new CRC32();
    try {
        bsrc = new BufferedInputStream( new FileInputStream( file ) );
        byte[] bytes = new byte[1024];
        int i;
        while( (i = bsrc.read(bytes)) != -1 ) {
            crc.update(bytes, 0, i );
        }
    }
    finally {
        if ( bsrc != null ) {
            bsrc.close();
        }
    }
    return crc.getValue();
}
 
Example 3
Source File: B7050028.java    From openjdk-8-source 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 4
Source File: ShortWrite.java    From openjdk-jdk8u-backup 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 5
Source File: Util.java    From rscplus with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Gets the CRC32 of a given file name.
 *
 * @param fname Path to the file
 * @return CRC32 of the file data
 */
public static long fileGetCRC32(String fname) {
  try {
    byte[] data = Files.readAllBytes(new File(fname).toPath());
    CRC32 crc = new CRC32();
    crc.update(data);
    return crc.getValue();
  } catch (Exception e) {
  }

  return -1;
}
 
Example 6
Source File: FileCRC32Calculator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public @Override void execute() throws BuildException {

        try {
            CRC32 crc32 = new CRC32();
            try(FileChannel channel = FileChannel.open(file.toPath(), StandardOpenOption.READ)) {
                MappedByteBuffer mbb = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
                crc32.update(mbb);
            }
            getProject().setProperty(property, Long.toString(crc32.getValue()));
        } catch (IOException ex) {
            throw new BuildException(ex);
        }
    }
 
Example 7
Source File: UserProfileBean.java    From wind-im 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 8
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 9
Source File: TestCRC32l.java    From util4j with Apache License 2.0 5 votes vote down vote up
public static String test(byte[] data)
{
	long time=System.currentTimeMillis();
       CRC32 crc32 = new CRC32();  
       crc32.update(data);
       long value=crc32.getValue();
       time=System.currentTimeMillis()-time;
       String hexValue=Long.toHexString(crc32.getValue()).toUpperCase();
       System.out.println(time+","+value+":"+hexValue);
       return hexValue;
}
 
Example 10
Source File: BigJar.java    From hottub 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 11
Source File: ShortWrite.java    From jdk8u_jdk 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 12
Source File: NativeCrc32Checksum.java    From joyrpc with Apache License 2.0 4 votes vote down vote up
@Override
public long compute(final ByteBuffer buffer) {
    CRC32 crc32 = new CRC32();
    crc32.update(buffer);
    return crc32.getValue();
}
 
Example 13
Source File: CRCTest.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private static void updateDirect(CRC32 crc3, byte[] b, int start, int length) {
    ByteBuffer buf = ByteBuffer.allocateDirect(length);
    buf.put(b, start, length);
    buf.flip();
    crc3.update(buf);
}
 
Example 14
Source File: ResponseFactory.java    From o2oa with GNU Affero General Public License v3.0 4 votes vote down vote up
private static String etagWoFile(WoFile wo) {
	CRC32 crc = new CRC32();
	crc.update(wo.getBytes());
	return crc.getValue() + "";
}
 
Example 15
Source File: Grib2Pds.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public long getIntervalHash() {
  CRC32 crc32 = new CRC32();
  crc32.update(input, 56, getNumberTimeRanges() * 12);
  return crc32.getValue();
}
 
Example 16
Source File: CRCTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        byte[] b = initializedBytes(4096 * 4096);

        {
            CRC32 crc1 = new CRC32();
            CRC32 crc2 = new CRC32();
            CRC32 crc3 = new CRC32();
            CRC32 crc4 = new CRC32();

            crc1.update(b, 0, b.length);
            updateSerial(crc2, b, 0, b.length);
            updateDirect(crc3, b, 0, b.length);
            updateSerialSlow(crc4, b, 0, b.length);

            check(crc1, crc2);
            check(crc3, crc4);
            check(crc1, crc3);

            crc1.update(17);
            crc2.update(17);
            crc3.update(17);
            crc4.update(17);

            crc1.update(b, 1, b.length-2);
            updateSerial(crc2, b, 1, b.length-2);
            updateDirect(crc3, b, 1, b.length-2);
            updateSerialSlow(crc4, b, 1, b.length-2);

            check(crc1, crc2);
            check(crc3, crc4);
            check(crc1, crc3);

            report("finished huge crc", crc1, crc2, crc3, crc4);

            for (int i = 0; i < 256; i++) {
                for (int j = 0; j < 256; j += 1) {
                    crc1.update(b, i, j);
                    updateSerial(crc2, b, i, j);
                    updateDirect(crc3, b, i, j);
                    updateSerialSlow(crc4, b, i, j);

                    check(crc1, crc2);
                    check(crc3, crc4);
                    check(crc1, crc3);

                }
            }

            report("finished small survey crc", crc1, crc2, crc3, crc4);
        }

    }
 
Example 17
Source File: CRCModShardLocator.java    From das with Apache License 2.0 4 votes vote down vote up
@Override
protected Number string2Long(String str) {
    CRC32 crc32 = new CRC32();
    crc32.update(str.getBytes());
    return crc32.getValue();
}
 
Example 18
Source File: LargeJarEntry.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        String srcDir = System.getProperty("test.src", ".");
        String keystore = srcDir + "/JarSigning.keystore";
        String jarName = "largeJarEntry.jar";

        // Set java.io.tmpdir to the current working dir (see 6474350)
        System.setProperty("java.io.tmpdir", System.getProperty("user.dir"));

        // first, create jar file with 8M uncompressed entry
        // note, we set the max heap size to 8M in @run tag above
        byte[] bytes = new byte[1000000];
        CRC32 crc = new CRC32();
        for (int i=0; i<8; i++) {
            crc.update(bytes);
        }
        JarEntry je = new JarEntry("large");
        je.setSize(8000000l);
        je.setMethod(JarEntry.STORED);
        je.setCrc(crc.getValue());
        File file = new File(jarName);
        FileOutputStream os = new FileOutputStream(file);
        JarOutputStream jos = new JarOutputStream(os);
        jos.setMethod(JarEntry.STORED);
        jos.putNextEntry(je);
        for (int i=0; i<8; i++) {
            jos.write(bytes, 0, bytes.length);
        }
        jos.close();

        String[] jsArgs = { "-keystore", keystore, "-storepass", "bbbbbb",
                jarName, "b" };
        // now, try to sign it
        try {
            sun.security.tools.jarsigner.Main.main(jsArgs);
        } catch (OutOfMemoryError err) {
            throw new Exception("Test failed with OutOfMemoryError", err);
        } finally {
            // remove jar file
            file.delete();
        }
    }
 
Example 19
Source File: ZipBuilder.java    From bundletool with Apache License 2.0 4 votes vote down vote up
private static long computeCrc32(byte[] data) {
  CRC32 crc32 = new CRC32();
  crc32.update(data);
  return crc32.getValue();
}
 
Example 20
Source File: Cache.java    From OpenRS with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Writes a file to the cache and updates the {@link ReferenceTable} that it
 * is associated with.
 * 
 * @param type
 *            The type of file.
 * @param file
 *            The file id.
 * @param container
 *            The {@link Container} to write.
 * @param keys
 *            The encryption keys.
 * @throws IOException
 *             if an I/O error occurs.
 */
public void write(int type, int file, Container container, int[] keys) throws IOException {
	/* we don't want people reading/manipulating these manually */
	if (type == 255)
		throw new IOException("Reference tables can only be modified with the low level FileStore API!");

	/* increment the container's version */
	container.setVersion(container.getVersion()/* + 1 */);

	/* decode the reference table for this index */
	Container tableContainer = Container.decode(store.read(255, type));
	ReferenceTable table = ReferenceTable.decode(tableContainer.getData());

	/* grab the bytes we need for the checksum */
	ByteBuffer buffer = container.encode(keys);
	
	/* last two bytes are the version and shouldn't be included */
	byte[] bytes = new byte[buffer.limit() - 2];
	buffer.mark();
	try {
		buffer.position(0);
		buffer.get(bytes, 0, bytes.length);
	} finally {
		buffer.reset();
	}

	/* calculate the new CRC checksum */
	CRC32 crc = new CRC32();
	crc.update(bytes, 0, bytes.length);

	/* update the version and checksum for this file */
	ReferenceTable.Entry entry = table.getEntry(file);
	if (entry == null) {
		/* create a new entry for the file */
		entry = new ReferenceTable.Entry(file);
		table.putEntry(file, entry);
	}
	entry.setVersion(container.getVersion());
	entry.setCrc((int) crc.getValue());

	/* calculate and update the whirlpool digest if we need to */
	if ((table.getFlags() & ReferenceTable.FLAG_WHIRLPOOL) != 0) {
		byte[] whirlpool = Whirlpool.whirlpool(bytes, 0, bytes.length);
		entry.setWhirlpool(whirlpool);
	}

	/* update the reference table version */
	table.setVersion(table.getVersion()/* + 1 */);

	/* save the reference table */
	tableContainer = new Container(tableContainer.getType(), table.encode());
	store.write(255, type, tableContainer.encode());

	/* save the file itself */
	store.write(type, file, buffer);
}