-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileManager.java
110 lines (92 loc) · 2.91 KB
/
FileManager.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.io.*;
//This class is used to deal with file such as writing content to specific piece and read specific piece.
public class FileManager {
private int ID;
private File myFile;
private int FileSize;
private int PieceSize;
private File myDirectory;
public FileManager(int id, String filename, int SizeofFile, int SizeofPiece) throws FileNotFoundException {
this.setID(id);//peer id
this.FileSize = SizeofFile;
this.PieceSize = SizeofPiece;
createDirectory("peer_" + id);
this.myFile = new File("peer_" + id + "/" + filename);
}
public boolean createFile() throws IOException {
if (myFile.exists()) {
return false;
}
if (!myFile.createNewFile()) {
return false;
}
FileOutputStream fos = new FileOutputStream(myFile);
byte[] tempBuffer = new byte[PieceSize];
for (int i = PieceSize; i < FileSize; i += PieceSize) {
fos.write(tempBuffer);
}
int remainingBytes = FileSize % PieceSize;
if (remainingBytes > 0) {
byte[] remainingtempBuffer = new byte[remainingBytes];
fos.write(remainingtempBuffer);
}
fos.close();
return true;
}
/**
* given a pieceID returns the corresponding chunk
* data should be already present in file else request message would not come
*/
public byte[] readPiece(int pieceID) throws IOException {
RandomAccessFile read = new RandomAccessFile(myFile, "r");
read.seek(pieceID * PieceSize);
byte[] x;
int remainingBytes = this.FileSize - pieceID * PieceSize;
if (remainingBytes < PieceSize) {
x = new byte[remainingBytes];
} else {
x = new byte[PieceSize];
}
read.read(x);
read.close();
return x;
}
public void writePiece(int pieceID, byte[] pieceData) throws IOException {
RandomAccessFile read = new RandomAccessFile(this.myFile, "rws");
int length = pieceData.length;
read.seek(pieceID * PieceSize);
read.write(pieceData, 0, length);
read.close();
}
public File getMyFile() {
return this.myFile;
}
public void setMyFile(File file) {
myFile = file;
}
public int getFileSize() {
return this.FileSize;
}
public void setFileSize(int fileSize) {
this.FileSize = fileSize;
}
public int getPieceSize() {
return PieceSize;
}
public void setPieceSize(int pieceSize) {
this.PieceSize = pieceSize;
}
public boolean createDirectory(String DirectoryPath) {
myDirectory = new File(DirectoryPath);
if (myDirectory.exists()) {
return true;
}
return myDirectory.mkdir();
}
public int getID() {
return this.ID;
}
public void setID(int iD) {
ID = iD;
}
}