package com.cmall.redis.utils; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.concurrent.ConcurrentHashMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.cmal.redis.vo.SeriObj; import com.dyuproject.protostuff.LinkedBuffer; import com.dyuproject.protostuff.ProtostuffIOUtil; import com.dyuproject.protostuff.Schema; import com.dyuproject.protostuff.runtime.RuntimeSchema; public class RedisSeriUtils { final static Logger logger = LoggerFactory.getLogger(RedisSeriUtils.class); private static ConcurrentHashMap<Class<?>, Schema<?>> cachedSchema = new ConcurrentHashMap<Class<?>, Schema<?>>(); /** *字段加了@Deprecated 不会序列化 / public static <T> byte[] serialize(final T source) { SeriObj<T> SeriObj = new SeriObj<T>(source); final LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE); try { final Schema<SeriObj> schema = getSchema(SeriObj.class); return serializeInternal(SeriObj, schema, buffer); } catch (final Exception e) { throw new IllegalStateException(e.getMessage(), e); } finally { buffer.clear(); } } public static <T> T deserialize(final byte[] bytes) { try { Schema<SeriObj> schema = getSchema(SeriObj.class); SeriObj SeriObj = deserializeInternal(bytes, schema.newMessage(), schema); if (SeriObj != null && SeriObj.getValue() != null) { return (T) SeriObj.getValue(); } } catch (final Exception e) { throw new IllegalStateException(e.getMessage(), e); } return null; } /** * * @description * @return * @Exception * @see RedisSeriUtils.deserialize */ @Deprecated public static Object ByteToObject(byte[] bytes) { Object obj = null; try { if (bytes == null) return null; ByteArrayInputStream bi = new ByteArrayInputStream(bytes); ObjectInputStream oi = new ObjectInputStream(bi); obj = oi.readObject(); bi.close(); oi.close(); } catch (Exception e) { logger.error("translation", e); } return obj; } /** * * @description * @return * @Exception * @see RedisSeriUtils.serialize */ @Deprecated public static byte[] ObjectToByte(Object obj) { byte[] bytes = null; try { // object to bytearray ByteArrayOutputStream bo = new ByteArrayOutputStream(); ObjectOutputStream oo = new ObjectOutputStream(bo); oo.writeObject(obj); bytes = bo.toByteArray(); bo.close(); oo.close(); } catch (Exception e) { logger.error("translation", e); } return bytes; } private static <T> byte[] serializeInternal(final T source, final Schema<T> schema, final LinkedBuffer buffer) { return ProtostuffIOUtil.toByteArray(source, schema, buffer); } private static <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; } }
序列化 protostuff
猜你喜欢
转载自m635674608.iteye.com/blog/2381011
今日推荐
周排行