public static void main(String[] args) throws IOException, ClassNotFoundException { System.out.println(ByteUtil.sizeof(new Player1())); System.out.println(ByteUtil.sizeof(new Player2())); System.out.println(ByteUtil.sizeof(new Player3())); System.out.println(ByteUtil.sizeof(new Player4())); System.out.println(ByteUtil.sizeof(new Player5())); System.out.println("---------"); Player5 p5 = new Player5(); System.out.println(ByteUtil.sizeof(p5)); p5.id1 = 100000; p5.id2 = 200000; System.out.println(ByteUtil.sizeof(p5)); p5.name = "ooxx"; System.out.println(ByteUtil.sizeof(p5)); p5.name = "ooxxooxx"; System.out.println(ByteUtil.sizeof(p5)); System.out.println("---------"); byte[] bytes = ByteUtil.getBytes(p5); Player5 p5_2 = (Player5) ByteUtil.getObject(bytes); System.out.println(p5_2.id1); System.out.println(p5_2.id2); System.out.println(p5_2.name); System.out.println("---------"); System.out.println(ByteUtil.sizeof(new Player6())); Player6 p6 = new Player6(); System.out.println(ByteUtil.sizeof(p6)); p6.id1 = 100000; p6.id2 = 200000; System.out.println(ByteUtil.sizeof(p6)); p6.setName("Vicky"); System.out.println(ByteUtil.sizeof(p6)); p6.setName("中文名称"); System.out.println(ByteUtil.sizeof(p6)); bytes = ByteUtil.getBytes(p6); Player6 p6_2 = (Player6) ByteUtil.getObject(bytes); System.out.println(p6_2.id1); System.out.println(p6_2.id2); System.out.println(p6_2.getName()); }
public static ByteBuffer getByteBuffer(Serializable obj) throws IOException { byte[] bytes = ByteUtil.getBytes(obj); ByteBuffer buff = ByteBuffer.wrap(bytes); return buff; }