root / project / src / main / java / protocols / Backup.java @ 1
History | View | Annotate | Download (2.96 KB)
1 |
package main.java.protocols; |
---|---|
2 |
|
3 |
import main.java.file.*; |
4 |
import main.java.peer.Peer; |
5 |
|
6 |
|
7 |
import java.io.*; |
8 |
import java.util.Arrays; |
9 |
|
10 |
import static main.java.utils.Constants.*; |
11 |
import static main.java.utils.Utilities.*; |
12 |
|
13 |
public class Backup implements Runnable{ |
14 |
|
15 |
private File file; |
16 |
private int repDeg; |
17 |
private String encryptedID; |
18 |
private FileID fileID;
|
19 |
private int fileParts; |
20 |
//private static Message message;
|
21 |
|
22 |
|
23 |
public Backup(File file, int replicationDegree) { |
24 |
|
25 |
this.file = file;
|
26 |
this.repDeg = replicationDegree;
|
27 |
|
28 |
} |
29 |
|
30 |
/**
|
31 |
* @param file File to split into chunks and later backed up
|
32 |
* @return Number of chunks that the file was split into
|
33 |
* @throws IOException exception to be thrown in case of an invalid file.
|
34 |
*/
|
35 |
public int createChunks(File file) throws IOException { |
36 |
|
37 |
|
38 |
byte[] fileData = loadFileData(file); |
39 |
|
40 |
fileParts = fileData.length / CHUNK_MAX_SIZE; |
41 |
|
42 |
|
43 |
String fileName = file.getName();
|
44 |
|
45 |
ByteArrayInputStream streamBuffer = new ByteArrayInputStream(fileData); |
46 |
byte[] data = new byte[CHUNK_MAX_SIZE]; |
47 |
|
48 |
|
49 |
for(int i = 0; i <= fileParts; i++) { |
50 |
FileChunkID id = new FileChunkID(file.getName(), i);
|
51 |
|
52 |
|
53 |
byte[] chunkData; |
54 |
|
55 |
/*
|
56 |
Size of last chunk is always shorter than CHUNK_MAX_SIZE
|
57 |
If the file size is a multiple of CHUNK_MAX_SIZE, the last chunk has size 0.
|
58 |
*/
|
59 |
|
60 |
if(i == fileParts - 1 && file.length() % CHUNK_MAX_SIZE == 0) { |
61 |
chunkData = new byte[0]; |
62 |
} else {
|
63 |
int bytesRead = streamBuffer.read(data, 0, data.length); |
64 |
chunkData = Arrays.copyOfRange(data, 0, bytesRead); |
65 |
} |
66 |
|
67 |
|
68 |
|
69 |
fileID = new FileID(sha256(file.getName()), repDeg);
|
70 |
|
71 |
fileID.setNumChunks(fileParts + 1);
|
72 |
|
73 |
|
74 |
FileChunk chunk = new FileChunk(repDeg, i, fileID, chunkData);
|
75 |
|
76 |
Thread t = new Thread(new BackupChunk(chunk)); |
77 |
t.start(); |
78 |
|
79 |
try {
|
80 |
t.join(); |
81 |
} catch (InterruptedException e) { |
82 |
e.printStackTrace(); |
83 |
} |
84 |
} |
85 |
Peer.saveDBToDisk(); |
86 |
|
87 |
|
88 |
return fileParts;
|
89 |
|
90 |
} |
91 |
|
92 |
|
93 |
@Override
|
94 |
public void run() { |
95 |
//String fileIDString;
|
96 |
//fileIDString = file.getName() + file.getPath() + file.lastModified();
|
97 |
//encryptedID = sha256(fileIDString);
|
98 |
try {
|
99 |
createChunks(file); |
100 |
} catch (IOException e) { |
101 |
e.printStackTrace(); |
102 |
} |
103 |
|
104 |
|
105 |
|
106 |
Peer.getDb().insertFile(fileID); |
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
} |
114 |
|
115 |
public static byte[] loadFileData(File file) throws FileNotFoundException { |
116 |
FileInputStream inputStream = new FileInputStream(file); |
117 |
byte[] fileData = new byte[(int) file.length()]; |
118 |
|
119 |
try {
|
120 |
inputStream.read(fileData); |
121 |
inputStream.close(); |
122 |
|
123 |
} catch (IOException e) { |
124 |
e.printStackTrace(); |
125 |
} |
126 |
|
127 |
|
128 |
|
129 |
return fileData;
|
130 |
} |
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
} |