blob: f661b75d892ade2616ab34bddc1a5bb9cda9eef5 (
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
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 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 ) {
file = new RandomAccessFile( f, "r" );
}
file.seek( (long)block * blockSize );
return file.read( array );
}
public long length()
{
return f.length();
}
/**
* Closes the file.
* It's not a problem to use getBlock() again. The file will then be re-opened.
*/
public void close()
{
try {
if ( file == null )
return;
file.close();
file = null;
} catch ( IOException e ) {
/* Can't do anything about it. */
}
}
}
|