package org.openslx.satellitedaemon.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.util.zip.CRC32; import org.apache.log4j.BasicConfigurator; import org.apache.log4j.Logger; public class CrcFile { private static Logger log = Logger.getLogger( CrcFile.class ); public static void make( String path ) throws IOException { int blockSize = 16 * 1024 * 1024; InputStream is = new FileInputStream( path ); CRC32 crc = new CRC32(); int cnt; byte[] block = new byte[blockSize]; byte[] WriteToFile = new byte[0]; log.info( "Inputfile: " + path ); while ( ( cnt = is.read(block) ) != -1 ) { crc.update( cnt ); log.info( "CRC value: " + crc.getValue() ); byte[] latest = ByteBuffer.allocate(8).putLong(crc.getValue()).array(); WriteToFile = concatenateByteArrays( WriteToFile, latest ); } String outPath = path.substring( 0, (path.length() - 4) ).concat( "crc" ); File outFile = new File(outPath); FileOutputStream fos = new FileOutputStream( outFile ); fos.write( WriteToFile ); is.close(); fos.close(); } public static void main( String[] args ) throws IOException { BasicConfigurator.configure(); CrcFile.make( "/tmp/test.vmdk"); } public static byte[] concatenateByteArrays(byte[] a, byte[] b) { byte[] result = new byte[a.length + b.length]; System.arraycopy(a, 0, result, 0, a.length); System.arraycopy(b, 0, result, a.length, b.length); return result; } }