|
8 | 8 | import net.sharksystem.utils.Log; |
9 | 9 |
|
10 | 10 | import java.io.*; |
| 11 | +import java.util.ArrayList; |
11 | 12 | import java.util.HashSet; |
| 13 | +import java.util.List; |
12 | 14 | import java.util.Set; |
13 | 15 |
|
14 | 16 | public class ASAPSerialization { |
@@ -254,29 +256,63 @@ public static void writeEncounterConnectionType(EncounterConnectionType connecti |
254 | 256 |
|
255 | 257 | public static void writeBooleanParameter(boolean value, OutputStream os) throws IOException { |
256 | 258 | if(value) ASAPSerialization.writeByteParameter((byte) 1, os); |
257 | | - ASAPSerialization.writeByteParameter((byte) 0, os); |
| 259 | + else ASAPSerialization.writeByteParameter((byte) 0, os); |
258 | 260 | } |
259 | 261 |
|
260 | 262 | public static boolean readBooleanParameter(InputStream is) throws IOException, ASAPException { |
261 | 263 | return ASAPSerialization.readByte(is) == 1; |
262 | 264 | } |
263 | 265 |
|
| 266 | + public static void writeASAPHop(ASAPHop asapHop, OutputStream os) throws IOException { |
| 267 | + ASAPSerialization.writeCharSequenceParameter(asapHop.sender(), os); |
| 268 | + ASAPSerialization.writeBooleanParameter(asapHop.verified(), os); |
| 269 | + ASAPSerialization.writeBooleanParameter(asapHop.encrypted(), os); |
| 270 | + ASAPSerialization.writeEncounterConnectionType(asapHop.getConnectionType(), os); |
| 271 | + } |
| 272 | + |
264 | 273 | public static byte[] asapHop2ByteArray(ASAPHop asapHop) throws IOException { |
265 | 274 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
266 | | - ASAPSerialization.writeCharSequenceParameter(asapHop.sender(), baos); |
267 | | - ASAPSerialization.writeBooleanParameter(asapHop.verified(), baos); |
268 | | - ASAPSerialization.writeBooleanParameter(asapHop.encrypted(), baos); |
269 | | - ASAPSerialization.writeEncounterConnectionType(asapHop.getConnectionType(), baos); |
| 275 | + writeASAPHop(asapHop, baos); |
270 | 276 | return baos.toByteArray(); |
271 | 277 | } |
272 | 278 |
|
| 279 | + public static ASAPHop readASAPHop(InputStream is) throws IOException, ASAPException { |
| 280 | + CharSequence sender = ASAPSerialization.readCharSequenceParameter(is); |
| 281 | + boolean verified = ASAPSerialization.readBooleanParameter(is); |
| 282 | + boolean encrypted = ASAPSerialization.readBooleanParameter(is); |
| 283 | + EncounterConnectionType connectionType = ASAPSerialization.readEncounterConnectionType(is); |
| 284 | + |
| 285 | + return new ASAPHopImpl(sender, verified, encrypted, connectionType); |
| 286 | + } |
| 287 | + |
273 | 288 | public static ASAPHop byteArray2ASAPHop(byte[] bytes) throws IOException, ASAPException { |
274 | 289 | ByteArrayInputStream bais = new ByteArrayInputStream(bytes); |
275 | | - return new ASAPHopImpl( |
276 | | - ASAPSerialization.readCharSequenceParameter(bais), |
277 | | - ASAPSerialization.readBooleanParameter(bais), |
278 | | - ASAPSerialization.readBooleanParameter(bais), |
279 | | - ASAPSerialization.readEncounterConnectionType(bais) |
280 | | - ); |
| 290 | + return readASAPHop(bais); |
| 291 | + } |
| 292 | + |
| 293 | + public static void writeASAPHopList(List<ASAPHop> asapHopList, OutputStream os) throws IOException { |
| 294 | + if(asapHopList == null || asapHopList.isEmpty()) { |
| 295 | + // no hops |
| 296 | + ASAPSerialization.writeIntegerParameter(0, os); |
| 297 | + return; |
| 298 | + } |
| 299 | + |
| 300 | + // write number of hops |
| 301 | + ASAPSerialization.writeIntegerParameter(asapHopList.size(), os); |
| 302 | + for(ASAPHop asapHop : asapHopList) { |
| 303 | + ASAPSerialization.writeASAPHop(asapHop, os); |
| 304 | + } |
| 305 | + } |
| 306 | + |
| 307 | + public static List<ASAPHop> readASAPHopList(InputStream is) throws IOException, ASAPException { |
| 308 | + List<ASAPHop> asapHopList = new ArrayList<>(); |
| 309 | + |
| 310 | + int number = ASAPSerialization.readIntegerParameter(is); |
| 311 | + while(number-- > 0) { |
| 312 | + asapHopList.add(ASAPSerialization.readASAPHop(is)); |
| 313 | + } |
| 314 | + |
| 315 | + return asapHopList; |
| 316 | + |
281 | 317 | } |
282 | 318 | } |
0 commit comments