All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.rx.io.Serializer Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package org.rx.io;

import org.rx.core.Arrays;
import org.rx.core.Constants;

public interface Serializer {
    Serializer DEFAULT = new JdkAndJsonSerializer();

    default  byte[] serializeToBytes(T[] obj) {
        if (Arrays.isEmpty(obj)) {
            return Arrays.EMPTY_BYTE_ARRAY;
        }
        try (HybridStream stream = new HybridStream()) {
            for (T t : obj) {
                serialize(t, stream);
            }
            return stream.toArray();
        }
    }

    default  byte[] serializeToBytes(T obj) {
        try (IOStream stream = serialize(obj)) {
            return stream.toArray();
        }
    }

    default  IOStream serialize(T obj) {
        return serialize(obj, Constants.MAX_HEAP_BUF_SIZE, false);
    }

    default  HybridStream serialize(T obj, int maxMemorySize, boolean directMemory) {
        HybridStream stream = new HybridStream(maxMemorySize, directMemory);
        serialize(obj, stream);
        return stream.rewind();
    }

     void serialize(T obj, IOStream stream);

    default  T deserializeFromBytes(byte[] data) {
        return deserialize(IOStream.wrap("", data));
    }

    default  T deserialize(IOStream stream) {
        return deserialize(stream, false);
    }

     T deserialize(IOStream stream, boolean leveOpen);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy