通过网络和字节数组序列化/反序列化 Java 对象



我有来自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 可序列化对象到字节数组得到提示

@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());
}

如何使用 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();
        }
    }
}

最新更新