summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/satellitedaemon/util/CrcFile.java
blob: 59679d310a1afdceb90fc8a10e1eb5131b2c9c27 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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;

// TODO: Merge with code/classes from masterser and put into master-sync-shared

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;
	} 
}