I have a large file that needs to be transferred over socket from server to client. If I want to use writeObject
in ObjectOutputStream
to send the object, what's the normal way to do it? How about reading at the client side?
Asked
Active
Viewed 263 times
-1
-
You will call ObjectInputStream.readObject () in client. – Ravindra babu Nov 21 '15 at 01:28
-
http://stackoverflow.com/questions/21073024/receive-an-object-over-tcp-ip – Ravindra babu Nov 21 '15 at 01:33
-
If you want to use `writeObject()`, you use `writeObject()`. What's the question? – user207421 Nov 21 '15 at 01:37
-
My question is how the large data is wrapped into a object that can be transferred by writeObject(). In other words, do I need to break the data into different segments and then wrap them separately into objects? – Ting W Nov 21 '15 at 17:41
2 Answers
0
Be careful with Object Streams, they have the bad side effect to store and keep the Objects in memory to prevent double instances. Close it as soon as you are done with it.
If the object is large, you can chain your stream with a compressor, there are many good compression Objects in java like DeflatorOutputStream
and InflatorInputStream
.
For a quick tutorial on how to do it, check this : https://docs.oracle.com/javase/tutorial/essential/io/objectstreams.html

Guillaume F.
- 5,905
- 2
- 31
- 59
-
-
He didn't ask about (a) appropriateness/pitfalls of using object streams or (b) compression. That merely leaves your link, which makes the relevant parts of this a link-only answer. – user207421 Nov 24 '15 at 00:05
-1
do I need to break the data into different segments ... ?
No. Just call writeObject()
at one end and readObject()
at the other. TCP will sort out all the segmentation for you.

user207421
- 305,947
- 44
- 307
- 483
-
@downvoter OK, which is it? You don't call `readObject()`? `writeObject()`? TCP won't sort out all the segmentation for you? Information please. – user207421 Nov 24 '15 at 09:29