Hession2 序列化Byte--反序列化为Integer 默认(arbitrary object)情况。自定义类型反序列化则不会改变Byte类型
/**
* test Byte
*/
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(1024);
Hessian2ObjectOutput h2o = new Hessian2ObjectOutput(outputStream);
h2o.writeObject(new Byte("9"));//Byte 类型
h2o.flushBuffer();
outputStream.flush();
byte[] testByte = outputStream.toByteArray();
System.out.println(testByte + "---hession2+byte---" + testByte.length);
ByteBufInputStream in = new ByteBufInputStream(Unpooled.buffer().writeBytes(testByte));
Hessian2ObjectInput ino = new Hessian2ObjectInput(in);
//Hessian2ObjectInput ino = new Hessian2ObjectInput(new ByteArrayInputStream(testByte));
Object byteobj = ino.readObject();
System.out.println(byteobj.getClass());// Integer 类型
System.out.println(byteobj);
验证结果
[B@1189dd52---hession2+byte---1
class java.lang.Integer
9
原因:
/**
* Reads an arbitrary object from the input stream when the type
* is unknown.
*/
public Object readObject()
throws IOException
{
int tag = _offset < _length ? (_buffer[_offset++] & 0xff) : read();
switch (tag) {
case 'N':
return null;
case 'T':
return Boolean.valueOf(true);
case 'F':
return Boolean.valueOf(false);
// direct integer
case 0x80: case 0x81: case 0x82: case 0x83:
case 0x84: case 0x85: case 0x86: case 0x87:
case 0x88: case 0x89: case 0x8a: case 0x8b:
case 0x8c: case 0x8d: case 0x8e: case 0x8f:
case 0x90: case 0x91: case 0x92: case 0x93:
case 0x94: case 0x95: case 0x96: case 0x97:
case 0x98: case 0x99: case 0x9a: case 0x9b:
case 0x9c: case 0x9d: case 0x9e: case 0x9f:
case 0xa0: case 0xa1: case 0xa2: case 0xa3:
case 0xa4: case 0xa5: case 0xa6: case 0xa7:
case 0xa8: case 0xa9: case 0xaa: case 0xab:
case 0xac: case 0xad: case 0xae: case 0xaf:
case 0xb0: case 0xb1: case 0xb2: case 0xb3:
case 0xb4: case 0xb5: case 0xb6: case 0xb7:
case 0xb8: case 0xb9: case 0xba: case 0xbb:
case 0xbc: case 0xbd: case 0xbe: case 0xbf:
return Integer.valueOf(tag - BC_INT_ZERO);
/* byte int */
case 0xc0: case 0xc1: case 0xc2: case 0xc3:
case 0xc4: case 0xc5: case 0xc6: case 0xc7:
case 0xc8: case 0xc9: case 0xca: case 0xcb:
case 0xcc: case 0xcd: case 0xce: case 0xcf:
return Integer.valueOf(((tag - BC_INT_BYTE_ZERO) << 8) + read());
后面省略...
自定义类型
UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream(1024);
ObjectOutput out = new Hessian2ObjectOutput(bos);
try {
out.writeObject(new mmm("山东", "滨州", 1002,new Byte("45")));
out.flushBuffer();
bos.flush();
byte[] data = bos.toByteArray();
System.out.println(String.valueOf(data));
System.out.println(mmm.class.getClassLoader());
ByteBuf buf = Unpooled.directBuffer();
ByteBuf byteBuf = buf.writeBytes(data);
ByteBufInputStream stream = new ByteBufInputStream(byteBuf);
ByteSink sink = Files.asByteSink(new File("/Users/xiayin/123.txt"), FileWriteMode.APPEND);
ByteSink sink1 = Files.asByteSink(new File("/Users/xiayin/456.txt"), FileWriteMode.APPEND);
sink.write(data);
Hessian2ObjectInput objectInput = new Hessian2ObjectInput(stream);
Object o = objectInput.readObject();
System.out.println("hession2-----" + data.length);
System.out.println(JacksonSupport.toJson(o));
System.out.println(o.getClass());
System.out.println(o.getClass().getDeclaredField("DD").getType());}//不会改变的。因为使用classLoader 加载类类型,进行反序列化。
static class mmm implements Serializable {
private static final long serialVersionUID = 7214221008502796951L;
private String AA;
private String BB;
private int CC;
private Byte DD;
public mmm(String AA, String BB, int CC, Byte DD) {
this.AA = AA;
this.BB = BB;
this.CC = CC;
this.DD = DD;
}