sdis1819-t7g02 / channels / Message.java
History | View | Annotate | Download (2.26 KB)
1 | 1 | up20150366 | package channels; |
---|---|---|---|
2 | |||
3 | import java.util.ArrayList; |
||
4 | |||
5 | import service.Constants; |
||
6 | |||
7 | public class Message { |
||
8 | |||
9 | private MessageHeader h;
|
||
10 | private byte[] chunkData; |
||
11 | int totalLength = 0; |
||
12 | |||
13 | public Message(byte[] msg) { |
||
14 | int separation = headerLength(msg);
|
||
15 | |||
16 | if(separation < 0) { |
||
17 | throw new IllegalArgumentException("Couldn't find separation."); |
||
18 | } |
||
19 | |||
20 | h = new MessageHeader(msg, separation);
|
||
21 | |||
22 | if(!separateChunkData(msg, separation)) {
|
||
23 | throw new IllegalArgumentException("Invalid message, chuk max size exceeded"); |
||
24 | } |
||
25 | } |
||
26 | |||
27 | public Message(byte[] msg, int length) { |
||
28 | totalLength = length; |
||
29 | int separation = headerLength(msg);
|
||
30 | |||
31 | if(separation < 0) { |
||
32 | throw new IllegalArgumentException("Couldn't find separation."); |
||
33 | } |
||
34 | |||
35 | h = new MessageHeader(msg, separation);
|
||
36 | |||
37 | if(!separateChunkData(msg, separation)) {
|
||
38 | throw new IllegalArgumentException("Invalid message, chuk max size exceeded"); |
||
39 | } |
||
40 | } |
||
41 | |||
42 | public void setTotalLength(int l) { |
||
43 | totalLength = l; |
||
44 | } |
||
45 | |||
46 | public Message(MessageHeader h, byte[] chunk) { |
||
47 | if(chunk.length > Constants.MAX_CHUNK_SIZE) {
|
||
48 | throw new IllegalArgumentException("Invalid message, chuk max size exceeded"); |
||
49 | } |
||
50 | |||
51 | this.h = h;
|
||
52 | |||
53 | chunkData = chunk; |
||
54 | } |
||
55 | |||
56 | private int headerLength(byte[] msg) { |
||
57 | int i = 0; |
||
58 | |||
59 | for(i = 0; i < msg.length-4; i++) { |
||
60 | if(msg[i] == Constants.CR && msg[i+1] == Constants.LF && msg[i+2] == Constants.CR && msg[i+3] == Constants.LF) { |
||
61 | return i;
|
||
62 | } |
||
63 | } |
||
64 | |||
65 | return -1; |
||
66 | } |
||
67 | |||
68 | private boolean separateChunkData(byte[] msg, int separation) { |
||
69 | |||
70 | int chunkLength = totalLength <= 0 ? msg.length - separation - 4: totalLength - separation - 4; |
||
71 | |||
72 | if(chunkLength > Constants.MAX_CHUNK_SIZE) {
|
||
73 | System.out.println("looks to big"); |
||
74 | return false; |
||
75 | } |
||
76 | |||
77 | chunkData = new byte[chunkLength]; |
||
78 | |||
79 | System.arraycopy(msg, separation + 4, chunkData, 0, chunkLength); |
||
80 | |||
81 | return true; |
||
82 | } |
||
83 | |||
84 | public MessageHeader getHeader() {
|
||
85 | return h;
|
||
86 | } |
||
87 | |||
88 | public byte[] getChunkContent() { |
||
89 | return chunkData;
|
||
90 | } |
||
91 | |||
92 | public byte[] toBytes() { |
||
93 | ArrayList<Byte> messageToBeSent = h.toBytes(); |
||
94 | byte[] toReturn = new byte[messageToBeSent.size() + chunkData.length]; |
||
95 | int index = 0; |
||
96 | |||
97 | for(Byte b : messageToBeSent) { |
||
98 | toReturn[index] = b; |
||
99 | index++; |
||
100 | } |
||
101 | for(Byte b : chunkData) { |
||
102 | toReturn[index] = b; |
||
103 | index++; |
||
104 | } |
||
105 | |||
106 | return toReturn;
|
||
107 | } |
||
108 | |||
109 | } |