当前位置:   article > 正文

Hession2 序列化Byte--反序列化为Integer 默认_hessian反序列化int变为integer

hessian反序列化int变为integer

Hession2 序列化Byte--反序列化为Integer 默认(arbitrary object)情况。自定义类型反序列化则不会改变Byte类型

  1. /**
  2. * test Byte
  3. */
  4. ByteArrayOutputStream outputStream = new ByteArrayOutputStream(1024);
  5. Hessian2ObjectOutput h2o = new Hessian2ObjectOutput(outputStream);
  6. h2o.writeObject(new Byte("9"));//Byte 类型
  7. h2o.flushBuffer();
  8. outputStream.flush();
  9. byte[] testByte = outputStream.toByteArray();
  10. System.out.println(testByte + "---hession2+byte---" + testByte.length);
  11. ByteBufInputStream in = new ByteBufInputStream(Unpooled.buffer().writeBytes(testByte));
  12. Hessian2ObjectInput ino = new Hessian2ObjectInput(in);
  13. //Hessian2ObjectInput ino = new Hessian2ObjectInput(new ByteArrayInputStream(testByte));
  14. Object byteobj = ino.readObject();
  15. System.out.println(byteobj.getClass());// Integer 类型
  16. System.out.println(byteobj);

验证结果

  1. [B@1189dd52---hession2+byte---1
  2. class java.lang.Integer
  3. 9
原因:
  1. /**
  2. * Reads an arbitrary object from the input stream when the type
  3. * is unknown.
  4. */
  5. public Object readObject()
  6. throws IOException
  7. {
  8. int tag = _offset < _length ? (_buffer[_offset++] & 0xff) : read();
  9. switch (tag) {
  10. case 'N':
  11. return null;
  12. case 'T':
  13. return Boolean.valueOf(true);
  14. case 'F':
  15. return Boolean.valueOf(false);
  16. // direct integer
  17. case 0x80: case 0x81: case 0x82: case 0x83:
  18. case 0x84: case 0x85: case 0x86: case 0x87:
  19. case 0x88: case 0x89: case 0x8a: case 0x8b:
  20. case 0x8c: case 0x8d: case 0x8e: case 0x8f:
  21. case 0x90: case 0x91: case 0x92: case 0x93:
  22. case 0x94: case 0x95: case 0x96: case 0x97:
  23. case 0x98: case 0x99: case 0x9a: case 0x9b:
  24. case 0x9c: case 0x9d: case 0x9e: case 0x9f:
  25. case 0xa0: case 0xa1: case 0xa2: case 0xa3:
  26. case 0xa4: case 0xa5: case 0xa6: case 0xa7:
  27. case 0xa8: case 0xa9: case 0xaa: case 0xab:
  28. case 0xac: case 0xad: case 0xae: case 0xaf:
  29. case 0xb0: case 0xb1: case 0xb2: case 0xb3:
  30. case 0xb4: case 0xb5: case 0xb6: case 0xb7:
  31. case 0xb8: case 0xb9: case 0xba: case 0xbb:
  32. case 0xbc: case 0xbd: case 0xbe: case 0xbf:
  33. return Integer.valueOf(tag - BC_INT_ZERO);
  34. /* byte int */
  35. case 0xc0: case 0xc1: case 0xc2: case 0xc3:
  36. case 0xc4: case 0xc5: case 0xc6: case 0xc7:
  37. case 0xc8: case 0xc9: case 0xca: case 0xcb:
  38. case 0xcc: case 0xcd: case 0xce: case 0xcf:
  39. return Integer.valueOf(((tag - BC_INT_BYTE_ZERO) << 8) + read());
  40. 后面省略...

自定义类型

  1. UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream(1024);
  2. ObjectOutput out = new Hessian2ObjectOutput(bos);
  3. try {
  4. out.writeObject(new mmm("山东", "滨州", 1002,new Byte("45")));
  5. out.flushBuffer();
  6. bos.flush();
  7. byte[] data = bos.toByteArray();
  8. System.out.println(String.valueOf(data));
  9. System.out.println(mmm.class.getClassLoader());
  10. ByteBuf buf = Unpooled.directBuffer();
  11. ByteBuf byteBuf = buf.writeBytes(data);
  12. ByteBufInputStream stream = new ByteBufInputStream(byteBuf);
  13. ByteSink sink = Files.asByteSink(new File("/Users/xiayin/123.txt"), FileWriteMode.APPEND);
  14. ByteSink sink1 = Files.asByteSink(new File("/Users/xiayin/456.txt"), FileWriteMode.APPEND);
  15. sink.write(data);
  16. Hessian2ObjectInput objectInput = new Hessian2ObjectInput(stream);
  17. Object o = objectInput.readObject();
  18. System.out.println("hession2-----" + data.length);
  19. System.out.println(JacksonSupport.toJson(o));
  20. System.out.println(o.getClass());
  21. System.out.println(o.getClass().getDeclaredField("DD").getType());}//不会改变的。因为使用classLoader 加载类类型,进行反序列化。
  1. static class mmm implements Serializable {
  2. private static final long serialVersionUID = 7214221008502796951L;
  3. private String AA;
  4. private String BB;
  5. private int CC;
  6. private Byte DD;
  7. public mmm(String AA, String BB, int CC, Byte DD) {
  8. this.AA = AA;
  9. this.BB = BB;
  10. this.CC = CC;
  11. this.DD = DD;
  12. }






声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/1011983
推荐阅读
相关标签
  

闽ICP备14008679号