From 6ea561385a291bb78077c368b1df5f997e11d80e Mon Sep 17 00:00:00 2001 From: Nils Schwabe Date: Tue, 29 Jul 2014 15:55:52 +0200 Subject: Rename classes and methods to fit camelCase --- .../openslx/imagemaster/crcchecker/CRCChecker.java | 80 ---------- .../openslx/imagemaster/crcchecker/CRCFile.java | 167 -------------------- .../openslx/imagemaster/crcchecker/ClassTest.java | 4 +- .../openslx/imagemaster/crcchecker/CrcChecker.java | 80 ++++++++++ .../openslx/imagemaster/crcchecker/CrcFile.java | 172 +++++++++++++++++++++ .../openslx/imagemaster/crcchecker/ImageFile.java | 3 +- 6 files changed, 256 insertions(+), 250 deletions(-) delete mode 100644 src/main/java/org/openslx/imagemaster/crcchecker/CRCChecker.java delete mode 100644 src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java create mode 100644 src/main/java/org/openslx/imagemaster/crcchecker/CrcChecker.java create mode 100644 src/main/java/org/openslx/imagemaster/crcchecker/CrcFile.java (limited to 'src/main/java') diff --git a/src/main/java/org/openslx/imagemaster/crcchecker/CRCChecker.java b/src/main/java/org/openslx/imagemaster/crcchecker/CRCChecker.java deleted file mode 100644 index d0384a6..0000000 --- a/src/main/java/org/openslx/imagemaster/crcchecker/CRCChecker.java +++ /dev/null @@ -1,80 +0,0 @@ -package org.openslx.imagemaster.crcchecker; - -import java.io.IOException; -import java.util.zip.CRC32; - -public class CRCChecker -{ - private static final int blockSize = 16 * 1024 * 1024; - - private ImageFile imageFile; - private CRCFile crcFile; - - private byte[] block = new byte[ blockSize ]; // array that is used to read the blocks - - /** - * Initialize a crc checker with an image file and a crc file. - * - * @param imageFile The image file to check - * @param crcFile The crc file to check against - */ - public CRCChecker( ImageFile imageFile, CRCFile crcFile ) - { - this.imageFile = imageFile; - this.crcFile = crcFile; - } - - public void done() - { - imageFile.close(); - } - - public boolean hasValidCrcFile() - { - try { - return crcFile.isValid(); - } catch ( IOException e ) { - return false; - } - } - - /** - * Checks a chosen block against the crc file. - * - * @param block The block to check - * @return Whether the block was valid or not - * @throws IOException When image or crc file could not be read. - */ - public boolean checkBlock( int blockNumber ) throws IOException - { - if ( !this.hasValidCrcFile() ) - return false; - - int length; - try { - length = imageFile.getBlock( blockNumber, block ); - } catch ( IOException e ) { - throw new IOException( "Could not read image file", e ); - } - - if ( length <= 0 ) - return false; - - CRC32 crcCalc = new CRC32(); - if ( length == blockSize ) { - crcCalc.update( block ); - } else { - crcCalc.update( block, 0, length ); - } - - int crcSum = Integer.reverseBytes( (int)crcCalc.getValue() ); - int crcSumFromFile; - try { - crcSumFromFile = crcFile.getCRCSum( blockNumber ); - } catch ( IOException e ) { - throw new IOException( "Could not read CRC file", e ); - } - - return ( crcSum == crcSumFromFile ); - } -} diff --git a/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java b/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java deleted file mode 100644 index 357082b..0000000 --- a/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java +++ /dev/null @@ -1,167 +0,0 @@ -package org.openslx.imagemaster.crcchecker; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -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; - -/** - * Represents a crc file - */ -public class CRCFile -{ - private File file = null; - private List crcSums = null; - private Boolean valid = null; - - /** - * Loads a crcFile from file - * - * @param filename - */ - public CRCFile( String filename ) - { - this.file = new File( filename ); - } - - /** - * Creates a crc file which is not on the drive. - * - * @param crcSums - */ - public CRCFile( List 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. - * - * @param listOfCrcSums The list of the crc sums that are going into the crc file - * @param filename Where to save the created crc file - * @throws IOException If it's not possible to write the file - */ - public static CRCFile writeCrcFile( List listOfCrcSums, String filename ) throws IOException - { - File file = new File( filename ); - FileOutputStream fos = new FileOutputStream( file ); - DataOutputStream dos = new DataOutputStream( fos ); - - for ( Integer sum : listOfCrcSums ) { - dos.writeInt( sum.intValue() ); - } - - dos.close(); - return new CRCFile( filename ); - } - - /** - * Checks if given sums are valid. - * - * @param listOfCrcSums - * @return - */ - public static boolean sumsAreValid( List listOfCrcSums ) - { - if ( listOfCrcSums == null || listOfCrcSums.isEmpty() ) - return false; - - int masterSum = listOfCrcSums.get( 0 ); // don't use the first sum for the calculation because it is the sum over the other sums - int size = listOfCrcSums.size(); - - CRC32 crcCalc = new CRC32(); - - for ( int i = 1; i < size; i++ ) { - crcCalc.update( ByteBuffer.allocate( 4 ).putInt( listOfCrcSums.get( i ) ).array() ); // update the crc calculator with the next 4 bytes of the integer - } - - 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.) - * - * @return Whether the crc file is valid - * @throws IOException If the file could not be read or could not be found - */ - public boolean isValid() throws IOException - { - if ( valid == null ) { - if ( file == null ) { - valid = sumsAreValid( this.crcSums ); - } else { - if ( crcSums == null ) - loadSums(); - valid = sumsAreValid( this.crcSums ); - } - } - return valid; - } - - /** - * Get a specified crcSum for a block number - * - * @param blockNumber - * @return The crcSum or 0 if the block number is invalid - * @throws IOException If the crcSums could not be loaded from file - */ - public int getCRCSum( int blockNumber ) throws IOException - { - if ( crcSums == null ) - loadSums(); - if (crcSums.size() == 0) - return 0; - - if ( blockNumber < 0 ) - return 0; - if ( blockNumber > crcSums.size() - 2 ) - return 0; - - return crcSums.get( blockNumber + 1 ); - } - - /** - * Returns the loaded crcSums. - * - * @return The loaded crcSums - * @throws IOException If the crcSums could not be loaded from file - */ - public List getCrcSums() throws IOException - { - if ( crcSums == null ) - loadSums(); - if (crcSums.size() == 0) - return new ArrayList<>(); - return this.crcSums; - } - - 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++ ) { - crcSums.add( dis.readInt() ); - } - dis.close(); - } - - public int getMasterSum() throws IOException - { - if ( crcSums == null ) - loadSums(); - if (crcSums.size() == 0) - return 0; - return this.crcSums.get( 0 ); - } -} diff --git a/src/main/java/org/openslx/imagemaster/crcchecker/ClassTest.java b/src/main/java/org/openslx/imagemaster/crcchecker/ClassTest.java index 37f7fb2..f1e878d 100644 --- a/src/main/java/org/openslx/imagemaster/crcchecker/ClassTest.java +++ b/src/main/java/org/openslx/imagemaster/crcchecker/ClassTest.java @@ -14,13 +14,13 @@ public class ClassTest String filenameCrc = args[1]; final int blockSize = 16 * 1024 * 1024; - CRCFile f = new CRCFile( filenameCrc ); + CrcFile f = new CrcFile( filenameCrc ); System.out.println( "Master sum: '" + f.getMasterSum() + "'" ); System.out.println( f.getCrcSums() ); System.out.println( "CRC file is '" + ( ( f.isValid() ) ? "valid" : "invalid" ) + "'" ); ImageFile imageFile = new ImageFile( filename, blockSize ); - CRCChecker crcFile = new CRCChecker( imageFile, f ); + CrcChecker crcFile = new CrcChecker( imageFile, f ); int blocks = getNumberOfBlocks( imageFile.length(), blockSize ); for ( int i = 0; i < blocks; i++ ) { diff --git a/src/main/java/org/openslx/imagemaster/crcchecker/CrcChecker.java b/src/main/java/org/openslx/imagemaster/crcchecker/CrcChecker.java new file mode 100644 index 0000000..882f565 --- /dev/null +++ b/src/main/java/org/openslx/imagemaster/crcchecker/CrcChecker.java @@ -0,0 +1,80 @@ +package org.openslx.imagemaster.crcchecker; + +import java.io.IOException; +import java.util.zip.CRC32; + +public class CrcChecker +{ + private static final int blockSize = 16 * 1024 * 1024; + + private ImageFile imageFile; + private CrcFile crcFile; + + private byte[] block = new byte[ blockSize ]; // array that is used to read the blocks + + /** + * Initialize a crc checker with an image file and a crc file. + * + * @param imageFile The image file to check + * @param crcFile The crc file to check against + */ + public CrcChecker( ImageFile imageFile, CrcFile crcFile ) + { + this.imageFile = imageFile; + this.crcFile = crcFile; + } + + public void done() + { + imageFile.close(); + } + + public boolean hasValidCrcFile() + { + try { + return crcFile.isValid(); + } catch ( IOException e ) { + return false; + } + } + + /** + * Checks a chosen block against the crc file. + * + * @param block The block to check + * @return Whether the block was valid or not + * @throws IOException When image or crc file could not be read. + */ + public boolean checkBlock( int blockNumber ) throws IOException + { + if ( !this.hasValidCrcFile() ) + return false; + + int length; + try { + length = imageFile.getBlock( blockNumber, block ); + } catch ( IOException e ) { + throw new IOException( "image", e ); + } + + if ( length <= 0 ) + return false; + + CRC32 crcCalc = new CRC32(); + if ( length == blockSize ) { + crcCalc.update( block ); + } else { + crcCalc.update( block, 0, length ); + } + + int crcSum = Integer.reverseBytes( (int)crcCalc.getValue() ); + int crcSumFromFile; + try { + crcSumFromFile = crcFile.getCRCSum( blockNumber ); + } catch ( IOException e ) { + throw new IOException( "crc", e ); + } + + return ( crcSum == crcSumFromFile ); + } +} diff --git a/src/main/java/org/openslx/imagemaster/crcchecker/CrcFile.java b/src/main/java/org/openslx/imagemaster/crcchecker/CrcFile.java new file mode 100644 index 0000000..542234b --- /dev/null +++ b/src/main/java/org/openslx/imagemaster/crcchecker/CrcFile.java @@ -0,0 +1,172 @@ +package org.openslx.imagemaster.crcchecker; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +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; + +/** + * Represents a crc file + */ +public class CrcFile +{ + private File file = null; + private List crcSums = null; + private Boolean valid = null; + + /** + * Loads a crcFile from file + * + * @param filename + */ + public CrcFile( String filename ) + { + this.file = new File( filename ); + } + + /** + * Creates a crc file which is not on the drive. + * + * @param crcSums + */ + public CrcFile( List 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. + * Deletes existing files with the same name. + * + * @param listOfCrcSums The list of the crc sums that are going into the crc file + * @param filename Where to save the created crc file + * @throws IOException If it's not possible to write the file + */ + public static CrcFile writeCrcFile( List listOfCrcSums, String filename ) throws IOException + { + File file = new File( filename ); + + if ( file.exists() ) + file.delete(); + + FileOutputStream fos = new FileOutputStream( file ); + DataOutputStream dos = new DataOutputStream( fos ); + + for ( Integer sum : listOfCrcSums ) { + dos.writeInt( sum.intValue() ); + } + + dos.close(); + return new CrcFile( filename ); + } + + /** + * Checks if given sums are valid. + * + * @param listOfCrcSums + * @return + */ + public static boolean sumsAreValid( List listOfCrcSums ) + { + if ( listOfCrcSums == null || listOfCrcSums.isEmpty() ) + return false; + + int masterSum = listOfCrcSums.get( 0 ); // don't use the first sum for the calculation because it is the sum over the other sums + int size = listOfCrcSums.size(); + + CRC32 crcCalc = new CRC32(); + + for ( int i = 1; i < size; i++ ) { + crcCalc.update( ByteBuffer.allocate( 4 ).putInt( listOfCrcSums.get( i ) ).array() ); // update the crc calculator with the next 4 bytes of the integer + } + + 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.) + * + * @return Whether the crc file is valid + * @throws IOException If the file could not be read or could not be found + */ + public boolean isValid() throws IOException + { + if ( valid == null ) { + if ( file == null ) { + valid = sumsAreValid( this.crcSums ); + } else { + if ( crcSums == null ) + loadSums(); + valid = sumsAreValid( this.crcSums ); + } + } + return valid; + } + + /** + * Get a specified crcSum for a block number + * + * @param blockNumber + * @return The crcSum or 0 if the block number is invalid + * @throws IOException If the crcSums could not be loaded from file + */ + public int getCRCSum( int blockNumber ) throws IOException + { + if ( crcSums == null ) + loadSums(); + if ( crcSums.size() == 0 ) + return 0; + + if ( blockNumber < 0 ) + return 0; + if ( blockNumber > crcSums.size() - 2 ) + return 0; + + return crcSums.get( blockNumber + 1 ); + } + + /** + * Returns the loaded crcSums. + * + * @return The loaded crcSums + * @throws IOException If the crcSums could not be loaded from file + */ + public List getCrcSums() throws IOException + { + if ( crcSums == null ) + loadSums(); + if ( crcSums.size() == 0 ) + return new ArrayList<>(); + return this.crcSums; + } + + 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++ ) { + crcSums.add( dis.readInt() ); + } + dis.close(); + } + + public int getMasterSum() throws IOException + { + if ( crcSums == null ) + loadSums(); + if ( crcSums.size() == 0 ) + return 0; + return this.crcSums.get( 0 ); + } +} diff --git a/src/main/java/org/openslx/imagemaster/crcchecker/ImageFile.java b/src/main/java/org/openslx/imagemaster/crcchecker/ImageFile.java index 9c77984..f661b75 100644 --- a/src/main/java/org/openslx/imagemaster/crcchecker/ImageFile.java +++ b/src/main/java/org/openslx/imagemaster/crcchecker/ImageFile.java @@ -64,7 +64,8 @@ public class ImageFile return; file.close(); file = null; - } catch ( IOException e ) {/* Can't do anything about it.*/ + } catch ( IOException e ) { + /* Can't do anything about it. */ } } } -- cgit v1.2.3-55-g7522