简体   繁体   中英

Java/Android- How to use ObjectOutputStream to write a large size of List object in TCP/IP socket programming

I recently use ObjectOutputStream to write objects in TCP/IP socket programming.

If I want to write a large size of List<Object>/ArrayList<Object> through the socket

(eg list.size: 100, the total bytes may larger than the payload size),

should I just call writeObject(List<Object>/ArrayList<Object>) ?

Is that OK, or any Exception occurs?

Does ObjectOutputStream automatically split the list to a few segments before sending the packets? Or it doesn't support?

Is there another way to send a large size of an object?

As for ObjectInputStream, can I just call readObject() to receive the large size of List<Object>/ArrayList<Object> ?

Thanks in advance.

->

Another questions: Does ObjectInputStream receive anything when the list is not fully sent from the sender? I want to close the socket but the ObjectOutputStream is still sending the list. Does it shutdown immediately when I close socket, and the list segments are destroyed?

Is that OK, or any Exception occurs? It's ok.

Does ObjectOutputStream automatically split the list to a few segments

Yes.

before sending the packets? Or it doesn't support?

The underlying socket has no control over how packets are sent. Nor should you need it.

Is there another way to send a large size of an object?

Many but 100 isn't very large. If you had a few million of large objects it might be worth it. If you have a few billion you definitively need to consider alternatives. There is lots of alternatives but I wouldn't bother unless you need it.

As for ObjectInputStream, can I just call readObject() to receive the large size of List/ArrayList?

Yes, that is what it is for.

should I just call writeObject(List<Object>/ArrayList<Object>)?

Yes.

Is that OK, or any Exception occurs?

It is OK, but exceptions can always occur, especially over a network.

Does ObjectOutputStream automatically split the list to a few segments before sending the packets? Or it doesn't support?

Yes, and TCP does as well, and IP.

Is there another way to send a large size of an object?

Why? You have this way. You don't need another way.

As for ObjectInputStream, can I just call readObject() to receive the large size of List<Object>/ArrayList<Object>?

Yes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM