下面列出了java.util.zip.CRC32#update ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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()));
}
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();
}
@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();
}
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;
}
/**
* 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();
}
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();
}
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;
}
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();
}
/**
* 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();
}
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);
}
}
/**
* 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;
}
/**
* 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);
}
public long getIntervalHash() {
CRC32 crc32 = new CRC32();
crc32.update(input, 56, getNumberTimeRanges() * 12);
return crc32.getValue();
}
private static String etagWoFile(WoFile wo) {
CRC32 crc = new CRC32();
crc.update(wo.getBytes());
return crc.getValue() + "";
}
@Override
public long compute(final ByteBuffer buffer) {
CRC32 crc32 = new CRC32();
crc32.update(buffer);
return crc32.getValue();
}
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);
}
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);
}
}
@Override
protected Number string2Long(String str) {
CRC32 crc32 = new CRC32();
crc32.update(str.getBytes());
return crc32.getValue();
}
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();
}
}
private static long computeCrc32(byte[] data) {
CRC32 crc32 = new CRC32();
crc32.update(data);
return crc32.getValue();
}