![](/img/trans.png)
[英]How to serialize a java object with not Serializable fields inside it into byte array and deserialize the array to get the original object
[英]Serialize/Deserialize Java object through network and byte array
我有來自DZone( http://www.dzone.com/links/r/java_custom_serialization_example.html )的代碼,它將Java對象從/向文件序列化/反序列化。
final class Hello implements Serializable
{
int x = 10;
int y = 20;
public int getX()
{
return x;
}
public int getY()
{
return y;
}
}
public class SerializedComTest {
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Test
public void testFile() throws IOException, ClassNotFoundException {
Hello h = new Hello();
FileOutputStream bs = new FileOutputStream("hello.txt"); // ("testfile");
ObjectOutputStream out = new ObjectOutputStream(bs);
out.writeObject(h);
out.flush();
out.close();
Hello h2;
FileInputStream fis = new FileInputStream("hello.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
h2 = (Hello) ois.readObject();
assertTrue(10 == h2.getX());
assertTrue(20 == h2.getY());
}
}
如何使用Java套接字傳輸序列化對象? 還有如何將序列化/反序列化對象存儲到字節數組中/從字節數組存儲。
這是用於序列化到/從字節數組的代碼。 我得到了一些提示--Java Serializable Object to Byte Array
@Test
public void testByteArray() throws IOException, ClassNotFoundException, InterruptedException {
Hello h = new Hello();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(bos);
out.writeObject(h);
byte b[] = bos.toByteArray();
out.close();
bos.close();
Hello h2;
ByteArrayInputStream bis = new ByteArrayInputStream(b);
ObjectInput in = new ObjectInputStream(bis);
h2 = (Hello) in.readObject();
assertTrue(10 == h2.getX());
assertTrue(20 == h2.getY());
}
就像你用objectstream類包裝文件流一樣,你也可以使用套接字。 你不應該“存儲”的序列化對象的字符串。
如何使用Java套接字傳輸序列化對象?
將其輸出流包裝在ObjectOutputStream
。
還有如何將序列化/反序列化對象存儲到字符串中/從字符串中存儲。
你沒有。 序列化對象是二進制的,應存儲在字節數組中。 反序列化對象是對象本身,而不是字符串。
您不需要那些readObject()
和writeObject()
方法。 他們不做任何默認情況下不會發生的事情。
這是有效的代碼,我從http://cyberasylum.janithw.com/object-serialization-over-networks-in-java/得到了提示。
@Test(timeout = 2000)
public void testStream() throws IOException, ClassNotFoundException, InterruptedException {
PingerThread pinger = new PingerThread(9092);
pinger.start();
String serverAddress = "localhost";
Socket s;
PrintWriter output;
BufferedReader input;
try {
// Client
s = new Socket(serverAddress, 9092);
}
catch (IOException e)
{
// when error, try again
Thread.sleep(500);
s = new Socket(serverAddress, 9092);
}
// send the object over the network
Hello h = new Hello();
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
out.writeObject(h);
out.flush();
ObjectInputStream in = new ObjectInputStream(s.getInputStream());
System.out.println("2");
Hello h2;
h2 = (Hello) in.readObject();
assertTrue(10 == h2.getX());
assertTrue(20 == h2.getY());
}
private class PingerThread extends Thread {
public int portNumber;
public PingerThread(int portNumber) {
super();
this.portNumber = portNumber;
}
@Override
public void run() {
try {
ServerSocket listener = new ServerSocket(this.portNumber);
Socket socket = listener.accept();
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
Hello h;
while((h = (Hello) in.readObject()) != null) {
System.out.println("1");
//h = (Hello) in.readObject();
System.out.println(h.getX());
System.out.println(h.getY());
out.writeObject(h);
out.flush();
}
System.out.println("OUT");
socket.close();
listener.close();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.