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

com.jn.agileway.codec.serialization.hessian.Hessians Maven / Gradle / Ivy

Go to download

Provide an unified codec API for hession, kryo, protostuff, fst, fes, xson, cbor, jackson, json, etc....

There is a newer version: 3.1.12
Show newest version
package com.jn.agileway.codec.serialization.hessian;

import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;
import com.jn.langx.Factory;
import com.jn.langx.annotation.NonNull;
import com.jn.langx.annotation.Nullable;
import com.jn.langx.codec.CodecException;
import com.jn.langx.text.StringTemplates;
import com.jn.langx.util.Preconditions;
import com.jn.langx.util.concurrent.threadlocal.ThreadLocalFactory;
import com.jn.langx.util.io.IOs;
import com.jn.langx.util.reflect.Reflects;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Hessians {
    private Hessians() {
    }

    public static final ThreadLocalFactory hessian2OutputFactory = new ThreadLocalFactory(new Factory() {
        @Override
        public Hessian2Output get(Object o) {
            return new Hessian2Output();
        }
    });

    public static final ThreadLocalFactory hessian2InputFactory = new ThreadLocalFactory(new Factory() {
        @Override
        public Hessian2Input get(Object o) {
            return new Hessian2Input();
        }
    });

    public static  byte[] serialize(T o) throws IOException {
        return serialize(hessian2OutputFactory, o);
    }

    public static  byte[] serialize(Factory hessian2OutputFactory, T o) throws IOException {
        if (o == null) {
            return null;
        }
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        try {
            serialize(hessian2OutputFactory, o, bao);
            return bao.toByteArray();
        } finally {
            IOs.close(bao);
        }
    }

    public static  void serialize(@NonNull Factory hessian2OutputFactory, @Nullable T o, @NonNull OutputStream outputStream) throws IOException {
        if (o == null) {
            return;
        }
        Preconditions.checkNotNull(hessian2OutputFactory, "hessian output factory is null");
        Preconditions.checkNotNull(outputStream, "the output stream is null");
        Hessian2Output output = null;
        try {
            output = hessian2OutputFactory.get(null);
            output.init(outputStream);
            output.writeObject(o);
            output.flush();
        } finally {
            if (hessian2OutputFactory instanceof ThreadLocalFactory) {
                if (output != null) {
                    output.reset();
                }
            } else {
                IOs.close(output);
            }
        }
    }

    public static  T deserialize(byte[] bytes) throws IOException {
        return deserialize(hessian2InputFactory, bytes, null);
    }


    public static  T deserialize(byte[] bytes, @NonNull Class targetType) throws IOException {
        return deserialize(hessian2InputFactory, bytes, targetType);
    }


    public static  T deserialize(Factory hessian2InputFactory, byte[] bytes, @Nullable Class targetType) throws IOException {
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        Hessian2Input input = null;
        try {
            input = hessian2InputFactory.get(null);
            ByteArrayInputStream bai = new ByteArrayInputStream(bytes);
            input.init(bai);
            Object obj = input.readObject(targetType);
            if (targetType != null) {
                if (!Reflects.isInstance(obj, targetType)) {
                    throw new CodecException(StringTemplates.formatWithPlaceholder("{} is not cast to {} when use FSE deserialize", Reflects.getFQNClassName(obj.getClass()), Reflects.getFQNClassName(targetType)));
                }
                return (T) obj;
            } else {
                return (T) obj;
            }
        } finally {
            if (hessian2InputFactory instanceof ThreadLocalFactory) {
                if (input != null) {
                    input.reset();
                }
            } else {
                IOs.close(input);
            }
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy