Created
August 2, 2017 12:13
-
-
Save junbaor/61a3a7905a390d1ca0a4dee6ed5f73fd to your computer and use it in GitHub Desktop.
Test.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
<dependency> | |
<groupId>com.caucho</groupId> | |
<artifactId>hessian</artifactId> | |
<version>4.0.38</version> | |
</dependency> | |
*/ | |
import com.caucho.hessian.io.HessianInput; | |
import com.caucho.hessian.io.HessianOutput; | |
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
/** | |
* Created by junbaor on 2017/8/02. | |
*/ | |
public class Test { | |
public static void main(String[] args) throws IOException { | |
B b = new B(); | |
b.setAge(10); | |
A a = (A) b; | |
byte[] serialize = serialize(a); | |
B deserialize = (B) deserialize(serialize); | |
System.out.println(a); // B{age=10} | |
System.out.println(b); // B{age=10} | |
System.out.println(deserialize); // B{age=null} | |
} | |
public static byte[] serialize(Object obj) throws IOException { | |
ByteArrayOutputStream os = new ByteArrayOutputStream(); | |
HessianOutput ho = new HessianOutput(os); | |
ho.writeObject(obj); | |
return os.toByteArray(); | |
} | |
public static Object deserialize(byte[] by) throws IOException { | |
ByteArrayInputStream is = new ByteArrayInputStream(by); | |
HessianInput hi = new HessianInput(is); | |
return hi.readObject(); | |
} | |
static class ClassA { | |
private Integer age; | |
public Integer getAge() { | |
return age; | |
} | |
public void setAge(Integer age) { | |
this.age = age; | |
} | |
@Override | |
public String toString() { | |
final StringBuffer sb = new StringBuffer("B{"); | |
sb.append("age=").append(age); | |
sb.append('}'); | |
return sb.toString(); | |
} | |
} | |
static class ClassB { | |
private Integer age; | |
public Integer getAge() { | |
return age; | |
} | |
public void setAge(Integer age) { | |
this.age = age; | |
} | |
@Override | |
public String toString() { | |
final StringBuffer sb = new StringBuffer("B{"); | |
sb.append("age=").append(age); | |
sb.append('}'); | |
return sb.toString(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment