添加依赖
io.protostuff protostuff-core 1.5.9 io.protostuff protostuff-runtime 1.5.9 commons-io commons-io 2.6
编写序列化工具类
package meng.springboot.demo.obj;import io.protostuff.LinkedBuffer;import io.protostuff.ProtostuffIOUtil;import io.protostuff.Schema;import io.protostuff.runtime.RuntimeSchema;import java.util.Map;import java.util.concurrent.ConcurrentHashMap;/** * XXX * * @author mengzhang6 * @date 2018/10/22 19:18 */public class ProtostuffUtils { private static Map, Schema > cachedSchema = new ConcurrentHashMap<>(); private static Schema getSchema(Class cls) { Schema schema = (Schema ) cachedSchema.get(cls); if (schema == null) { schema = RuntimeSchema.createFrom(cls); if (schema != null) { cachedSchema.put(cls, schema); } } return schema; } /** * 序列化 * * @param obj * @param * @return */ public static byte[] serialize(T obj) { Class cls = (Class ) obj.getClass(); LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE); try { Schema schema = getSchema(cls); return ProtostuffIOUtil.toByteArray(obj, schema, buffer); } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } finally { buffer.clear(); } } /** * 反序列化 * * @param data * @param cls * @param * @return */ public static T deserialize(byte[] data, Class cls) { try { Schema schema = getSchema(cls); T message = schema.newMessage(); ProtostuffIOUtil.mergeFrom(data, message, schema); return message; } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } }}
实体类
package meng.springboot.demo.obj;import java.util.Date;/** * XXX * * @author mengzhang6 * @date 2018/10/22 19:36 */public class User4 { private int id; private String name; private Date addDate; /** * 使用transient表示,不参与序列化 */ private transient String no; /** * 声明为static和transient类型的成员数据不能被序列化 * 因为static代表类的状态,transient代表对象的临时数据。 */ public static String staNo; /** * Archive也需要实现Serializable */ private Archive archive; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getAddDate() { return addDate; } public void setAddDate(Date addDate) { this.addDate = addDate; } public String getNo() { return no; } public void setNo(String no) { this.no = no; } public Archive getArchive() { return archive; } public void setArchive(Archive archive) { this.archive = archive; } @Override public String toString() { return "User4{" + "id=" + id + ", name='" + name + '\'' + ", addDate=" + addDate + ", no='" + no + '\'' + ", archive=" + archive + '}'; }}
单元测试
@Test public void writeObject4() throws IOException { User4 user = new User4(); user.setId(1004); user.setName("晨猫"); user.setAddDate(Calendar.getInstance().getTime()); user.setNo("Xs01"); user.setArchive(new Archive("Mc001", "Hangzhou", "18366557620")); System.out.println(user); byte[] data = ProtostuffUtils.serialize(user); FileUtils.writeByteArrayToFile(new File("User4.out"), data); } @Test public void readObject4() throws IOException, ClassNotFoundException { User4 user = ProtostuffUtils.deserialize(FileUtils.readFileToByteArray(new File("User4.out")), User4.class); System.out.println(user); }