-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPieceClass.java
More file actions
72 lines (57 loc) · 1.75 KB
/
Copy pathPieceClass.java
File metadata and controls
72 lines (57 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
public class PieceClass {
int pieceIdx;
String myPeerId;
boolean isPrsnt;
byte[] pcData;
public PieceClass()
{
pieceIdx = -1;
myPeerId = null;
isPrsnt = false;
pcData = new byte[CommonCfg.pieceSize];
}
public static PieceClass initializePiece(byte []payload)
{ PieceClass pc = new PieceClass();
byte[] Idx = new byte[Constants.PIECE_INDEX_LENGTH];
System.arraycopy(payload, 0, Idx, 0, Constants.PIECE_INDEX_LENGTH);
pc.pieceIdx = ArrToIntConv(Idx);
pc.pcData = new byte[payload.length-Constants.PIECE_INDEX_LENGTH];
System.arraycopy(payload,Constants.PIECE_INDEX_LENGTH, pc.pcData, 0, payload.length-Constants.PIECE_INDEX_LENGTH);
return pc;
}
public static int ArrToIntConv(byte[] b) {
return ArrToIntConv(b, 0);
}
public static int ArrToIntConv(byte[] b, int offsetVal)
{
int output = 0;
for (int i = 0; i < 4; i++)
{
int shift = (4 - 1 - i) * 8;
output += (b[i + offsetVal] & 0x000000FF) << shift;
}
return output;
}
public boolean isPrsnt() {
return isPrsnt;
}
public void setPcData(byte[] pcData) {
this.pcData = pcData;
}
public void setPcIdx(int pieceIdx) {
this.pieceIdx = pieceIdx;
}
public int getPcIdx() { return pieceIdx; }
public String getFromPeerId() {
return myPeerId;
}
public byte[] getPcData() {
return pcData;
}
public void setMyPeerId(String myPeerId) {
this.myPeerId = myPeerId;
}
public void setIsPrsnt(boolean isPrsnt) {
this.isPrsnt = isPrsnt;
}
}