java - What is the elegant way to handle interruptions on IO? -
i looking @ implementation of ioutils.copy() apache commons @ http://www.docjar.com/html/api/org/apache/commons/io/ioutils.java.html , boils down to:
public static long copylarge(inputstream input, outputstream output) throws ioexception { // default_buffer_size 4096 byte[] buffer = new byte[default_buffer_size]; long count = 0; int n = 0; while (-1 != (n = input.read(buffer))) { output.write(buffer, 0, n); count += n; } return count; } i running in executor, task has timeout etc. if understand correctly if times out , future cancelled, thread keeps on running because there no check thread status anywhere in loop. leads dangerous leak , starvation.
seems need rewrite loop, sane , correct way handle this? throw new ioexception(new interruptedexception())? declare method throwing interruptedexception , throw (hate io helper methods)?
edit: checked bytestreams guava , seem doing same thing. i'm wondering why 2 major libraries don't support interruptions in such loop. missing something?
if close input and/or output, copylarge method throw exception, exiting loop.
Comments
Post a Comment