diff options
author | Sven Gothel <[email protected]> | 2015-09-21 07:30:03 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2015-09-21 07:30:03 +0200 |
commit | 6c45f1dbeb875790056aef424b91b54440790a2b (patch) | |
tree | 4f20b20afbab309a76fae0d9e61740483a3f6211 /src | |
parent | a8db919494e934f768ee8adb0d0bad75fa390e62 (diff) |
Bug 1219: Fix IOUtil.StreamMonitor EOS handling
- Make StreamMonitor a daemon thread, i.e. not hindering VM from exit
- Earmark each InputStream's EOS state
and only attempt to readByte if !eos
- End loop and hence the thread if all InputStream
have reached EOS.
- Don't close the InputStream.
Closing the InputStream is expected to be done
by the owner, otherwise no EOS could even be reached!
- Flush the output PrintStream at thread exit
Diffstat (limited to 'src')
-rw-r--r-- | src/java/com/jogamp/common/util/IOUtil.java | 42 |
1 files changed, 25 insertions, 17 deletions
diff --git a/src/java/com/jogamp/common/util/IOUtil.java b/src/java/com/jogamp/common/util/IOUtil.java index 9bbb09b..62144fc 100644 --- a/src/java/com/jogamp/common/util/IOUtil.java +++ b/src/java/com/jogamp/common/util/IOUtil.java @@ -832,44 +832,52 @@ public class IOUtil { public static class StreamMonitor implements Runnable { private final InputStream[] istreams; + private final boolean[] eos; private final PrintStream ostream; private final String prefix; public StreamMonitor(final InputStream[] streams, final PrintStream ostream, final String prefix) { this.istreams = streams; + this.eos = new boolean[streams.length]; this.ostream = ostream; this.prefix = prefix; - new InterruptSource.Thread(null, this, "StreamMonitor-"+Thread.currentThread().getName()).start(); + final InterruptSource.Thread t = new InterruptSource.Thread(null, this, "StreamMonitor-"+Thread.currentThread().getName()); + t.setDaemon(true); + t.start(); } + @Override public void run() { final byte[] buffer = new byte[4096]; try { - int numRead; + final int streamCount = istreams.length; + int eosCount = 0; do { - numRead = 0; for(int i=0; i<istreams.length; i++) { - final int numReadI = istreams[i].read(buffer); - if (numReadI > 0) { - if( null != ostream ) { - if( null != prefix ) { - ostream.write(prefix.getBytes()); + if( !eos[i] ) { + final int numReadI = istreams[i].read(buffer); + if (numReadI > 0) { + if( null != ostream ) { + if( null != prefix ) { + ostream.write(prefix.getBytes()); + } + ostream.write(buffer, 0, numReadI); } - ostream.write(buffer, 0, numReadI); + } else { + // numReadI == -1 + eosCount++; + eos[i] = true; } - numRead += numReadI; } } if( null != ostream ) { ostream.flush(); } - } while (numRead >= 0); - } - catch (final IOException e) { - for(int i=0; i<istreams.length; i++) { - try { - istreams[i].close(); - } catch (final IOException e2) { } + } while ( eosCount < streamCount ); + } catch (final IOException e) { + } finally { + if( null != ostream ) { + ostream.flush(); } // Should allow clean exit when process shuts down } |