summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/crcchecker/ImageFile.java
blob: a6ff8f03dc03f7aefe9edd089afed2dc75e08bef (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
package org.openslx.imagemaster.crcchecker;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * Representing an image file.
 * Is able to return certain blocks of this file.
 * @author nils
 *
 */
public class ImageFile
{
	private File file;
	private int blockSize;
	
	public ImageFile(String filename, int blockSize) {
		this.file = new File( filename );
		this.blockSize = blockSize;
	}
	
	/**
	 * Get a certain block
	 * @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;
		
		FileInputStream fis = new FileInputStream( file );
		
		fis.skip( block * (long) blockSize );
		
		byte[] result = new byte[ ( blockSize > fis.available() )? fis.available() : blockSize ];	// only read availible bytes
		
		fis.read(result);
		fis.close();
		
		return result;
	}
	
	public long length() {
		return file.length();
	}
}