OSGI下的SerializeUtil,解决ClassNotFoundException

原文参考http://stackoverflow.com/questions/13861342/how-do-you-deserialize-an-object-from-bytes-in-osgi

package com.daren.core.util;

import com.google.gson.Gson;
import org.apache.wicket.application.IClassResolver;
import org.ops4j.pax.wicket.util.serialization.PaxWicketObjectInputStream;
import org.ops4j.pax.wicket.util.serialization.PaxWicketObjectOutputStream;

import java.io.*;
import java.net.URL;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * @类描述:redis 序列化应用类
 * @创建人:sunlf
 * @创建时间:2014-07-01 下午3:47
 * @修改人:
 * @修改时间:
 * @修改备注:
 */
public class SerializeUtil {
    public static byte[] serialize(Object object) {
        ByteArrayOutputStream baos = null;
        ObjectOutputStream oos = null;
        try {
            // 序列化
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(object);
            byte[] bytes = baos.toByteArray();
            return bytes;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;


    }

    public static String serializeJson(Object object) {

        Gson mapper = new Gson();
        try {
            return mapper.toJson(object);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;

    }

    public static <T> T unserializeJson(String json, Class cls) {

        Gson mapper = new Gson();
        try {
            return (T) mapper.fromJson(json, cls);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    @SuppressWarnings("unchecked")
    public static <T> T unserialize(byte[] bytes) {

        ByteArrayInputStream bais = null;
        try {
            // 反序列化
            bais = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ObjectInputStream(bais) {
                Set<ClassLoader> lhs = new LinkedHashSet<ClassLoader>();
                {
                    // Keep a set if discovered class loaders
                    lhs.add(getClass().getClassLoader());
                }

                @Override
                protected Class<?> resolveClass(ObjectStreamClass desc)
                        throws ClassNotFoundException, IOException {

                    for (ClassLoader cl : lhs)
                        try {
                            Class<?> c = cl.loadClass(desc.getName());

                            // we found the class, so we can use its class loader,
                            // it is in the proper class space  if the uses constraints
                            // are set properly (and you're using bnd so you should be ok)

                            lhs.add(c.getClassLoader());

                            // The paranoid among us would check
                            // the serial uuid here ...
                            // long uuid = desc.getSerialVersionUID();
                            // Field field = c.getField("serialVersionUID");
                            // assert uuid == field.get(null)

                            return c;
                        } catch (Exception e) {
                            // Ignore
                        }

                    // Fallback (for void and primitives)
                    return super.resolveClass(desc);
                }
            };

            Object o = ois.readObject();
            return (T) o;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值