1 | package de.uka.ipd.sdq.sensorframework.dao.file.entities; |
2 | |
3 | |
4 | import de.uka.ipd.sdq.sensorframework.entities.ScalabilitySensor; |
5 | import de.uka.ipd.sdq.sensorframework.storage.lists.ISerialiser; |
6 | |
7 | /**TODO document me!!! |
8 | * Raw type... |
9 | * @author Paul-Remis Beauvais |
10 | * |
11 | */ |
12 | public class ParameterValueSerialiser implements ISerialiser { |
13 | |
14 | //HashMap<Long, State> hashMap = new HashMap<Long, State>(); |
15 | private int nbParameters; |
16 | |
17 | public ParameterValueSerialiser(ScalabilitySensor sSensor){ |
18 | nbParameters = 1; |
19 | /*for (State s : stateSensor.getSensorStates()) { |
20 | hashMap.put(s.getStateID(),s); |
21 | }*/ |
22 | } |
23 | |
24 | public Object[] deserialise(byte[] bytes) { |
25 | if (bytes.length < 4) { |
26 | return new Double[0][0]; |
27 | } |
28 | int blockPos = 0; |
29 | int nb = 0; |
30 | for (int i = 3; i >=0; i--) { |
31 | nb = nb << 4; |
32 | nb |= bytes[blockPos+i] < 0 ? 256 + bytes[blockPos+i] : bytes[blockPos+i]; |
33 | } |
34 | blockPos += 4; |
35 | nbParameters = nb; |
36 | Double[][] values = new Double[(int)((bytes.length - 4)/ getElementLength())][nbParameters]; |
37 | |
38 | |
39 | |
40 | for (int j = 0; j<values.length; j++){ |
41 | for (int k = 0; k < nbParameters; k++) { |
42 | long l = 0; |
43 | for (int i = 7; i >=0; i--) { |
44 | l = l << 8; |
45 | l |= bytes[blockPos+i] < 0 ? 256 + bytes[blockPos+i] : bytes[blockPos+i]; |
46 | } |
47 | blockPos += 8; |
48 | values[j][k] = Double.valueOf(l);//hashMap.get(l); |
49 | } |
50 | |
51 | //if (states[j] == null) |
52 | // throw new RuntimeException("State could not be deserialized as it returned null values for states."); |
53 | } |
54 | |
55 | return values; |
56 | } |
57 | |
58 | public long getElementLength() { |
59 | return 8*nbParameters; |
60 | } |
61 | |
62 | public byte[] serialise(Object[] objects, int count) { |
63 | |
64 | if (count>0) { |
65 | nbParameters = ((Double[])objects[0]).length; |
66 | } |
67 | byte[] block = new byte[(int)(count*getElementLength()+4)];; |
68 | int blockPos = 0; |
69 | |
70 | int nb = nbParameters; |
71 | for (int i = 0; i < 4; i++) { |
72 | block[blockPos++] = (byte)(nb & 0xff); |
73 | nb = nb >> 4; |
74 | } |
75 | |
76 | for (int j = 0; j < count; j++){ |
77 | Double[] serie = (Double[])objects[j]; |
78 | |
79 | |
80 | for (int k = 0; k < nbParameters; k++) { |
81 | |
82 | |
83 | |
84 | long l = ((Double)serie[k]).longValue(); |
85 | for (int i = 0; i < 8; i++) { |
86 | block[blockPos++] = (byte)(l & 0xff); |
87 | l = l >> 8; |
88 | } |
89 | } |
90 | } |
91 | |
92 | |
93 | return block; |
94 | } |
95 | |
96 | |
97 | } |