2

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

devoured elysium
  • 101,373
  • 131
  • 340
  • 557

2 Answers2

2

One way to get around it to use streaming. Streaming approach does not load the entire contents in memory. You can open two streams, input-stream from the object to be sent over the network and output-stream for the TCP socket connection.

In java, both the InputStream and OutputStream. support reading and writing of bytes.

You can define a buffer size (a byte array). Read (from file) the content into that buffer using InputStream.read(byte[] b) and write (to TCP socket) the same buffer to output-stream using OutputStream.write(byte[] b)

Santosh
  • 17,667
  • 4
  • 54
  • 79
  • That is the obvious answer. But that would imply treating in a different way this message or the other kinds of messages, which is something I'm looking to avoid. – devoured elysium Feb 28 '12 at 10:30
  • why not just stream them all....you are writing a custom low level socket based app on the stack aren't you? Maybe the original TCP rfc woud be useful http://op.to/42Kb/plain/text/ietf+ – Cris Stringfellow Mar 01 '12 at 14:21
0

Instead of using Java Serilization, you can directly read the file in Java in a buffer and send that buffer over the network. Why involve java serialization in b/w and complicate stuff. Using simple n/w program, you will be able to send your files.

MoveFast
  • 3,011
  • 2
  • 27
  • 53