sdis1819-t7g02 / service / Chunk.java @ 1
History | View | Annotate | Download (2.57 KB)
1 |
package service; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
|
5 |
public class Chunk implements java.io.Serializable { |
6 |
|
7 |
private int chunkNr; |
8 |
private String fileID; |
9 |
private String filePathName = null; |
10 |
private int size; |
11 |
|
12 |
private int actual_replication_degree = 0; |
13 |
private int desired_replication_degree; |
14 |
|
15 |
private byte[] chunkContent; |
16 |
|
17 |
// each chunk is identified by the pair <fileID, chunkNo>
|
18 |
public Chunk(int n, byte[] c, String s, int desiredRepDegree) throws Exception |
19 |
{ |
20 |
this.chunkNr = n;
|
21 |
this.chunkContent = getFiltredData(c);
|
22 |
|
23 |
if(chunkContent.length > Constants.MAX_CHUNK_SIZE) {
|
24 |
throw new Exception("Invalid chunk size -> " + chunkContent.length); |
25 |
} |
26 |
|
27 |
this.size = chunkContent.length;
|
28 |
|
29 |
desired_replication_degree = desiredRepDegree; |
30 |
|
31 |
fileID = s; |
32 |
} |
33 |
|
34 |
private byte[] getFiltredData(byte[] c) { |
35 |
ArrayList<Byte> aux = new ArrayList<Byte>(); |
36 |
boolean toAdd = false; |
37 |
|
38 |
for(byte b : c) { |
39 |
aux.add(b); |
40 |
} |
41 |
|
42 |
byte[] toReturn = new byte[aux.size()]; |
43 |
|
44 |
for(int i = aux.size() - 1, k = 0; i >= 0 ; i--) { |
45 |
if(aux.get(i) == 0) { |
46 |
if(!toAdd) {
|
47 |
continue;
|
48 |
} |
49 |
else {
|
50 |
toReturn[k] = aux.get(i); |
51 |
k++; |
52 |
} |
53 |
} |
54 |
else {
|
55 |
toReturn[k] = aux.get(i); |
56 |
toAdd = true;
|
57 |
k++; |
58 |
} |
59 |
} |
60 |
|
61 |
return toReturn;
|
62 |
} |
63 |
|
64 |
public int getReplicationDegree() { |
65 |
return desired_replication_degree;
|
66 |
} |
67 |
|
68 |
public void setActualReplicationDegree(int rd) |
69 |
{ |
70 |
this.actual_replication_degree = rd;
|
71 |
} |
72 |
|
73 |
|
74 |
public String getChunkNumber() |
75 |
{ |
76 |
return Integer.toString(this.chunkNr); |
77 |
} |
78 |
|
79 |
|
80 |
public String getChunkId() |
81 |
{ |
82 |
return this.fileID; |
83 |
} |
84 |
|
85 |
|
86 |
public int getChunkSize() |
87 |
{ |
88 |
return this.size; |
89 |
} |
90 |
|
91 |
|
92 |
public int getActualReplicationDegree() |
93 |
{ |
94 |
return this.actual_replication_degree; |
95 |
} |
96 |
|
97 |
|
98 |
public byte[] getChunkContent() |
99 |
{ |
100 |
return this.chunkContent; |
101 |
} |
102 |
|
103 |
public int compareTo(Chunk c) |
104 |
{ |
105 |
return(this.chunkNr - c.chunkNr); |
106 |
} |
107 |
|
108 |
@Override
|
109 |
public boolean equals(Object obj) { |
110 |
if (this == obj) { |
111 |
return true; |
112 |
} |
113 |
if (obj == null) { |
114 |
return false; |
115 |
} |
116 |
if (!(obj instanceof Chunk)) { |
117 |
return false; |
118 |
} |
119 |
Chunk other = (Chunk) obj; |
120 |
if (chunkNr != other.chunkNr) {
|
121 |
return false; |
122 |
} |
123 |
if (fileID == null) { |
124 |
if (other.fileID != null) { |
125 |
return false; |
126 |
} |
127 |
} else if (!fileID.equals(other.fileID)) { |
128 |
return false; |
129 |
} |
130 |
return true; |
131 |
} |
132 |
|
133 |
public String getFilePathName() { |
134 |
return filePathName;
|
135 |
} |
136 |
|
137 |
public void setFilePathName(String filePathName) { |
138 |
this.filePathName = filePathName;
|
139 |
} |
140 |
} |