summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/crcchecker
diff options
context:
space:
mode:
authorNils Schwabe2014-06-30 17:11:03 +0200
committerNils Schwabe2014-06-30 17:11:03 +0200
commit1a3dbab6ca7118f4ca9f61043f416f074ede13bc (patch)
treefad14555be544c3ba2afdf31b8f315364a67e7a6 /src/main/java/org/openslx/imagemaster/crcchecker
parent[Webinterface] Add "images" tab (diff)
downloadmasterserver-1a3dbab6ca7118f4ca9f61043f416f074ede13bc.tar.gz
masterserver-1a3dbab6ca7118f4ca9f61043f416f074ede13bc.tar.xz
masterserver-1a3dbab6ca7118f4ca9f61043f416f074ede13bc.zip
Add implementation for the new up- and download protocoll
Remove some old stuff that is not needed anymore Fix some small bugs
Diffstat (limited to 'src/main/java/org/openslx/imagemaster/crcchecker')
-rw-r--r--src/main/java/org/openslx/imagemaster/crcchecker/CRCChecker.java15
-rw-r--r--src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java2
-rw-r--r--src/main/java/org/openslx/imagemaster/crcchecker/ImageFile.java29
3 files changed, 21 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 04f6a22..29b7090 100644
--- a/src/main/java/org/openslx/imagemaster/crcchecker/CRCChecker.java
+++ b/src/main/java/org/openslx/imagemaster/crcchecker/CRCChecker.java
@@ -1,14 +1,12 @@
package org.openslx.imagemaster.crcchecker;
-import java.io.DataInputStream;
-import java.io.File;
-import java.io.FileInputStream;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.zip.CRC32;
import org.apache.log4j.Logger;
+import org.openslx.imagemaster.Globals;
public class CRCChecker
@@ -27,9 +25,7 @@ public class CRCChecker
{
List<Integer> result = new LinkedList<>();
- final int blockSize = 16 * 1024 * 1024;
-
- ImageFile image = new ImageFile( imageFile, blockSize );
+ ImageFile image = new ImageFile( imageFile, Globals.blockSize );
CRCFile crc = new CRCFile( crcFile );
log.debug( "Checking image file: '" + imageFile + "' with crc file: '" + crcFile + "'");
@@ -40,7 +36,7 @@ public class CRCChecker
}
// file is smaller than one block - no need to check crc yet
- if (image.length() < blockSize) {
+ if (image.length() < Globals.blockSize) {
return result;
}
// check all blocks
@@ -60,16 +56,15 @@ public class CRCChecker
CRC32 crcCalc = new CRC32();
crcCalc.update( block );
int crcSum = Integer.reverseBytes( (int) crcCalc.getValue() );
- log.debug( "Got crc for block '" + blockN + "': '" + crcSum + "'" );
int crcSumFromFile;
try {
crcSumFromFile = crc.getCRCSum( blockN );
} catch ( IOException e ) {
throw new IOException( "Could not read CRC file", e );
}
- log.debug( "And read crc from file: '" + crcSumFromFile + "'" );
- if (crcSum == crcSumFromFile) result.add( blockN );
+ if (crcSum == crcSumFromFile) result.add( blockN );
+ else log.debug(blockN + " was invalid");
}
return result;
diff --git a/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java b/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java
index 3a75b37..b557ada 100644
--- a/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java
+++ b/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java
@@ -54,7 +54,7 @@ public class CRCFile
*/
public int getCRCSum(int blockNumber) throws IOException {
if (crcSums == null) {
- // the crcSum were not read yet
+ // 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++) {
diff --git a/src/main/java/org/openslx/imagemaster/crcchecker/ImageFile.java b/src/main/java/org/openslx/imagemaster/crcchecker/ImageFile.java
index a6ff8f0..9e77537 100644
--- a/src/main/java/org/openslx/imagemaster/crcchecker/ImageFile.java
+++ b/src/main/java/org/openslx/imagemaster/crcchecker/ImageFile.java
@@ -1,8 +1,8 @@
package org.openslx.imagemaster.crcchecker;
import java.io.File;
-import java.io.FileInputStream;
import java.io.IOException;
+import java.io.RandomAccessFile;
/**
* Representing an image file.
@@ -12,37 +12,38 @@ import java.io.IOException;
*/
public class ImageFile
{
- private File file;
+ private File f;
+ private RandomAccessFile file = null;
private int blockSize;
public ImageFile(String filename, int blockSize) {
- this.file = new File( filename );
+ this.f = new File( filename );
this.blockSize = blockSize;
}
/**
- * Get a certain block
+ * Get a certain block (uses RandomAccessFile)
* @param block The number of the block you want to get
* @return The specified block
* @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 > file.length()/blockSize) return null;
+ if (block > f.length()/blockSize) return null;
- FileInputStream fis = new FileInputStream( file );
+ if (file == null) {
+ file = new RandomAccessFile( f, "r" );
+ }
- fis.skip( block * (long) blockSize );
-
- byte[] result = new byte[ ( blockSize > fis.available() )? fis.available() : blockSize ]; // only read availible bytes
-
- fis.read(result);
- fis.close();
+ 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;
}
public long length() {
- return file.length();
+ return f.length();
}
-}
+} \ No newline at end of file