root / chunk / ChunkFile.java @ 10
History | View | Annotate | Download (3.73 KB)
1 | 10 | up20160473 | package chunk; |
---|---|---|---|
2 | |||
3 | |||
4 | |||
5 | import java.io.BufferedInputStream; |
||
6 | import java.io.File; |
||
7 | import java.io.FileInputStream; |
||
8 | import java.io.IOException; |
||
9 | import java.io.Serializable; |
||
10 | import java.security.MessageDigest; |
||
11 | import java.util.HashMap; |
||
12 | import java.util.Arrays; |
||
13 | import java.math.BigInteger; |
||
14 | import java.security.NoSuchAlgorithmException; |
||
15 | |||
16 | |||
17 | |||
18 | public class ChunkFile implements Serializable { |
||
19 | private HashMap<Integer, Chunk> chunks; |
||
20 | private String fileId; |
||
21 | private String filePath; |
||
22 | private File file; |
||
23 | private int replicationDegree; |
||
24 | |||
25 | public ChunkFile(String filePath, int replicationDegree) { |
||
26 | this.chunks = new HashMap<>(); |
||
27 | this.file = new File(filePath); |
||
28 | this.replicationDegree = replicationDegree;
|
||
29 | generateChunks(); |
||
30 | generateFileId(); |
||
31 | // create FILE AND STORE IT IN MEMORY;
|
||
32 | } |
||
33 | |||
34 | public String getFileId() { |
||
35 | return this.fileId; |
||
36 | } |
||
37 | |||
38 | public int getReplicationDegree() { |
||
39 | return this.replicationDegree; |
||
40 | } |
||
41 | |||
42 | public String getPath() { |
||
43 | return this.file.toPath().toString(); |
||
44 | } |
||
45 | |||
46 | public Chunk getChunk(int position) { |
||
47 | return chunks.get(position);
|
||
48 | } |
||
49 | |||
50 | public void deleteChunk(int position) { |
||
51 | chunks.remove(position); |
||
52 | } |
||
53 | |||
54 | public HashMap<Integer,Chunk> getChunks(){ |
||
55 | return this.chunks; |
||
56 | } |
||
57 | |||
58 | public void generateFileId() { |
||
59 | String filename = this.file.getName(); |
||
60 | String path = String.valueOf(this.file.toPath()); |
||
61 | String owner = this.file.getParent(); |
||
62 | String modifiedDate = String.valueOf(this.file.lastModified()); |
||
63 | String totalSize = String.valueOf(this.file.getTotalSpace()); |
||
64 | String fileID = filename + '-' + path + '-' + owner + '-' + modifiedDate + '-' + totalSize; |
||
65 | this.fileId = hashSha256(fileID);
|
||
66 | } |
||
67 | |||
68 | public void generateChunks() { |
||
69 | int chunkNr = 0; |
||
70 | |||
71 | int sizeOfChunks = Global.MAX_CHUNK_SIZE;
|
||
72 | byte[] buffer = new byte[sizeOfChunks]; |
||
73 | |||
74 | try (FileInputStream fis = new FileInputStream(this.file); |
||
75 | BufferedInputStream bis = new BufferedInputStream(fis)) { |
||
76 | |||
77 | int bytesAmount;
|
||
78 | while ((bytesAmount = bis.read(buffer)) > 0) { |
||
79 | byte[] body = Arrays.copyOf(buffer, bytesAmount); |
||
80 | Chunk chunk = new Chunk(chunkNr, body, bytesAmount);
|
||
81 | this.chunks.put(chunkNr,chunk);
|
||
82 | chunkNr++; |
||
83 | buffer = new byte[sizeOfChunks]; |
||
84 | } |
||
85 | |||
86 | if (this.file.length() % Global.MAX_CHUNK_SIZE == 0) { |
||
87 | Chunk chunk = new Chunk(chunkNr, null, 0); |
||
88 | this.chunks.put(chunkNr,chunk);
|
||
89 | } |
||
90 | |||
91 | } catch (IOException e) { |
||
92 | e.printStackTrace(); |
||
93 | } |
||
94 | } |
||
95 | public String hashSha256(String unHashed) { |
||
96 | try {
|
||
97 | |||
98 | // Static getInstance method is called with hashing SHA
|
||
99 | MessageDigest md = MessageDigest.getInstance("SHA-256"); |
||
100 | |||
101 | // digest() method called
|
||
102 | // to calculate message digest of an input
|
||
103 | // and return array of byte
|
||
104 | byte[] messageDigest = md.digest(unHashed.getBytes()); |
||
105 | |||
106 | // Convert byte array into signum representation
|
||
107 | BigInteger no = new BigInteger(1, messageDigest); |
||
108 | |||
109 | // Convert message digest into hex value
|
||
110 | String hashtext = no.toString(16); |
||
111 | |||
112 | while (hashtext.length() < 32) { |
||
113 | hashtext = "0" + hashtext;
|
||
114 | } |
||
115 | return hashtext;
|
||
116 | } |
||
117 | |||
118 | // For specifying wrong message digest algorithms
|
||
119 | catch (NoSuchAlgorithmException e) { |
||
120 | System.out.println("Exception thrown" |
||
121 | + " for incorrect algorithm: " + e);
|
||
122 | |||
123 | return null; |
||
124 | } |
||
125 | } |
||
126 | } |