org.rx.io.Serializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxlib Show documentation
Show all versions of rxlib Show documentation
A set of utilities for Java
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);
}