diff options
Diffstat (limited to 'src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java')
-rw-r--r-- | src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java | 38 |
1 files changed, 28 insertions, 10 deletions
diff --git a/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java b/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java index 49c909d..7d059fc 100644 --- a/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java +++ b/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java @@ -11,10 +11,7 @@ import java.util.List; import java.util.zip.CRC32; /** - * Represents a crcfile - * - * @author nils - * + * Represents a crc file */ public class CRCFile { @@ -46,7 +43,7 @@ public class CRCFile DataOutputStream dos = new DataOutputStream( fos ); for ( Integer sum : listOfCrcSums ) { - dos.writeInt( Integer.reverseBytes( sum.intValue() ) ); // save byte-reversed integers to match right order in crc file + dos.writeInt( sum.intValue() ); // save byte-reversed integers to match right order in crc file TODO: is that right? } dos.close(); @@ -54,6 +51,23 @@ public class CRCFile } /** + * Checks if given sums are valid. + * @param listOfCrcSums + * @return + */ + public static boolean sumsAreValid( List<Integer> listOfCrcSums ) + { + byte[] bytes = new byte[ ( listOfCrcSums.size() - 1 ) * Integer.SIZE / 8 ]; + int masterSum = listOfCrcSums.remove( 0 ); + for ( int i = 0; i < bytes.length; i++ ) { + bytes[i] = listOfCrcSums.remove( 0 ).byteValue(); + } + CRC32 crcCalc = new CRC32(); + crcCalc.update( bytes ); + return ( masterSum == Integer.reverseBytes( (int)crcCalc.getValue() ) ); + } + + /** * Checks if this crc file is valid. * (If the crc over the file is equal to the first crc sum.) * @@ -90,7 +104,8 @@ public class CRCFile */ public int getCRCSum( int blockNumber ) throws IOException { - if (crcSums == null) loadSums(); + if ( crcSums == null ) + loadSums(); if ( blockNumber < 0 ) return 0; @@ -99,17 +114,20 @@ public class CRCFile return crcSums.get( blockNumber ); } - + /** * Returns the loaded crcSums. + * * @return The loaded crcSums * @throws IOException If the crcSums could not be loaded from file */ - public List<Integer> getCrcSums() throws IOException { - if (crcSums == null) loadSums(); + public List<Integer> getCrcSums() throws IOException + { + if ( crcSums == null ) + loadSums(); return this.crcSums; } - + private void loadSums() throws IOException { // the crcSums were not read yet |