summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/crcchecker/ImageFile.java
diff options
context:
space:
mode:
authorNils Schwabe2014-07-11 16:29:22 +0200
committerNils Schwabe2014-07-11 16:29:22 +0200
commit17609bc347e11117de0e0644209eeae3bf18a1ff (patch)
treeab28d49db821411b27f1614f527a5dd2c79190f4 /src/main/java/org/openslx/imagemaster/crcchecker/ImageFile.java
parentMerge branch 'master' of git.openslx.org:bwlp/master-sync-shared (diff)
downloadmaster-sync-shared-17609bc347e11117de0e0644209eeae3bf18a1ff.tar.gz
master-sync-shared-17609bc347e11117de0e0644209eeae3bf18a1ff.tar.xz
master-sync-shared-17609bc347e11117de0e0644209eeae3bf18a1ff.zip
Fix some todos (new errors, ...)
Diffstat (limited to 'src/main/java/org/openslx/imagemaster/crcchecker/ImageFile.java')
-rw-r--r--src/main/java/org/openslx/imagemaster/crcchecker/ImageFile.java58
1 files changed, 34 insertions, 24 deletions
diff --git a/src/main/java/org/openslx/imagemaster/crcchecker/ImageFile.java b/src/main/java/org/openslx/imagemaster/crcchecker/ImageFile.java
index cb8c700..13bb0fd 100644
--- a/src/main/java/org/openslx/imagemaster/crcchecker/ImageFile.java
+++ b/src/main/java/org/openslx/imagemaster/crcchecker/ImageFile.java
@@ -7,51 +7,61 @@ import java.io.RandomAccessFile;
/**
* Representing an image file.
* Is able to return certain blocks of this file.
+ *
* @author nils
- *
+ *
*/
public class ImageFile
{
private File f;
private RandomAccessFile file = null;
private int blockSize;
-
- public ImageFile(String filename, int blockSize) {
+
+ public ImageFile( String filename, int blockSize )
+ {
this.f = new File( filename );
this.blockSize = blockSize;
}
-
+
/**
* 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 actual size of the array or 0 if the block number is < 0 or the block is not in the file
+ * @return The actual number of read bytes or -1 if nothing was read.
* @throws IOException When file was not found or could not be read
*/
- public int getBlock(int block, byte[] array) throws IOException {
- if (block < 0) return 0;
- if (block > f.length()/blockSize) return 0;
-
- if (file == 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" );
}
-
+
file.seek( (long)block * blockSize );
- long remaining = length() - (block * blockSize);
- 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;
- }
+ return file.read( array );
}
-
- public long length() {
+
+ public long length()
+ {
return f.length();
}
-} \ No newline at end of file
+
+ /**
+ * Closes the file.
+ * It's not a problem to use getBlock() again. The file will then be re-opened.
+ */
+ public void close()
+ {
+ try {
+ file.close();
+ file = null;
+ } catch ( IOException e ) {/* Can't do anything about it.*/}
+ }
+}