diff options
author | Nils Schwabe | 2014-07-07 14:55:43 +0200 |
---|---|---|
committer | Nils Schwabe | 2014-07-07 14:55:43 +0200 |
commit | cf3d1d0b3f7dc8bddd3f87131afc414d53d3c800 (patch) | |
tree | 708768d87c50ff8cdede20988c9e7d4016066e07 /src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java | |
parent | Add port to up- and download infos (diff) | |
download | master-sync-shared-cf3d1d0b3f7dc8bddd3f87131afc414d53d3c800.tar.gz master-sync-shared-cf3d1d0b3f7dc8bddd3f87131afc414d53d3c800.tar.xz master-sync-shared-cf3d1d0b3f7dc8bddd3f87131afc414d53d3c800.zip |
Add argument to send crc sums over thrift
Diffstat (limited to 'src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java')
-rw-r--r-- | src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java | 68 |
1 files changed, 40 insertions, 28 deletions
diff --git a/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java b/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java index 74e3d84..15eba44 100644 --- a/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java +++ b/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java @@ -13,89 +13,101 @@ import java.util.zip.CRC32; /** * Represents a crcfile + * * @author nils - * + * */ public class CRCFile { private File file; private List<Integer> crcSums = null; - + /** * Loads a crcFile from file + * * @param filename */ - CRCFile(String filename) + CRCFile( String filename ) { this.file = new File( filename ); } - + /** * 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 */ - CRCFile(List<Integer> listOfCrcSums, String filename) throws IOException + public CRCFile( List<Integer> listOfCrcSums, String filename ) throws IOException { this.file = new File( filename ); FileOutputStream fos = new FileOutputStream( file ); 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.close(); this.crcSums = listOfCrcSums; } - + /** * 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 { + 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) + + 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 ( crcSum == Integer.reverseBytes( (int)crcCalc.getValue() ) ) + return true; + else + return false; } - + /** * Get a specified crcSum for a block number + * * @param blockNumber * @return The crcSum or 0 if the block number is invalid - * @throws IOException + * @throws IOException */ - public int getCRCSum(int blockNumber) throws IOException { - if (crcSums == null) { + public int getCRCSum( int blockNumber ) throws IOException + { + if ( crcSums == null ) { // 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++) { + 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 + if ( i > 0 ) + crcSums.add( s ); // skip the first crcSum because it's the sum over the crcFile } dis.close(); } - - if (blockNumber < 0) return 0; - if (blockNumber > crcSums.size() - 1) return 0; - + + if ( blockNumber < 0 ) + return 0; + if ( blockNumber > crcSums.size() - 1 ) + return 0; + return crcSums.get( blockNumber ); - } + } } |