I'm developing a client-server system in Java. I'm sending the messaeges over a TCP channel with serialized objects.
I will also need to send files. I could just define my own custom message class:
public class SendFile implements Serializable {
private byte[] fileContents;
...
}
and send it. This poses a problem if we have a large file, as from what I understand it will try to have everything in memory before sending the file to the other side.
I've read about Externalizable
but that won't be any better if the file is still being read to all my computer's memory before sending it over the TCP channel. I wanted some "lazy" way to do it (reading it from disk as I'm sending it over the network, and discarding from memory what was already read).
What's the cleanest way of handling this issue? In the worst case, I could just divide this into small chunks, but I'd like to avoid it if possible.
EDIT:
Of course I could just open a stream on the file and "send it". But that would imply treating sending a file and all the other messages in distinct ways, something I'm looking for to avoid.
Thanks