diff options
author | Nils Schwabe | 2014-07-14 14:56:00 +0200 |
---|---|---|
committer | Nils Schwabe | 2014-07-14 14:56:00 +0200 |
commit | 77c49b92b3a5476e51ecd0c7bf9d4e6105e5022d (patch) | |
tree | 424d48a2521601c89c787b8d54e743e23fb66f1a /src/main/java/org/openslx/imagemaster | |
parent | Fix typo in ImageData "conentOperatingSystem" => "contentOperatingSystem" (diff) | |
download | master-sync-shared-77c49b92b3a5476e51ecd0c7bf9d4e6105e5022d.tar.gz master-sync-shared-77c49b92b3a5476e51ecd0c7bf9d4e6105e5022d.tar.xz master-sync-shared-77c49b92b3a5476e51ecd0c7bf9d4e6105e5022d.zip |
Change behaviour of CRCFile and CRCChecker
Diffstat (limited to 'src/main/java/org/openslx/imagemaster')
-rw-r--r-- | src/main/java/org/openslx/imagemaster/crcchecker/CRCChecker.java | 5 | ||||
-rw-r--r-- | src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java | 47 |
2 files changed, 27 insertions, 25 deletions
diff --git a/src/main/java/org/openslx/imagemaster/crcchecker/CRCChecker.java b/src/main/java/org/openslx/imagemaster/crcchecker/CRCChecker.java index b7f288e..f081294 100644 --- a/src/main/java/org/openslx/imagemaster/crcchecker/CRCChecker.java +++ b/src/main/java/org/openslx/imagemaster/crcchecker/CRCChecker.java @@ -18,9 +18,10 @@ public class CRCChecker * @param imageFile The image file to check * @param crcFile The crc file to check against */ - public CRCChecker( String imageFilename, String crcFilename ) + public CRCChecker( ImageFile imageFile, CRCFile crcFile) { - this.imageFile = new ImageFile( imageFilename, blockSize ); + this.imageFile = imageFile; + this.crcFile = crcFile; } public void done() diff --git a/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java b/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java index 7d059fc..ade7bc0 100644 --- a/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java +++ b/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java @@ -15,7 +15,7 @@ import java.util.zip.CRC32; */ public class CRCFile { - private File file; + private File file = null; private List<Integer> crcSums = null; /** @@ -29,6 +29,16 @@ public class CRCFile } /** + * Creates a crc file which is not on the drive. + * + * @param crcSums + */ + public CRCFile( List<Integer> crcSums ) + { + this.crcSums = crcSums; + } + + /** * Creates a new crc file with the given sums. * The first crc sum in the list needs to be the sum over the other sums. * @@ -52,6 +62,7 @@ public class CRCFile /** * Checks if given sums are valid. + * * @param listOfCrcSums * @return */ @@ -76,23 +87,13 @@ public class CRCFile */ public boolean isValid() throws IOException { - FileInputStream fis = new FileInputStream( file ); - DataInputStream dis = new DataInputStream( fis ); - - int crcSum = dis.readInt(); - - CRC32 crcCalc = new CRC32(); - - byte[] bytes = new byte[ (int)file.length() - Integer.SIZE / 8 ]; // byte array with length of the file minus the first crc sum (=4byte) - fis.read( bytes ); - crcCalc.update( bytes ); - - dis.close(); - - if ( crcSum == Integer.reverseBytes( (int)crcCalc.getValue() ) ) - return true; - else - return false; + if ( file == null ) { + return sumsAreValid( this.crcSums ); + } else { + if ( crcSums == null ) + loadSums(); + return sumsAreValid( this.crcSums ); + } } /** @@ -109,10 +110,10 @@ public class CRCFile if ( blockNumber < 0 ) return 0; - if ( blockNumber > crcSums.size() - 1 ) + if ( blockNumber > crcSums.size() - 2 ) return 0; - return crcSums.get( blockNumber ); + return crcSums.get( blockNumber + 1 ); } /** @@ -130,13 +131,13 @@ public class CRCFile private void loadSums() throws IOException { + if ( crcSums != null ) + return; // the crcSums were not read yet DataInputStream dis = new DataInputStream( new FileInputStream( file ) ); crcSums = new ArrayList<>(); for ( int i = 0; i < file.length() / 4; i++ ) { - int s = dis.readInt(); - if ( i > 0 ) - crcSums.add( s ); // skip the first crcSum because it's the sum over the crcFile + crcSums.add( dis.readInt() ); } dis.close(); } |