root / RestoreFile.java @ 1
History | View | Annotate | Download (1.37 KB)
1 |
import java.io.File; |
---|---|
2 |
import java.io.FileOutputStream; |
3 |
import java.io.IOException; |
4 |
import java.util.ArrayList; |
5 |
|
6 |
/**
|
7 |
* BackupFile
|
8 |
*/
|
9 |
public class RestoreFile { |
10 |
private File file; |
11 |
private String fileId; |
12 |
private long numberOfChunks; |
13 |
private ArrayList<byte[]> chunks = new ArrayList<>(); |
14 |
|
15 |
public RestoreFile(File file, String fileId, long numberOfChunks) { |
16 |
this.file = file;
|
17 |
this.fileId = fileId;
|
18 |
this.numberOfChunks = numberOfChunks;
|
19 |
} |
20 |
|
21 |
public boolean restoreFile(File file) throws IOException { |
22 |
if (this.numberOfChunks != this.chunks.size()) { |
23 |
System.out.println("restoreFile(): numberOfChunks = "+this.numberOfChunks+" chunks.size() = "+this.chunks.size()); |
24 |
return false; |
25 |
} |
26 |
|
27 |
FileOutputStream stream = new FileOutputStream(this.file); |
28 |
|
29 |
for (byte[] chunk : this.chunks) |
30 |
stream.write(chunk); |
31 |
|
32 |
stream.close(); |
33 |
return true; |
34 |
} |
35 |
|
36 |
/**
|
37 |
* @return the chunks
|
38 |
*/
|
39 |
public ArrayList<byte[]> getChunks() { |
40 |
return chunks;
|
41 |
} |
42 |
|
43 |
/**
|
44 |
* @return the fileId
|
45 |
*/
|
46 |
public String getFileId() { |
47 |
return fileId;
|
48 |
} |
49 |
|
50 |
public File getFile() { |
51 |
return this.file; |
52 |
} |
53 |
|
54 |
public boolean hasChunk(int i) { |
55 |
try {
|
56 |
this.chunks.get(i);
|
57 |
} catch (Exception e) { |
58 |
return false; |
59 |
} |
60 |
return true; |
61 |
} |
62 |
} |