1 package org.sim0mq.message;
2
3 import org.djunits.unit.Unit;
4 import org.sim0mq.Sim0MQException;
5 import org.sim0mq.message.types.Sim0MQDisplayType;
6 import org.sim0mq.message.types.Sim0MQUnitType;
7 import org.sim0mq.message.types.TypesUtil;
8 import org.sim0mq.message.util.EndianUtil;
9
10
11
12
13
14
15
16
17
18
19
20 class MessageBuffer
21 {
22
23 private byte[] message;
24
25
26 private int pointer;
27
28
29
30
31
32 MessageBuffer(final byte[] message)
33 {
34 this.message = message;
35 this.pointer = 0;
36 }
37
38
39
40
41 public int getPointer()
42 {
43 return this.pointer;
44 }
45
46
47
48
49 public boolean hasMore()
50 {
51 return this.pointer < this.message.length;
52 }
53
54
55
56
57 public byte getByte()
58 {
59 return this.message[this.pointer++];
60 }
61
62
63
64
65 public short getShort()
66 {
67 short value = EndianUtil.decodeShort(this.message, this.pointer);
68 this.pointer += 2;
69 return value;
70 }
71
72
73
74
75 public int getInt()
76 {
77 int value = EndianUtil.decodeInt(this.message, this.pointer);
78 this.pointer += 4;
79 return value;
80 }
81
82
83
84
85 public long getLong()
86 {
87 long value = EndianUtil.decodeLong(this.message, this.pointer);
88 this.pointer += 8;
89 return value;
90 }
91
92
93
94
95 public float getFloat()
96 {
97 float value = EndianUtil.decodeFloat(this.message, this.pointer);
98 this.pointer += 4;
99 return value;
100 }
101
102
103
104
105 public double getDouble()
106 {
107 double value = EndianUtil.decodeDouble(this.message, this.pointer);
108 this.pointer += 8;
109 return value;
110 }
111
112
113
114
115 public boolean getBoolean()
116 {
117 return getByte() == 0 ? false : true;
118 }
119
120
121
122
123 public char getCharUTF8()
124 {
125 return (char) getByte();
126 }
127
128
129
130
131 public char getCharUTF16()
132 {
133 char value = EndianUtil.decodeChar(this.message, this.pointer);
134 this.pointer += 2;
135 return value;
136 }
137
138
139
140
141 public String getStringUTF8()
142 {
143 String s = EndianUtil.decodeUTF8String(this.message, this.pointer);
144 this.pointer += 4 + s.length();
145 return s;
146 }
147
148
149
150
151 public String getStringUTF16()
152 {
153 String s = EndianUtil.decodeUTF16String(this.message, this.pointer);
154 this.pointer += 4 + 2 * s.length();
155 return s;
156 }
157
158
159
160
161 public Unit<? extends Unit<?>> getUnit()
162 {
163 Unit<? extends Unit<?>> unit;
164 Sim0MQUnitType unitType = Sim0MQUnitType.getUnitType(getByte());
165 if (unitType.getCode() == 100)
166 {
167 unit = decodeMoneyUnit();
168 }
169 else if (unitType.getCode() >= 101 && unitType.getCode() <= 106)
170 {
171 unit = decodeMoneyPerUnit(unitType);
172 }
173 else
174 {
175 Sim0MQDisplayType displayType = Sim0MQDisplayType.getDisplayType(unitType, getByte());
176 unit = displayType.getDjunitsType();
177 }
178 return unit;
179 }
180
181
182
183
184
185 private Unit<? extends Unit<?>> decodeMoneyUnit()
186 {
187 short moneyCode = getShort();
188 Sim0MQDisplayType displayType = Sim0MQDisplayType.getDisplayType(Sim0MQUnitType.MONEY, moneyCode);
189 return displayType.getDjunitsType();
190 }
191
192
193
194
195
196
197 @SuppressWarnings("checkstyle:needbraces")
198 private Unit<? extends Unit<?>> decodeMoneyPerUnit(final Sim0MQUnitType unitType)
199 {
200 short moneyCode = getShort();
201 Sim0MQDisplayType moneyDisplayType = Sim0MQDisplayType.getDisplayType(Sim0MQUnitType.MONEY, moneyCode);
202 byte perCode = getByte();
203 Sim0MQDisplayType perDisplayType;
204 if (unitType.getCode() == 101)
205 perDisplayType = Sim0MQDisplayType.getDisplayType(Sim0MQUnitType.AREA, perCode);
206 else if (unitType.getCode() == 102)
207 perDisplayType = Sim0MQDisplayType.getDisplayType(Sim0MQUnitType.ENERGY, perCode);
208 else if (unitType.getCode() == 103)
209 perDisplayType = Sim0MQDisplayType.getDisplayType(Sim0MQUnitType.LENGTH, perCode);
210 else if (unitType.getCode() == 104)
211 perDisplayType = Sim0MQDisplayType.getDisplayType(Sim0MQUnitType.MASS, perCode);
212 else if (unitType.getCode() == 105)
213 perDisplayType = Sim0MQDisplayType.getDisplayType(Sim0MQUnitType.DURATION, perCode);
214 else if (unitType.getCode() == 106)
215 perDisplayType = Sim0MQDisplayType.getDisplayType(Sim0MQUnitType.VOLUME, perCode);
216 else
217 throw new RuntimeException(new Sim0MQException("Unknown MoneyPerUnit type with code " + unitType.getCode()));
218 return TypesUtil.moneyPerUnitType(moneyDisplayType, perDisplayType);
219 }
220
221 }