[FELIX-5821][gogo][runtime] Hitting Ctrl+C may kill the console output thread
*/
package org.apache.felix.gogo.runtime;
-import java.io.ByteArrayOutputStream;
-import java.io.EOFException;
-import java.io.PipedInputStream;
-import java.io.PipedOutputStream;
+import java.io.*;
+import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.Channels;
+import java.nio.channels.ClosedChannelException;
+import java.nio.channels.WritableByteChannel;
+import java.nio.channels.spi.AbstractInterruptibleChannel;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
@@ -228,7 +229,11 @@ public class Closure implements Function, Evaluate
streams = Pipe.getCurrentPipe().streams.clone();
} else {
streams = new Channel[10];
- System.arraycopy(session.channels, 0, streams, 0, 3);
+ streams[0] = session.channels[0];
+ streams[1] = new WritableByteChannelImpl((WritableByteChannel) session.channels[1]);
+ streams[2] = session.channels[1] == session.channels[2]
+ ? streams[1]
+ : new WritableByteChannelImpl((WritableByteChannel) session.channels[2]);
}
if (capturingOutput != null) {
streams[1] = capturingOutput;
@@ -304,6 +309,30 @@ public class Closure implements Function, Evaluate
return last == null ? null : last.result;
}
+ private static class WritableByteChannelImpl extends AbstractInterruptibleChannel
+ implements WritableByteChannel {
+ private final WritableByteChannel out;
+
+ WritableByteChannelImpl(WritableByteChannel out) {
+ this.out = out;
+ }
+
+ public int write(ByteBuffer src) throws IOException {
+ if (!isOpen()) {
+ throw new ClosedChannelException();
+ }
+ begin();
+ try {
+ return out.write(src);
+ } finally {
+ end(true);
+ }
+ }
+
+ protected void implCloseChannel() {
+ }
+ }
+
static Object eval(Object v)
{
String s = v.toString();
