sdis1819-t7g02 / service / FileChunker.java @ 1
History | View | Annotate | Download (3.17 KB)
1 | 1 | up20150366 | package service; |
---|---|---|---|
2 | |||
3 | import java.io.File; |
||
4 | import java.io.IOException; |
||
5 | import java.nio.file.Files; |
||
6 | import java.nio.file.Path; |
||
7 | import java.nio.file.Paths; |
||
8 | import java.security.MessageDigest; |
||
9 | import java.text.SimpleDateFormat; |
||
10 | import java.util.ArrayList; |
||
11 | import java.util.Arrays; |
||
12 | |||
13 | /**
|
||
14 | * The porpous of this class is to load a file and split it to chunks so it they can be shared
|
||
15 | *
|
||
16 | */
|
||
17 | public class FileChunker implements java.io.Serializable { |
||
18 | |||
19 | private String name; |
||
20 | private String id; |
||
21 | private ArrayList<Chunk> fileChunks = new ArrayList<Chunk>(); |
||
22 | private String fileDate; |
||
23 | private Path fileLocation;
|
||
24 | private int replication_degree; |
||
25 | |||
26 | public FileChunker(String path, int replication_degree) throws IOException { |
||
27 | |||
28 | this.fileLocation = Paths.get(path);
|
||
29 | byte[] fileData = Files.readAllBytes(fileLocation); |
||
30 | int fileDataLength = fileData.length;
|
||
31 | byte[] aux; |
||
32 | int to = 0; |
||
33 | Chunk auxChunk = null;
|
||
34 | |||
35 | name = fileLocation.getFileName().toString(); |
||
36 | fileDate = new SimpleDateFormat("yyyy/MM/dd").format(new File(fileLocation.toString()).lastModified()); |
||
37 | id = SHA256(name + fileDate); |
||
38 | |||
39 | System.out.println(name + " has " + fileDataLength + " bytes."); |
||
40 | |||
41 | for(int chunkID = 0, from = 0; from < fileDataLength; chunkID++, from += (to-from)) { |
||
42 | |||
43 | to = fileDataLength - from > Constants.MAX_CHUNK_SIZE ? from + Constants.MAX_CHUNK_SIZE : fileData.length; |
||
44 | |||
45 | try {
|
||
46 | aux = Arrays.copyOfRange(fileData, from, to);
|
||
47 | |||
48 | //System.out.println("Chunk no " + chunkID + " with " + aux.length + " created with bytes from " + from + " to " + to);
|
||
49 | |||
50 | auxChunk = new Chunk(chunkID, aux, id, replication_degree);
|
||
51 | |||
52 | auxChunk.setFilePathName(name); |
||
53 | |||
54 | fileChunks.add(auxChunk); // para podermos distinguir entre diferentes versões
|
||
55 | |||
56 | } catch(Exception e) { |
||
57 | System.out.println("Invalid chunk size, might be corrupted"); |
||
58 | } |
||
59 | } |
||
60 | } |
||
61 | |||
62 | public FileChunker(String path) throws IOException |
||
63 | { |
||
64 | this.fileLocation = Paths.get(path);
|
||
65 | |||
66 | name = fileLocation.getFileName().toString(); |
||
67 | fileDate = new SimpleDateFormat("yyyy/MM/dd").format(new File(fileLocation.toString()).lastModified()); |
||
68 | id = SHA256(name + fileDate); |
||
69 | } |
||
70 | |||
71 | public ArrayList<Chunk> getChunks() { |
||
72 | return fileChunks;
|
||
73 | } |
||
74 | |||
75 | public String getFileID() { |
||
76 | return id;
|
||
77 | } |
||
78 | |||
79 | public String getFileName() |
||
80 | { |
||
81 | return this.name; |
||
82 | } |
||
83 | |||
84 | public int getFileSize() { |
||
85 | int size = 0; |
||
86 | |||
87 | for(Chunk c : fileChunks) {
|
||
88 | size += c.getChunkSize(); |
||
89 | } |
||
90 | |||
91 | return size;
|
||
92 | } |
||
93 | |||
94 | public Path getPath()
|
||
95 | { |
||
96 | return this.fileLocation; |
||
97 | } |
||
98 | |||
99 | public int getReplicationDegree() |
||
100 | { |
||
101 | return this.replication_degree; |
||
102 | } |
||
103 | |||
104 | private static String SHA256(String base) { |
||
105 | try {
|
||
106 | MessageDigest digest = MessageDigest.getInstance("SHA-256"); |
||
107 | byte[] hash = digest.digest(base.getBytes("UTF-8")); |
||
108 | StringBuffer hexString = new StringBuffer(); |
||
109 | |||
110 | for (byte aHash : hash) { |
||
111 | String hex = Integer.toHexString(0xff & aHash); |
||
112 | if (hex.length() == 1) { |
||
113 | hexString.append('0');
|
||
114 | } |
||
115 | hexString.append(hex); |
||
116 | } |
||
117 | |||
118 | return hexString.toString();
|
||
119 | } catch (Exception ex) { |
||
120 | throw new RuntimeException(ex); |
||
121 | } |
||
122 | } |
||
123 | } |