vigna / fastutil

fastutil extends the Java™ Collections Framework by providing type-specific maps, sets, lists and queues.
Apache License 2.0
1.81k stars 199 forks source link

Fix FastBufferedOutputStream.close() exception #259

Closed stachenov closed 3 years ago

stachenov commented 3 years ago

If the output is closed before FastBufferedOutputStream is closed, then close() would throw because close() calls flush(), and it would try to write the buffer, even if there's nothing to write, and zero-length write() on a closed stream throws in some OutputStream implementations.

This can happen if the output is a socket, for example, that is managed separately from the output stream.

Fix by not calling write() from flush() at all if there is nothing to write.

If the buffer is not empty, then it would still try to write it, and would throw, which is probably correct.

vigna commented 3 years ago

OK, thanks for noting this!