import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Arrays; import java.util.List; import java.util.Objects; import org.junit.Test; import io.protostuff.LinkedBuffer; import io.protostuff.ProtostuffIOUtil; import io.protostuff.Schema; import io.protostuff.runtime.RuntimeSchema; /** * 序列化,反序列化工具 * * @project iweixin-pay * @fileName SerializeUtil.java * @Description * @author light-zhang * @date 2018年5月31日上午9:51:25 * @version 1.0.0 */ public class SerializeUtil { /** * 序列化 * * @param object * @return */ public static <T> byte[] serialize(T object) { if (Objects.isNull(object)) { throw new RuntimeException("序列化对象(" + object + ")!"); } @SuppressWarnings("unchecked") Schema<T> schema = (Schema<T>) RuntimeSchema.getSchema(object.getClass()); LinkedBuffer buffer = LinkedBuffer.allocate(1024 * 1024); byte[] protostuff = null; try { protostuff = ProtostuffIOUtil.toByteArray(object, schema, buffer); } catch (Exception e) { throw new RuntimeException("序列化(" + object.getClass() + ")对象(" + object + ")发生异常!", e); } finally { buffer.clear(); } return protostuff; } /** * 反序列化 * * @param paramArrayOfByte * @param targetClass * @return */ public static <T> T unserialize(byte[] paramArrayOfByte, Class<T> targetClass) { if (Objects.isNull(paramArrayOfByte) || paramArrayOfByte.length == 0) { throw new RuntimeException("反序列化对象发生异常,byte序列为空!"); } T instance = null; try { instance = targetClass.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new RuntimeException("反序列化过程中依据类型创建对象失败!", e); } Schema<T> schema = RuntimeSchema.getSchema(targetClass); ProtostuffIOUtil.mergeFrom(paramArrayOfByte, instance, schema); return instance; } /** * 序列化集合 * * @param objList * @return */ public static <T> byte[] serializeList(List<T> objList) { if (objList == null || objList.isEmpty()) { throw new RuntimeException("序列化对象列表(" + objList + ")参数异常!"); } @SuppressWarnings("unchecked") Schema<T> schema = (Schema<T>) RuntimeSchema.getSchema(objList.get(0).getClass()); LinkedBuffer buffer = LinkedBuffer.allocate(1024 * 1024); byte[] protostuff = null; ByteArrayOutputStream bos = null; try { bos = new ByteArrayOutputStream(); ProtostuffIOUtil.writeListTo(bos, objList, schema, buffer); protostuff = bos.toByteArray(); } catch (Exception e) { throw new RuntimeException("序列化对象列表(" + objList + ")发生异常!", e); } finally { buffer.clear(); try { if (bos != null) { bos.close(); } } catch (IOException e) { e.printStackTrace(); } } return protostuff; } public static <T> List<T> deserializeList(byte[] paramArrayOfByte, Class<T> targetClass) { if (paramArrayOfByte == null || paramArrayOfByte.length == 0) { throw new RuntimeException("反序列化对象发生异常,byte序列为空!"); } Schema<T> schema = RuntimeSchema.getSchema(targetClass); List<T> result = null; try { result = ProtostuffIOUtil.parseListFrom(new ByteArrayInputStream(paramArrayOfByte), schema); } catch (IOException e) { throw new RuntimeException("反序列化对象列表发生异常!", e); } return result; } @Test public void demo() { byte[] result = SerializeUtil.serialize("123"); System.out.println(Arrays.toString(result)); System.out.println(SerializeUtil.unserialize(result, String.class)); } }