public static byte[] calCrc32(byte[] data) { Checksum checksum = new CRC32(); // update the current checksum with the specified array of bytes checksum.update(data, 0, data.length); // get the current checksum value long checksumValue = checksum.getValue(); String hex = Long.toHexString(checksumValue).toUpperCase(); while (hex.length() < 8) { hex = "0" + hex; } byte[] crc32 = Hex.decode(hex); inverseBytes(crc32); reverseArray(crc32); return crc32; }
private static long getApkFileChecksum(Context context) { String apkPath = context.getPackageCodePath(); Long chksum = null; try { // Open the file and build a CRC32 checksum. FileInputStream fis = new FileInputStream(new File(apkPath)); CRC32 chk = new CRC32(); CheckedInputStream cis = new CheckedInputStream(fis, chk); byte[] buff = new byte[80]; while (cis.read(buff) >= 0) ; chksum = chk.getValue(); } catch (Exception e) { e.printStackTrace(); } return chksum; }
private static void validateFileWithChecksum(FileSystem fs, Path filePath, BackupFileInfo backupFileInfo) throws IOException { final CheckedInputStream cin = new CheckedInputStream(fs.open(filePath), new CRC32()); final BufferedReader reader = new BufferedReader(new InputStreamReader(cin)); final ObjectMapper objectMapper = new ObjectMapper(); String line; long records = 0; // parse records just to make sure formatting is correct while ((line = reader.readLine()) != null) { objectMapper.readValue(line, BackupRecord.class); ++records; } cin.close(); long found = cin.getChecksum().getValue(); if (backupFileInfo.getChecksum() != found) { throw new IOException(format("Corrupt backup data file %s. Expected checksum %x, found %x", filePath, backupFileInfo.getChecksum(), found)); } if (backupFileInfo.getRecords() != records) { throw new IOException(format("Corrupt backup data file %s. Expected records %x, found %x", filePath, backupFileInfo.getRecords(), records)); } }
private static void writeJarEntry(JarOutputStream jos, String path, File f) throws IOException, FileNotFoundException { JarEntry je = new JarEntry(path); ByteArrayOutputStream baos = new ByteArrayOutputStream(); InputStream is = new FileInputStream(f); try { copyStreams(is, baos); } finally { is.close(); } byte[] data = baos.toByteArray(); je.setSize(data.length); CRC32 crc = new CRC32(); crc.update(data); je.setCrc(crc.getValue()); jos.putNextEntry(je); jos.write(data); }
public static DataChecksum newDataChecksum(Type type, int bytesPerChecksum ) { if ( bytesPerChecksum <= 0 ) { return null; } switch ( type ) { case NULL : return new DataChecksum(type, new ChecksumNull(), bytesPerChecksum ); case CRC32 : return new DataChecksum(type, newCrc32(), bytesPerChecksum ); case CRC32C: return new DataChecksum(type, new PureJavaCrc32C(), bytesPerChecksum); default: return null; } }
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(); }
/** * Generates a CRC 32 Value of String passed * * @param data * @return long crc32 value of input. -1 if string is null * @throws RuntimeException if UTF-8 is not a supported character set */ public static long crc32(String data) { if (data == null) { return -1; } try { // get bytes from string byte bytes[] = data.getBytes("UTF-8"); Checksum checksum = new CRC32(); // update the current checksum with the specified array of bytes checksum.update(bytes, 0, bytes.length); // get the current checksum value return checksum.getValue(); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } }
private long getCrc(ZipFile file, ZipEntry entry) throws IOException { long crc = -1; if (entry != null) { crc = entry.getCrc(); if (crc < 0) { CRC32 checksum = new CRC32(); final InputStream in = file.getInputStream(entry); try { final byte[] buffer = new byte[1024]; int count; while ((count = in.read(buffer)) >= 0) { checksum.update(buffer, 0, count); } in.close(); crc = checksum.getValue(); } finally { IOUtils.closeQuietly(in); } } } return crc; }
/** * extracts the checksum of the source, calculates a new one on the cut off source and compares them. * returns the cut off data only if the checksums matched * * @param source * @return */ protected byte[] validateAndStripChecksum(byte[] source) { // retrieve checksum int lastIndexOf = Bytes.lastIndexOf(source, (byte) SEPARATOR); if (lastIndexOf < 0) { throw new RuntimeException("no checksum was found on source: " + new String(source)); } long checkSum = Long.parseLong(new String(Arrays.copyOfRange(source, lastIndexOf + 1, source.length))); byte[] data = Arrays.copyOfRange(source, 0, lastIndexOf); CRC32 crc32 = new CRC32(); crc32.update(data); long calculatedCheckSum = crc32.getValue(); // validate checksum if (Long.compare(checkSum, calculatedCheckSum) != 0) { throw new RuntimeException("checksums do not match! calculated: " + calculatedCheckSum + ", provided: " + checkSum); } return data; }
/** * Find a CRC value for the full QName using UTF-8 conversion. * * @param qname the association qname * @return Returns the CRC value (UTF-8 compatible) */ public static Long getQNameCrc(QName qname) { CRC32 crc = new CRC32(); try { crc.update(qname.getNamespaceURI().getBytes("UTF-8")); crc.update(qname.getLocalName().getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { throw new RuntimeException("UTF-8 encoding is not supported"); } return crc.getValue(); }
public static long file(File f) throws IOException { FileInputStream fi = new FileInputStream(f); byte[] buff = new byte[65536]; CRC32 crc32 = new CRC32(); while (true) { try { int len = fi.read(buff); if (len == -1) { break; } crc32.update(buff, 0, len); } catch (Exception e) { e.printStackTrace(); } finally { fi.close(); } } return crc32.getValue(); }
@Test public void transformerInPlaceTest() throws Exception { assertTrue(new BytesTransformer.BitSwitchTransformer(0, true).supportInPlaceTransformation()); assertTrue(new BytesTransformer.BitWiseOperatorTransformer(new byte[]{}, BytesTransformer.BitWiseOperatorTransformer.Mode.XOR).supportInPlaceTransformation()); assertTrue(new BytesTransformer.NegateTransformer().supportInPlaceTransformation()); assertTrue(new BytesTransformer.ShiftTransformer(0, BytesTransformer.ShiftTransformer.Type.LEFT_SHIFT).supportInPlaceTransformation()); assertTrue(new BytesTransformer.ReverseTransformer().supportInPlaceTransformation()); assertFalse(new BytesTransformer.MessageDigestTransformer("SHA1").supportInPlaceTransformation()); assertFalse(new BytesTransformer.CopyTransformer(0, 0).supportInPlaceTransformation()); assertFalse(new BytesTransformer.ResizeTransformer(0, BytesTransformer.ResizeTransformer.Mode.RESIZE_KEEP_FROM_MAX_LENGTH).supportInPlaceTransformation()); assertFalse(new BytesTransformer.ConcatTransformer(new byte[]{}).supportInPlaceTransformation()); assertFalse(new BytesTransformers.GzipCompressor(false).supportInPlaceTransformation()); assertFalse(new BytesTransformers.ChecksumTransformer(new CRC32(), ChecksumTransformer.Mode.TRANSFORM, 4).supportInPlaceTransformation()); assertTrue(new BytesTransformers.SortTransformer().supportInPlaceTransformation()); assertFalse(new BytesTransformers.SortTransformer(new Comparator<Byte>() { @Override public int compare(Byte o1, Byte o2) { return 0; } }).supportInPlaceTransformation()); assertTrue(new BytesTransformers.ShuffleTransformer(new SecureRandom()).supportInPlaceTransformation()); }
/** * 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(); InputStream in = new FileInputStream(file); try { byte[] buf = new byte[8192]; int n = in.read(buf); while (n != -1) { crc.update(buf, 0, n); n = in.read(buf); } } finally { in.close(); } return crc.getValue(); }
private static void readBytesByBufferedCheckedInputStream(byte[] bytes){ InputStream is = null; int data = -1; try { is = new ByteArrayInputStream(bytes); // 先用BufferedInputStream这个装饰器对ByteArrayInputStream进行包装 is = new BufferedInputStream(is); // 再用CheckedInputStream这个装饰器对已经缓冲的流进行再次包装 CheckedInputStream cis = new CheckedInputStream(is, new CRC32()); while (-1 != (data = cis.read())){ System.out.print((char) data); } System.out.println(" crc32:" + cis.getChecksum().getValue()); System.out.println("-------------"); } catch (IOException e) { e.printStackTrace(); } }
/** * Compressed file or directory * * @param srcPath The address of the file or folder * @param zipFilePath The address of the compressed package */ public static void zipFiles(String srcPath,String zipFilePath) { File file = new File(srcPath); if (!file.exists()) throw new RuntimeException(srcPath + "not exist!"); try { FileOutputStream fileOutputStream = new FileOutputStream(zipFilePath); CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream, new CRC32()); ZipOutputStream out = new ZipOutputStream(cos); String baseDir=""; zip(file,out,baseDir); out.close(); } catch (Exception e) { throw new RuntimeException(e); } }
private static long _getCRC(int type, byte[] data) { CRC32 crc = new CRC32(); // First add in the type bytes crc.update((type >> 24) & 0x000000ff); crc.update((type >> 16) & 0x000000ff); crc.update((type >> 8) & 0x000000ff); crc.update(type & 0x000000ff); // Now, add in the data if (data != null) crc.update(data); return crc.getValue(); }
static long computeCrcOfCentralDir(RandomAccessFile raf, CentralDirectory dir) throws IOException { CRC32 crc = new CRC32(); long stillToRead = dir.size; raf.seek(dir.offset); byte[] buffer = new byte[16384]; int length = raf.read(buffer, 0, (int) Math.min(16384, stillToRead)); while (length != -1) { crc.update(buffer, 0, length); stillToRead -= (long) length; if (stillToRead == 0) { break; } length = raf.read(buffer, 0, (int) Math.min(16384, stillToRead)); } return crc.getValue(); }
/** * Check if the CRC of the snapshot file matches the digest. * @param f The snapshot file object * @return The table list as a string * @throws IOException If CRC does not match */ public static String CRCCheck(File f) throws IOException { final FileInputStream fis = new FileInputStream(f); try { final BufferedInputStream bis = new BufferedInputStream(fis); ByteBuffer crcBuffer = ByteBuffer.allocate(4); if (4 != bis.read(crcBuffer.array())) { throw new EOFException("EOF while attempting to read CRC from snapshot digest"); } final int crc = crcBuffer.getInt(); final InputStreamReader isr = new InputStreamReader(bis, "UTF-8"); CharArrayWriter caw = new CharArrayWriter(); while (true) { int nextChar = isr.read(); if (nextChar == -1) { throw new EOFException("EOF while reading snapshot digest"); } if (nextChar == '\n') { break; } caw.write(nextChar); } String tableList = caw.toString(); byte tableListBytes[] = tableList.getBytes("UTF-8"); CRC32 tableListCRC = new CRC32(); tableListCRC.update(tableListBytes); tableListCRC.update("\n".getBytes("UTF-8")); final int calculatedValue = (int)tableListCRC.getValue(); if (crc != calculatedValue) { throw new IOException("CRC of snapshot digest did not match digest contents"); } return tableList; } finally { try { if (fis != null) fis.close(); } catch (IOException e) {} } }
public PNGDecoder(InputStream input) throws IOException { this.input = input; this.crc = new CRC32(); this.buffer = new byte[4096]; readFully(buffer, 0, SIGNATURE.length); if(!checkSignature(buffer)) { throw new IOException("Not a valid PNG file"); } openChunk(IHDR); readIHDR(); closeChunk(); searchIDAT: for(;;) { openChunk(); switch (chunkType) { case IDAT: break searchIDAT; case PLTE: readPLTE(); break; case tRNS: readtRNS(); break; } closeChunk(); } if(colorType == COLOR_INDEXED && palette == null) { throw new IOException("Missing PLTE chunk"); } }
/** * Updates checksum with the provided ByteBuffer at the given offset + length. * Resets position and limit back to their original values on return. * This method is *NOT* thread-safe. */ public static void updateChecksum(CRC32 checksum, ByteBuffer buffer, int offset, int length) { int position = buffer.position(); int limit = buffer.limit(); buffer.position(offset).limit(offset + length); checksum.update(buffer); buffer.position(position).limit(limit); }
/** * Updates checksum with the provided ByteBuffer. * Resets position back to its original values on return. * This method is *NOT* thread-safe. */ public static void updateChecksum(CRC32 checksum, ByteBuffer buffer) { int position = buffer.position(); checksum.update(buffer); buffer.position(position); }
private static boolean isRepackingRequired(FileStore store, Entry entry, int type, int file) { ByteBuffer buffer; try { buffer = store.read(type, file); } catch (IOException ex) { return true; } if (buffer.capacity() <= 2) { return true; } byte[] bytes = new byte[buffer.limit() - 2];// last two bytes are the // version and shouldn't be // included buffer.position(0); buffer.get(bytes, 0, bytes.length); CRC32 crc = new CRC32(); crc.update(bytes, 0, bytes.length); if ((int) crc.getValue() != entry.getCrc()) { return true; } buffer.position(buffer.limit() - 2); if ((buffer.getShort() & 0xFFFF) != entry.getVersion()) { return true; } return false; }
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; }
public ZioEntry( String name, String sourceDataFile) throws IOException { zipInput = new ZipInput( sourceDataFile); filename = name; fileComment = ""; this.compression = 0; this.size = (int)zipInput.getFileLength(); this.compressedSize = this.size; if (getLogger().isDebugEnabled()) getLogger().debug(String.format("Computing CRC for %s, size=%d",sourceDataFile,size)); // compute CRC CRC32 crc = new CRC32(); byte[] buffer = new byte[8096]; int numRead = 0; while (numRead != size) { int count = zipInput.read( buffer, 0, Math.min( buffer.length, (this.size - numRead))); if (count > 0) { crc.update( buffer, 0, count); numRead += count; } } this.crc32 = (int)crc.getValue(); zipInput.seek(0); this.dataPosition = 0; extraData = new byte[0]; setTime( new File(sourceDataFile).lastModified()); }
/** * Create a fresh JAR file. * @param jar the file to create * @param contents keys are JAR entry paths, values are text contents (will be written in UTF-8) * @param manifest a manifest to store (key/value pairs for main section) */ public static void createJar(File jar, Map<String,String> contents, Map<String,String> manifest) throws IOException { // XXX use TestFileUtils.writeZipFile Manifest m = new Manifest(); m.getMainAttributes().putValue("Manifest-Version", "1.0"); // workaround for JDK bug for (Map.Entry<String,String> line : manifest.entrySet()) { m.getMainAttributes().putValue(line.getKey(), line.getValue()); } jar.getParentFile().mkdirs(); OutputStream os = new FileOutputStream(jar); try { JarOutputStream jos = new JarOutputStream(os, m); Iterator it = contents.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); String path = (String) entry.getKey(); byte[] data = ((String) entry.getValue()).getBytes("UTF-8"); JarEntry je = new JarEntry(path); je.setSize(data.length); CRC32 crc = new CRC32(); crc.update(data); je.setCrc(crc.getValue()); jos.putNextEntry(je); jos.write(data); } jos.close(); } finally { os.close(); } }
public static long getCrc32(final InputStream input) throws IOException { CRC32 crc = new CRC32(); final byte[] buffer = new byte[BUFFER_SIZE]; int readLength; while ((readLength = input.read(buffer)) != -1) { crc.update(buffer, 0, readLength); } return crc.getValue(); }
private String crcOf(StringBuffer text) { CRC32 crc = new CRC32(); try { crc.update(text.toString().replaceAll("\\s+", " ").trim().getBytes("UTF-8")); } catch (UnsupportedEncodingException ex) { throw new BuildException(ex); } return Long.toHexString(crc.getValue()).toUpperCase(Locale.ENGLISH); }
private long computeCRC32(InputStream is) throws IOException { byte[] buf = new byte[4096]; CRC32 crc32 = new CRC32(); int read; while ((read = is.read(buf)) != -1) { crc32.update(buf, 0, read); } return crc32.getValue(); }
void addFileForRoot(File file) throws IOException { CRC32 crc = crcForFile(file); if (!file.getPath().startsWith(nbPath)) { throw new BuildException("File " + file + " needs to be under " + nbPath); } String rel = file.getPath().substring(nbPath.length()).replace(File.separatorChar, '/'); if (rel.startsWith("/")) { rel = rel.substring(1); } addFileWithCrc(rel, "" + crc.getValue()); }
long computeCRC(File inFile) throws IOException { byte[] buffer = new byte[8192]; CRC32 crc = new CRC32(); try (FileInputStream fis = new FileInputStream(inFile); BufferedInputStream bis = new BufferedInputStream(fis)) { int n = bis.read(buffer); while (n > 0) { crc.update(buffer, 0, n); n = bis.read(buffer); } } return crc.getValue(); }
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(); }
private static void registerCRC32Plugins(InvocationPlugins plugins, GraalHotSpotVMConfig config, BytecodeProvider bytecodeProvider) { if (config.useCRC32Intrinsics) { Registration r = new Registration(plugins, CRC32.class, bytecodeProvider); r.registerMethodSubstitution(CRC32Substitutions.class, "update", int.class, int.class); if (Java8OrEarlier) { r.registerMethodSubstitution(CRC32Substitutions.class, "updateBytes", int.class, byte[].class, int.class, int.class); r.registerMethodSubstitution(CRC32Substitutions.class, "updateByteBuffer", int.class, long.class, int.class, int.class); } else { r.registerMethodSubstitution(CRC32Substitutions.class, "updateBytes0", int.class, byte[].class, int.class, int.class); r.registerMethodSubstitution(CRC32Substitutions.class, "updateByteBuffer0", int.class, long.class, int.class, int.class); } } }
/** * Partition an identifier into a directory structure * @param identifier the identifier * @return a string usable as a directory path */ public static String partition(final String identifier) { requireNonNull(identifier, "identifier must not be null!"); final StringJoiner joiner = new StringJoiner(separator); final CRC32 hasher = new CRC32(); hasher.update(identifier.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(md5Hex(identifier)); return joiner.toString(); }
/** * Check that jar: URLs are correctly mapped back into JarFileSystem resources. * @see "#39190" */ public void testJarMapping() throws Exception { clearWorkDir(); File workdir = getWorkDir(); File jar = new File(workdir, "test.jar"); String textPath = "x.txt"; OutputStream os = new FileOutputStream(jar); try { JarOutputStream jos = new JarOutputStream(os); jos.setMethod(ZipEntry.STORED); JarEntry entry = new JarEntry(textPath); entry.setSize(0L); entry.setTime(System.currentTimeMillis()); entry.setCrc(new CRC32().getValue()); jos.putNextEntry(entry); jos.flush(); jos.close(); } finally { os.close(); } assertTrue("JAR was created", jar.isFile()); assertTrue("JAR is not empty", jar.length() > 0L); JarFileSystem jfs = new JarFileSystem(); jfs.setJarFile(jar); Repository.getDefault().addFileSystem(jfs); FileObject rootFO = jfs.getRoot(); FileObject textFO = jfs.findResource(textPath); assertNotNull("JAR contains a/b.txt", textFO); String rootS = "jar:" + BaseUtilities.toURI(jar) + "!/"; URL rootU = new URL(rootS); URL textU = new URL(rootS + textPath); assertEquals("correct FO -> URL for root", rootU, URLMapper.findURL(rootFO, URLMapper.EXTERNAL)); assertEquals("correct FO -> URL for " + textPath, textU, URLMapper.findURL(textFO, URLMapper.EXTERNAL)); assertTrue("correct URL -> FO for root", Arrays.asList(URLMapper.findFileObjects(rootU)).contains(rootFO)); assertTrue("correct URL -> FO for " + textPath, Arrays.asList(URLMapper.findFileObjects(textU)).contains(textFO)); }
/** * Create a {@link CompressedXContent} out of a {@link ToXContent} instance. */ public CompressedXContent(ToXContent xcontent, XContentType type, ToXContent.Params params) throws IOException { BytesStreamOutput bStream = new BytesStreamOutput(); OutputStream compressedStream = CompressorFactory.defaultCompressor().streamOutput(bStream); CRC32 crc32 = new CRC32(); OutputStream checkedStream = new CheckedOutputStream(compressedStream, crc32); try (XContentBuilder builder = XContentFactory.contentBuilder(type, checkedStream)) { builder.startObject(); xcontent.toXContent(builder, params); builder.endObject(); } this.bytes = bStream.bytes().toBytes(); this.crc32 = (int) crc32.getValue(); assertConsistent(); }