package ch.ethz.globis.disindex.codec.field;

import ch.ethz.globis.disindex.codec.api.FieldEncoderDecoder;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.ByteBufferOutput;
import com.esotericsoftware.kryo.io.FastOutput;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;

import java.nio.ByteBuffer;
import java.util.Arrays;

/**
 * Generic Encoder - Decoder for any type of fields. The encoding/decoding is done using
 * serialization via Kryo.
 *
 * This encoder/decoder is only responsible for ONE type of Jave objects.
 *
 * @param <V>                               The type of Java objects used in the encoding/decoding.
 */
public class SerializingEncoderDecoder<V> implements FieldEncoderDecoder<V> {

    /** Reference to the Kryo API */
    private final Kryo kryo;

    /** The class of the type of Java object,*/
    private final Class<V> clazz;

    public SerializingEncoderDecoder(Class<V> clazz) {
        this.clazz = clazz;
        this.kryo = new Kryo();
        kryo.register(clazz);
    }

    @Override
    public V decode(byte[] payload) {
        if (payload[0] == -1) {
            return null;
        }
        Input input = new Input(payload);
        return kryo.readObject(input, clazz);
    }

    @Override
    public V decode(ByteBuffer payload) {
        return decode(payload.array());
    }

    @Override
    public byte[] encode(V value) {
        //ToDo make the serializing buffer configurable.
        Output output = new Output(1024);
        kryo.writeObject(output, value);
        int position = output.position();

        return Arrays.copyOf(output.getBuffer(), position);
    }
}
@@ -180,21 +180,10 @@
 .io.
-ByteBufferOut
+In
 put;
@@ -223,20 +223,16 @@
 .io.
-Fast
 Output;%0A
 impo
@@ -231,95 +231,46 @@
 ut;%0A
+%0A
 import 
-com.esotericsoftware.kryo.io.Input;%0Aimport com.esotericsoftware.kryo.io.Output;%0A
+java.io.ByteArrayOutputStream;
 %0Aimp
@@ -1384,96 +1384,62 @@
     
-//ToDo make the serializing buffer configurable.%0A        Output output = new Output(1024
+Output output = new Output(new ByteArrayOutputStream()
 );%0A 

Index distribution Changed the fixed size output buffer used for conversion to a resizable ByteArrayOutputStream.