com.jn.agileway.codec.serialization.activejser.ActivejSerializers Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of agileway-codec Show documentation
Show all versions of agileway-codec Show documentation
Provide an unified codec API for hession, kryo, protostuff, fst, fes, xson, cbor, jackson, json, etc....
package com.jn.agileway.codec.serialization.activejser;
import com.jn.agileway.codec.serialization.SchemaedStruct;
import com.jn.langx.annotation.NonNull;
import com.jn.langx.codec.CodecException;
import com.jn.langx.util.ClassLoaders;
import com.jn.langx.util.Preconditions;
import com.jn.langx.util.reflect.Reflects;
import io.activej.serializer.BinaryInput;
import io.activej.serializer.BinaryOutput;
import io.activej.serializer.BinarySerializer;
import io.activej.serializer.SerializerBuilder;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class ActivejSerializers {
private static final SerializerBuilder DEFAULT = SerializerBuilder.create();
public static SerializerBuilder getSerializerBuilder() {
return DEFAULT;
}
private static final ThreadLocal outBufferFactory = new ThreadLocal() {
@Override
protected BinaryOutput initialValue() {
return new BinaryOutput(new byte[4096]);
}
};
private static final ThreadLocal inputBufferFactory = new ThreadLocal() {
@Override
protected BinaryInput initialValue() {
return new BinaryInput(new byte[4096]);
}
};
public static byte[] serializeWithSchema(Object obj) {
try {
byte[] data = serialize(obj);
SchemaedStruct struct = new SchemaedStruct();
struct.setName(Reflects.getFQNClassName(obj.getClass()));
struct.setValue(data);
byte[] ret = serialize(struct);
return ret;
} catch (Throwable ex) {
throw new CodecException(ex);
}
}
public static byte[] serialize(Object obj) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
serialize(obj, out);
return out.toByteArray();
}
public static void serialize(Object obj, @NonNull OutputStream outputStream) throws IOException {
Preconditions.checkNotNull(outputStream);
BinarySerializer serializer = getSerializerBuilder().build(obj.getClass());
serializer.encode(outBufferFactory.get(), obj);
BinaryOutput out = outBufferFactory.get();
outputStream.write(out.array(), 0, out.pos());
out.pos(0);
}
public static T deserializeWithSchema(byte[] bytes) {
try {
SchemaedStruct struct = deserialize(bytes, SchemaedStruct.class);
String schema = struct.getName();
byte[] data = struct.getValue();
Class targetClass = ClassLoaders.loadClass(schema);
return deserialize(data, targetClass);
} catch (Throwable throwable) {
throw new CodecException(throwable);
}
}
public static T deserialize(byte[] bytes, @NonNull Class targetClass) {
Preconditions.checkNotNull(targetClass);
BinarySerializer serializer = getSerializerBuilder().build(targetClass);
T t = serializer.decode(new BinaryInput(bytes));
return t;
}
}