blob: cb8c700b4e326012ade0cab9c642fb6000450b7f (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
package org.openslx.imagemaster.crcchecker;
import java.io.File;
import java.io.IOException;
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) {
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
* @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) {
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;
}
}
public long length() {
return f.length();
}
}
|