Fix a bug when deserializing Kryo-serialized values.
Move all deserializations to a single common method deserializeValue().
If a Kryo value is serialized by writeClassAndObject, then each deserialization
should use the corresponding readClassAndObject.
In commit 14c4a7671d3661290c315d2bd32f09464717a122 there were two leftover
readObject() calls (in deprecated methods).
Change-Id: Ia24b3fbd7f57cff3b18d4bbcfee10df46ad4c179
diff --git a/src/main/java/net/onrc/onos/core/datagrid/HazelcastEventChannel.java b/src/main/java/net/onrc/onos/core/datagrid/HazelcastEventChannel.java
index 5774af2..00ff7a8 100644
--- a/src/main/java/net/onrc/onos/core/datagrid/HazelcastEventChannel.java
+++ b/src/main/java/net/onrc/onos/core/datagrid/HazelcastEventChannel.java
@@ -192,6 +192,31 @@
}
/**
+ * Deserialize the value.
+ *
+ * @param kryo the Kryo instance to use for the deserialization.
+ * @param valueBytes the buffer with the serialized value.
+ * @return the deserialized value.
+ */
+ private V deserializeValue(Kryo kryo, byte[] valueBytes) {
+ V value;
+
+ //
+ // Decode the value
+ //
+ Input input = new Input(valueBytes);
+ Object objValue = kryo.readClassAndObject(input);
+ try {
+ value = typeV.cast(objValue);
+ } catch (ClassCastException e) {
+ log.error("Received notification value cast failed", e);
+ return null;
+ }
+
+ return value;
+ }
+
+ /**
* Remove an entry from the channel.
*
* @param key the key of the entry to remove.
@@ -232,12 +257,11 @@
return null;
}
- Kryo kryo = kryoFactory.newKryo();
//
// Decode the value
//
- Input input = new Input(valueBytes);
- V value = (V) kryo.readObject(input, typeV);
+ Kryo kryo = kryoFactory.newKryo();
+ V value = deserializeValue(kryo, valueBytes);
kryoFactory.deleteKryo(kryo);
return value;
@@ -266,8 +290,7 @@
//
// Decode the value
//
- Input input = new Input(valueBytes);
- V value = (V) kryo.readObject(input, typeV);
+ V value = deserializeValue(kryo, valueBytes);
allEntries.add(value);
}
kryoFactory.deleteKryo(kryo);
@@ -314,16 +337,7 @@
//
byte[] valueBytes = event.getValue();
Kryo kryo = kryoFactory.newKryo();
- Input input = new Input(valueBytes);
-
- Object objValue = kryo.readClassAndObject(input);
- V value;
- try {
- value = typeV.cast(objValue);
- } catch (ClassCastException e) {
- log.error("Received notification value cast failed", e);
- return;
- }
+ V value = deserializeValue(kryo, valueBytes);
//
// Deliver the notification
@@ -352,16 +366,7 @@
//
byte[] valueBytes = event.getValue();
Kryo kryo = kryoFactory.newKryo();
- Input input = new Input(valueBytes);
-
- Object objValue = kryo.readClassAndObject(input);
- V value;
- try {
- value = typeV.cast(objValue);
- } catch (ClassCastException e) {
- log.error("Received notification value cast failed", e);
- return;
- }
+ V value = deserializeValue(kryo, valueBytes);
//
// Deliver the notification
@@ -390,16 +395,7 @@
//
byte[] valueBytes = event.getValue();
Kryo kryo = kryoFactory.newKryo();
- Input input = new Input(valueBytes);
-
- Object objValue = kryo.readClassAndObject(input);
- V value;
- try {
- value = typeV.cast(objValue);
- } catch (ClassCastException e) {
- log.error("Received notification value cast failed", e);
- return;
- }
+ V value = deserializeValue(kryo, valueBytes);
//
// Deliver the notification