-
Notifications
You must be signed in to change notification settings - Fork 0
/
PieceMessage.java
48 lines (39 loc) · 1.24 KB
/
PieceMessage.java
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
import java.io.ByteArrayOutputStream;
import java.io.IOException;
// This calss is to construct the peice message.
public class PieceMessage extends ActualMessage {
private int index;
private byte[] payload;
public PieceMessage(int index, byte[] payload) throws IOException {
super(5 + payload.length, MessageType.piece);
this.index = index;
this.payload = payload;
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write(intToByteArray(this.length));
out.write(this.msType.getMessageType());
out.write(intToByteArray(this.index));
out.write(this.payload);
this.msBytes = out.toByteArray();
out.close();
}
public int getIndex() {
return this.index;
}
public void setIndex(int index) {
this.index = index;
}
public byte[] getContent() {
return this.payload;
}
public void setContent(byte[] content) {
this.payload = payload;
}
public static byte[] intToByteArray(int a) {
return new byte[] {
(byte) ((a >> 24) & 0xFF),
(byte) ((a >> 16) & 0xFF),
(byte) ((a >> 8) & 0xFF),
(byte) (a & 0xFF)
};
}
}