I think you need to work with a timeout for a "broken" connection, since tcp isn't a true stream anyway, instead it's a packet system.
Maybe you can solve the problem with reading incoming data as Lines. And you might ask for the file size before downloading it.
Maybe you can solve the problem with reading incoming data as Lines. And you might ask for the file size before downloading it.