I hope this is a straight forward design question.
Context:
I may be downloading one to many files over a socket connection. I am being passed byte[]s as they are read from the socket. I also know which file to write those bytes to. I am appending these bytes to the file with a FileOutputStream. I am also informed when all bytes for a file have been received.
Question:
Is it better to:
- keep a
FileOutputStreamopen until allbytes have been received and written - open a new
FileOutputStreamthat appends thebytes to the proper file as they’re received and then closes each time.
2 feels safer to me because I will close the stream after each write in case something goes wrong (like I stop getting bytes for some reason) with any of the downloads. But it also seems not very efficient. I’m having trouble finding out how expensive opening and closing FileOutputStreams is. Are there any other side-effects to keeping the FileOutputStream open besides the extra care needed for knowing when to close it?
Thanks in advance.
I agree with you that 2 is safer, but also much more expensive than 1.
Whenever you create a FileOutputStream, it calls its native-implemented open() method. The implementation of this method will end up creating a file handle and issuing and open() system call to the operating system. This will involve at least synchronization (file creation at OS level is usually atomic) and probably some buffer allocation, thus it will be much more expensive than keeping the file handle open.
On the other hand, if you use 1, you have the risk of mantaining too many open file handles and reaching the limit (which depends on the underlying operating system). And also of not closing the file if you don’t control all error paths.
I would suggest evaluating how many open files you will have open in parallel, how difficult it would be to control all error paths to close the file, and the throughput you need for your server. Depending on the outcome of such analysis, I would go for 1 or 2.
Additionally, if you use 1, you could implement some safe net, like a watchdog closing files which have not been written for some timeout.