Hession2 序列化Byte--反序列化為Integer 預設
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;
}
相關文章
- kotlin gson反序列化預設值失效Kotlin
- 為什麼需要序列化和反序列化
- 物件序列化(序列化)物件
- Java序列化、反序列化、反序列化漏洞Java
- 序列化與反序列化
- JavaScript物件序列化為JSONJavaScript物件JSON
- 009 Rust 網路程式設計,序列化與反序列化Rust程式設計
- 序列化與反序列化(GO)Go
- 預設序列化會報警告Hessian/Burlap: calss is an unknown class in WebappClassLoaderWebAPP
- python json反序列化為物件PythonJSON物件
- Hive的序列化/反序列化(SerDe)Hive
- python的序列化和反序列化Python
- ctf serialize 序列化和反序列化
- Java的序列化和反序列化Java
- Java的序列化與反序列化Java
- xml序列化和反序列化(一)XML
- C++ 序列化和反序列化C++
- 序列化
- Python中物件序列化和反序列化Python物件
- [Java基礎]序列化和反序列化Java
- Java中的序列化與反序列化Java
- JSR310-LocalDateTime序列化 & 反序列化JSLDA
- IO流(3) - 序列化和反序列化
- Flutter中JSON序列化與反序列化FlutterJSON
- PHP的序列化和反序列化入門PHP
- C#序列化和反序列化(json)C#JSON
- 實體類為啥要序列化
- Django REST framework 序列化與反序列化(4)DjangoRESTFramework
- 序列化/反序列化,我忍你很久了
- .NET物件的XML序列化和反序列化物件XML
- XML序列化XML
- 反序列化
- 序列化集合
- java序列化Java
- Spark序列化Spark
- 全網最適合入門的物件導向程式設計教程:57 Python字串與序列化-序列化與反序列化物件程式設計Python字串
- 為什麼Java需要物件的序列化Java物件
- 從java的序列化和反序列化說起Java