-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileObject.java
72 lines (68 loc) · 1.99 KB
/
FileObject.java
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
72
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
/***
* Utility class to access, read and write pieces to file.
* The associated file should have been created before the use of this class object.
* */
public class FileObject {
private RandomAccessFile fileAccess;
private File file;
private int fileSize;
private int pieceSize;
private String fileName;
FileObject(String fileName, int fileSize, int pieceSize) throws FileNotFoundException {
try {
this.fileName = fileName;
this.fileSize = fileSize;
this.pieceSize = pieceSize;
file = new File(fileName);
if (!file.exists()) {
throw new FileNotFoundException();
}
fileAccess = new RandomAccessFile(file, "rw");
} catch (FileNotFoundException ex) {
}
}
public void cleanUp(){
try {
fileAccess.close();
}catch (IOException ex){
}
}
/**
* Read the piece with pieceIndex from the file.
*/
public byte[] readPiece(int pieceIndex, int size) {
byte[] piece = new byte[size];
try {
long offSet = (pieceIndex - 1) * pieceSize;
fileAccess.seek(offSet);
fileAccess.read(piece, 0, size);
} catch (IOException ex) {
try {
fileAccess.close();
} catch (IOException ex1) {
}
}
return piece;
}
/***
* Write piece to the file.
**/
public boolean writePiece(int pieceIndex, byte[] piece, int size) {
try {
long offSet = (pieceIndex - 1) * pieceSize;
fileAccess.seek(offSet);
fileAccess.write(piece, 0, size);
return true;
} catch (IOException ex) {
try {
fileAccess.close();
} catch (IOException ex1) {
}
}
return false;
}
}