Revision 10
chunk/Chunk.java | ||
---|---|---|
1 |
package chunk; |
|
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
import java.io.Serializable; |
|
5 |
|
|
6 |
public class Chunk implements Serializable{ |
|
7 |
private String fileId; //Hash256 |
|
8 |
private int chunkNum; //Starts with 0 |
|
9 |
private int repDeg; //Desired Rep degree |
|
10 |
private int currentRepDeg = 0; //Actual Rep degree |
|
11 |
private int chunkSize; //MAX 64000kbytes |
|
12 |
private byte[] info; // chunk itself |
|
13 |
|
|
14 |
public Chunk(int chunkNum, byte[] content, int chunkSize) throws IOException { //BACKUP CHUNKS |
|
15 |
|
|
16 |
this.chunkNum = chunkNum; |
|
17 |
this.info = content; |
|
18 |
this.chunkSize = chunkSize; |
|
19 |
} |
|
20 |
|
|
21 |
public Chunk(int chunkNum, String fileId , int repDeg, int chunkSize) throws IOException { //BACKUP CHUNKS |
|
22 |
|
|
23 |
this.chunkNum = chunkNum; |
|
24 |
this.fileId = fileId; |
|
25 |
this.repDeg = repDeg; |
|
26 |
this.chunkSize = chunkSize; |
|
27 |
} |
|
28 |
|
|
29 |
public String getfileId() { |
|
30 |
return fileId; |
|
31 |
} |
|
32 |
|
|
33 |
public int getNum() { |
|
34 |
return chunkNum; |
|
35 |
} |
|
36 |
|
|
37 |
|
|
38 |
public int getRepDegree() { |
|
39 |
return repDeg; |
|
40 |
} |
|
41 |
|
|
42 |
public int getActualRepDegree() { |
|
43 |
return currentRepDeg; |
|
44 |
} |
|
45 |
|
|
46 |
public int getchunkSize() { |
|
47 |
return chunkSize; |
|
48 |
} |
|
49 |
|
|
50 |
public byte[] getContent(){ |
|
51 |
return info; |
|
52 |
} |
|
53 |
|
|
54 |
public void setCurrentRepDegree(int degree){ |
|
55 |
this.currentRepDeg = degree; |
|
56 |
} |
|
57 |
|
|
58 |
public void setRepDegree(int degree){ |
|
59 |
this.repDeg = degree; |
|
60 |
} |
|
61 |
|
|
62 |
public void setFileId(String id){ |
|
63 |
this.fileId = id; |
|
64 |
} |
|
65 |
|
|
66 |
public void setContent(byte[] content){ |
|
67 |
this.info = content; |
|
68 |
} |
|
69 |
} |
chunk/ChunkFile.java | ||
---|---|---|
1 |
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 |
} |
chunk/Global.java | ||
---|---|---|
1 |
package chunk; |
|
2 |
public class Global { |
|
3 |
public static int MAX_CHUNK_SIZE = 64000; |
|
4 |
public static int MAX_STORAGE_SIZE = 64000000; |
|
5 |
/// |
|
6 |
} |
Also available in: Unified diff