summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/crcchecker
diff options
context:
space:
mode:
authorNils Schwabe2014-07-04 12:50:59 +0200
committerNils Schwabe2014-07-04 12:50:59 +0200
commitd1ab55e39b392e62ca1f1e41e3c70cf48d676885 (patch)
tree2fcf410f3a7ff4a3acae9ad5502fc933f8887e2c /src/main/java/org/openslx/imagemaster/crcchecker
parentFix that Globals are initialized before servers are starting up (diff)
downloadmasterserver-d1ab55e39b392e62ca1f1e41e3c70cf48d676885.tar.gz
masterserver-d1ab55e39b392e62ca1f1e41e3c70cf48d676885.tar.xz
masterserver-d1ab55e39b392e62ca1f1e41e3c70cf48d676885.zip
Change handling of connections and add support for download
Diffstat (limited to 'src/main/java/org/openslx/imagemaster/crcchecker')
-rw-r--r--src/main/java/org/openslx/imagemaster/crcchecker/CRCChecker.java59
-rw-r--r--src/main/java/org/openslx/imagemaster/crcchecker/ImageFile.java22
2 files changed, 43 insertions, 38 deletions
diff --git a/src/main/java/org/openslx/imagemaster/crcchecker/CRCChecker.java b/src/main/java/org/openslx/imagemaster/crcchecker/CRCChecker.java
index 1baa994..78ac4c7 100644
--- a/src/main/java/org/openslx/imagemaster/crcchecker/CRCChecker.java
+++ b/src/main/java/org/openslx/imagemaster/crcchecker/CRCChecker.java
@@ -12,68 +12,65 @@ import org.openslx.imagemaster.Globals;
public class CRCChecker
{
-
+
private static Logger log = Logger.getLogger( CRCChecker.class );
-
+
/**
- * Checks the CRC sum of given blocks of a given imageFile against a given crcFile.
+ * Checks the CRC sum of given blocks of a given imageFile against a given crcFile.
+ * The caller needs to make sure that block that are going to be checked are complete!
+ *
* @param imageFile The imageFile to check
* @param crcFile The crcFile to check against
* @param blocks The blocks to check
* @return List of blocks where the crc matches, or null if the crc file is corrupted
*/
- public static List<Integer> checkCRC(String imageFile, String crcFile, List<Integer> blocks) throws IOException
+ public static List<Integer> checkCRC( String imageFile, String crcFile, List<Integer> blocks ) throws IOException
{
List<Integer> result = new LinkedList<>();
-
+
ImageFile image = new ImageFile( imageFile, Globals.blockSize );
CRCFile crc = new CRCFile( crcFile );
-
- log.debug( "Checking image file: '" + imageFile + "' with crc file: '" + crcFile + "'");
+
+ log.debug( "Checking image file: '" + imageFile + "' with crc file: '" + crcFile + "'" );
try {
- if (!crc.isValid()) return null;
+ if ( !crc.isValid() )
+ return null;
// TODO: also return null if the crc file contains the wrong number of checksums (only makes sense if the upload is complete)
- } catch (IOException e) {
+ } catch ( IOException e ) {
throw new IOException( "Could not read CRC file", e );
}
-
- // file is smaller than one block - no need to check crc yet
- // TODO: Needs more logic, if the image is complete and < 16MB, we still need to check the block.
- // The caller should make sure to only check crc of blocks that are finished downloading
- if (image.length() < Globals.blockSize) {
- return result;
- }
+
// check all blocks
- for (Integer blockN : blocks) {
- byte[] block;
+ byte[] block = new byte[Globals.blockSize];
+ for ( Integer blockN : blocks ) {
try {
- // TODO: For performance improvements maybe prealloc the array outside the loop with a size of Globals.BLOCK_SIZE
- // and then make getBlock(blockN, block) return the actual size of the block (Should always be BLOCK_SIZE except
- // for the last block), so you never need to create fresh byte arrays inside the loop
- block = image.getBlock( blockN );
+ image.getBlock( blockN, block );
} catch ( IOException e ) {
throw new IOException( "Could not read image file", e );
}
-
- if (block == null) continue; // some error occured (for example: someone tried to check a block that is not in the file)
-
+
+ if ( block == null )
+ continue; // some error occured (for example: someone tried to check a block that is not in the file)
+
// check this block with CRC32
// add this block to result, if check was ok with CRC file
-
+
CRC32 crcCalc = new CRC32();
crcCalc.update( block );
- int crcSum = Integer.reverseBytes( (int) crcCalc.getValue() );
+ int crcSum = Integer.reverseBytes( (int)crcCalc.getValue() );
int crcSumFromFile;
try {
crcSumFromFile = crc.getCRCSum( blockN );
} catch ( IOException e ) {
throw new IOException( "Could not read CRC file", e );
}
-
- if (crcSum == crcSumFromFile) result.add( blockN );
- else log.debug(blockN + " was invalid");
+
+ if ( crcSum == crcSumFromFile )
+ result.add( blockN );
+ else
+ log.debug( blockN + " was invalid" );
}
-
+
return result;
}
}
diff --git a/src/main/java/org/openslx/imagemaster/crcchecker/ImageFile.java b/src/main/java/org/openslx/imagemaster/crcchecker/ImageFile.java
index 9e77537..cb8c700 100644
--- a/src/main/java/org/openslx/imagemaster/crcchecker/ImageFile.java
+++ b/src/main/java/org/openslx/imagemaster/crcchecker/ImageFile.java
@@ -23,13 +23,15 @@ public class ImageFile
/**
* Get a certain block (uses RandomAccessFile)
+ * If the last block is not full an array with a smaller size is set
+ * and the actual number of bytes is returned.
* @param block The number of the block you want to get
- * @return The specified block
+ * @return The actual size of the array or 0 if the block number is < 0 or the block is not in the file
* @throws IOException When file was not found or could not be read
*/
- public byte[] getBlock(int block) throws IOException {
- if (block < 0) return null;
- if (block > f.length()/blockSize) return null;
+ public int getBlock(int block, byte[] array) throws IOException {
+ if (block < 0) return 0;
+ if (block > f.length()/blockSize) return 0;
if (file == null) {
file = new RandomAccessFile( f, "r" );
@@ -37,10 +39,16 @@ public class ImageFile
file.seek( (long)block * blockSize );
long remaining = length() - (block * blockSize);
- byte[] result = new byte[ ( blockSize > remaining )? (int) remaining : blockSize ]; // only read available bytes
- file.read( result );
- return result;
+ if (blockSize > remaining) {
+ array = new byte[(int)remaining]; // only read available bytes
+ file.read( array );
+ return (int)remaining; // return actual size of array
+ } else {
+ // size of array is ok, read the full array and return block size
+ file.read( array );
+ return this.blockSize;
+ }
}
public long length() {