VO.java

import java.io.Serializable;

/**
 * [概 要] java对象序列化工具<br/>
 * [环 境] J2SE 1.7
 * @author 研发部-ly
 * @version 1.0
 */
public class VO<T> implements Serializable {
    private T value;
    public VO(T value) {
        this.value = value;
    }
    public VO() {
    }
    public T getValue() {
        return value;
    }
    @Override
    public String toString() {
        return "VO{" +
                "value=" + value +
                '}';
    }
}

 

 

ProtostuffSerializer.java

import java.util.concurrent.ConcurrentHashMap;

import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtostuffIOUtil;
import com.dyuproject.protostuff.Schema;
import com.dyuproject.protostuff.runtime.RuntimeSchema;

/**
 * [概 要] protostuff对象序列化工具<br/>
 * [环 境] J2SE 1.7
 * @author 研发部-ly
 * @version 1.0
 */
public class ProtostuffSerializer {
    private static ConcurrentHashMap<Class<?>, Schema<?>> cachedSchema = new ConcurrentHashMap<Class<?>, Schema<?>>();
    public <T> byte[] serialize(final T source) {
        VO<T> vo = new VO<T>(source);
        final LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
        try {
            final Schema<VO> schema = getSchema(VO.class);
            return serializeInternal(vo, schema, buffer);
        } catch (final Exception e) {
            throw new IllegalStateException(e.getMessage(), e);
        } finally {
            buffer.clear();
        }
    }
    public <T> T deserialize(final byte[] bytes) {
        try {
            Schema<VO> schema = getSchema(VO.class);
            VO vo = deserializeInternal(bytes, schema.newMessage(), schema);
            if (vo != null && vo.getValue() != null) {
                return (T) vo.getValue();
            }
        } catch (final Exception e) {
            throw new IllegalStateException(e.getMessage(), e);
        }
        return null;
    }
    private <T> byte[] serializeInternal(final T source, final Schema<T> schema, final LinkedBuffer buffer) {
        return ProtostuffIOUtil.toByteArray(source, schema, buffer);
    }
    private <T> T deserializeInternal(final byte[] bytes, final T result, final Schema<T> schema) {
        ProtostuffIOUtil.mergeFrom(bytes, result, schema);
        return result;
    }
    private static <T> Schema<T> getSchema(Class<T> clazz) {
        @SuppressWarnings("unchecked")
        Schema<T> schema = (Schema<T>) cachedSchema.get(clazz);
        if (schema == null) {
            schema = RuntimeSchema.createFrom(clazz);
            cachedSchema.put(clazz, schema);
        }
        return schema;
    }
}

 

 

测试:

 

/**
     * 
     * [概 要] 序列化对象
     * 
     * @param object 要序列化的对象
     * @return byte[] 对象序列化后的字节信息
     */
    private byte[] serialize(Object object) {
        
        ProtostuffSerializer protostuffSerializer = new ProtostuffSerializer();
        byte[] result=protostuffSerializer.serialize(object);
        
        return result;
    }
    
    /**
     * 
     * [概 要] 反序列化
     * @param bytes 对象的字节信息
     * @return Object 反序列化的对象
     */
    public static Object unserialize(byte[] bytes) {
        
        ProtostuffSerializer protostuffSerializer = new ProtostuffSerializer();
        Object object=protostuffSerializer.deserialize(bytes);
        
        return object;
    }