1 | package de.uka.ipd.sdq.sensorframework.storage.lists; |
2 | |
3 | /** |
4 | * A serialiser to serialise doubles |
5 | * @author Steffen Becker |
6 | */ |
7 | public class DoubleSerialiser implements ISerialiser<Double> { |
8 | |
9 | public long getElementLength() { |
10 | return 8; |
11 | } |
12 | |
13 | public byte[] serialise(Object[] objects, int count){ |
14 | byte[] block = new byte[(int)(count*getElementLength())]; |
15 | int blockPos = 0; |
16 | for (int j = 0; j < count; j++){ |
17 | long l = Double.doubleToRawLongBits((Double)objects[j]); |
18 | for (int i = 0; i < 8; i++) { |
19 | block[blockPos++] = (byte)(l & 0xff); |
20 | l = l >> 8; |
21 | } |
22 | } |
23 | return block; |
24 | } |
25 | |
26 | public Double[] deserialise(byte[] bytes){ |
27 | Double[] doubles = new Double[(int)(bytes.length / getElementLength())]; |
28 | int blockPos = 0; |
29 | for (int j = 0; j<doubles.length; j++){ |
30 | long l = 0; |
31 | for (int i = 7; i >=0; i--) { |
32 | l = l << 8; |
33 | l |= bytes[blockPos+i] < 0 ? 256 + bytes[blockPos+i] : bytes[blockPos+i]; |
34 | } |
35 | blockPos += 8; |
36 | doubles[j] = Double.longBitsToDouble(l); |
37 | } |
38 | return doubles; |
39 | } |
40 | |
41 | } |