[ONOS-6892] Use streams for Kryo serialization to avoid IndexOutOfBoundsException when serializing/deserializing large objects

Change-Id: Ifc1ecf59f655aa65f034f94e10b61e77bbe4c3c4
This commit is contained in:
Jordan Halterman 2017-08-03 21:38:02 -07:00
parent a37e32f488
commit ecfca4f877

View File

@ -33,6 +33,8 @@ import org.apache.commons.lang3.tuple.Pair;
import org.objenesis.strategy.StdInstantiatorStrategy;
import org.slf4j.Logger;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
@ -303,11 +305,12 @@ public final class KryoNamespace implements KryoFactory, KryoPool {
* @return serialized bytes
*/
public byte[] serialize(final Object obj, final int bufferSize) {
Output out = new Output(bufferSize, MAX_BUFFER_SIZE);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(bufferSize);
Output out = new Output(outputStream);
return pool.run(kryo -> {
kryo.writeClassAndObject(out, obj);
out.flush();
return out.toBytes();
return outputStream.toByteArray();
});
}
@ -364,7 +367,7 @@ public final class KryoNamespace implements KryoFactory, KryoPool {
* @return deserialized Object
*/
public <T> T deserialize(final byte[] bytes) {
Input in = new Input(bytes);
Input in = new Input(new ByteArrayInputStream(bytes));
Kryo kryo = borrow();
try {
@SuppressWarnings("unchecked")