-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileHandler.java
130 lines (112 loc) · 4.31 KB
/
FileHandler.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import java.net.*;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import java.util.*;
import java.io.*;
import java.nio.file.*;
public class FileHandler {
private final int BUF_SIZE = 64*1024;
public FileHandler () {
}
public Metadata readFileToBytes(File file) throws IOException {
FileInputStream fis = new FileInputStream(file.getPath());
Metadata mdata = new Metadata(file.toPath().getFileName().toString(), fis.getChannel().size());
byte[] buf = new byte[BUF_SIZE];
int read = 0;
while ((read = fis.read(buf)) > 0) {
mdata.addChunk(new Data(buf));
}
return mdata;
}
public void restoreFile(LinkedList<Data> dataList, String filename) throws FileNotFoundException, IOException {
FileOutputStream stream = new FileOutputStream(filename);
try {
for (Data d : dataList) {
if (d.getData() != null) {
stream.write(d.getData());
stream.flush();
}
}
stream.close();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
public void serializeMetadata(Metadata o) throws IOException, ClassNotFoundException {
FileOutputStream fos = new FileOutputStream("metadata/" + o.getFilename() + ".sdi");
ObjectOutputStream oos = new ObjectOutputStream(fos);
o.eraseChunks();
oos.writeObject(o);
oos.flush();
oos.close();
fos.close();
}
public void serializeData(Data o) throws IOException, ClassNotFoundException {
FileOutputStream fos = new FileOutputStream("chunks/" + Long.toString(o.getHashChunk()) + ".chunk");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(o);
oos.flush();
oos.close();
fos.close();
}
// DEPRECATED
// recoverChunks has been deprecated since our last version
// and will be deleted in the future
// REASON: Stopped storing all chunks in memory.
// Still wondering why we did that.
public Hashtable <Long, Data> recoverChunks() throws IOException, FileNotFoundException, ClassNotFoundException {
LinkedList <String> files = sdiFiles("chunks", ".chunk");
Hashtable <Long, Data> aux = new Hashtable<>();
for (String fname : files) {
FileInputStream fis = new FileInputStream(fname);
ObjectInputStream ois = new ObjectInputStream(fis);
Data tmp = (Data) ois.readObject();
aux.put(tmp.getHashChunk(), tmp);
fis.close();
ois.close();
}
return aux;
}
public Data recoverSingleChunk(long hash_chunk) throws IOException, FileNotFoundException, ClassNotFoundException {
String fname = "chunks/" + Long.toString(hash_chunk) + ".chunk";
File file = new File(fname);
if(!file.exists()) {
System.out.println("Arquivo nao existe.");
return null;
}
FileInputStream fis = new FileInputStream(fname);
ObjectInputStream ois = new ObjectInputStream(fis);
Data ret = (Data) ois.readObject();
fis.close();
ois.close();
return ret;
}
public Hashtable <String,Metadata> recoverMetadata() throws IOException, FileNotFoundException, ClassNotFoundException {
LinkedList<String> files = sdiFiles("metadata", ".sdi");
Hashtable <String,Metadata> aux = new Hashtable<String,Metadata>();
for (String fname : files) {
FileInputStream fis = new FileInputStream(fname);
ObjectInputStream ois = new ObjectInputStream(fis);
Metadata tmp = (Metadata) ois.readObject();
aux.put(tmp.getFilename(), tmp);
fis.close();
ois.close();
}
return aux;
}
public LinkedList<String> sdiFiles(String directory, String r) {
LinkedList<String> files = new LinkedList<String>();
File dir = new File(directory);
for (File file : dir.listFiles()) {
if (file.getName().endsWith(r)) {
files.add(directory + "/" + file.getName());
}
}
return files;
}
public Boolean verifyMetadata() throws IOException, FileNotFoundException {
File file = new File("metadata.dat");
if(file.exists()) return true;
return false;
}
}