From c9f5b5ff1d27899aa296f8d2c7ed792ac6854e77 Mon Sep 17 00:00:00 2001 From: Nils Schwabe Date: Wed, 16 Jul 2014 15:37:34 +0200 Subject: [CRCFile] Add method to get the master sum --- .../openslx/imagemaster/crcchecker/CRCFile.java | 28 +++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java b/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java index ade7bc0..be061ce 100644 --- a/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java +++ b/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java @@ -6,15 +6,19 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; import java.util.zip.CRC32; +import org.apache.log4j.Logger; + /** * Represents a crc file */ public class CRCFile { + private static Logger log = Logger.getLogger( CRCFile.class ); private File file = null; private List crcSums = null; @@ -68,13 +72,24 @@ public class CRCFile */ public static boolean sumsAreValid( List listOfCrcSums ) { - byte[] bytes = new byte[ ( listOfCrcSums.size() - 1 ) * Integer.SIZE / 8 ]; + if ( listOfCrcSums == null || listOfCrcSums.isEmpty() ) + return false; + + byte[] bytes = new byte[ ( listOfCrcSums.size() - 2 ) * Integer.SIZE / 8 ]; + log.debug( listOfCrcSums.size() + ", " + bytes.length ); int masterSum = listOfCrcSums.remove( 0 ); - for ( int i = 0; i < bytes.length; i++ ) { - bytes[i] = listOfCrcSums.remove( 0 ).byteValue(); + byte[] bytesOfInt; + for ( int i = 0; i < ( listOfCrcSums.size() - 1 ); i++ ) { + bytesOfInt = ByteBuffer.allocate( 4 ).putInt( listOfCrcSums.get( 0 ) ).array(); // get the bytes of this integer + bytes[4 * i] = bytesOfInt[0]; + bytes[4 * i + 1] = bytesOfInt[1]; + bytes[4 * i + 2] = bytesOfInt[2]; + bytes[4 * i + 3] = bytesOfInt[3]; } CRC32 crcCalc = new CRC32(); crcCalc.update( bytes ); + log.debug( masterSum ); + log.debug( Integer.reverseBytes( (int)crcCalc.getValue() ) ); return ( masterSum == Integer.reverseBytes( (int)crcCalc.getValue() ) ); } @@ -141,4 +156,11 @@ public class CRCFile } dis.close(); } + + public int getMasterSum() throws IOException + { + if ( crcSums == null ) + loadSums(); + return this.crcSums.get( 0 ); + } } -- cgit v1.2.3-55-g7522