Carmelo Cascone
Committed by Brian O'Connor

Added kryo serializer for ImmutableByteSequence

Serialization was failing when the inner ByteBuffer instance was a
HeapByteBuffer. Also, simplified ImmutableByteSequence.toString()

Change-Id: I4f75086a9b6536205fb43b78a0e4d2bfce9e8a5c
1 +/*
2 + * Copyright 2016-present Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +package org.onosproject.store.serializers;
18 +
19 +import com.esotericsoftware.kryo.Kryo;
20 +import com.esotericsoftware.kryo.Serializer;
21 +import com.esotericsoftware.kryo.io.Input;
22 +import com.esotericsoftware.kryo.io.Output;
23 +import org.onlab.util.ImmutableByteSequence;
24 +
25 +/**
26 + * Kryo serializer for {@link ImmutableByteSequence}.
27 + */
28 +public class ImmutableByteSequenceSerializer extends Serializer<ImmutableByteSequence> {
29 +
30 + /**
31 + * Creates a new {@link ImmutableByteSequence} serializer instance.
32 + */
33 + public ImmutableByteSequenceSerializer() {
34 + // non-null, immutable.
35 + super(false, true);
36 + }
37 +
38 + @Override
39 + public void write(Kryo kryo, Output output, ImmutableByteSequence object) {
40 + byte[] data = object.asArray();
41 + output.writeInt(data.length);
42 + output.write(data);
43 + }
44 +
45 + @Override
46 + public ImmutableByteSequence read(Kryo kryo, Input input, Class<ImmutableByteSequence> type) {
47 + int length = input.readInt();
48 + byte[] data = new byte[length];
49 + input.read(data);
50 + return ImmutableByteSequence.copyFrom(data);
51 + }
52 +}
...@@ -534,7 +534,7 @@ public final class KryoNamespaces { ...@@ -534,7 +534,7 @@ public final class KryoNamespaces {
534 ) 534 )
535 .register(ClosedOpenRange.class) 535 .register(ClosedOpenRange.class)
536 .register(DiscreteResourceCodec.class) 536 .register(DiscreteResourceCodec.class)
537 - .register(ImmutableByteSequence.class) 537 + .register(new ImmutableByteSequenceSerializer(), ImmutableByteSequence.class)
538 .build("API"); 538 .build("API");
539 539
540 540
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
16 16
17 package org.onlab.util; 17 package org.onlab.util;
18 18
19 -import com.google.common.base.MoreObjects;
20 import com.google.common.base.Objects; 19 import com.google.common.base.Objects;
21 20
22 import java.nio.ByteBuffer; 21 import java.nio.ByteBuffer;
...@@ -238,8 +237,6 @@ public final class ImmutableByteSequence { ...@@ -238,8 +237,6 @@ public final class ImmutableByteSequence {
238 237
239 @Override 238 @Override
240 public String toString() { 239 public String toString() {
241 - return MoreObjects.toStringHelper(this) 240 + return HexString.toHexString(value.array());
242 - .addValue(HexString.toHexString(asArray()))
243 - .toString();
244 } 241 }
245 } 242 }
...\ No newline at end of file ...\ No newline at end of file
......