private Thread readInThread;
private Thread writeOutThread;
- public SocketPipeMirror() throws IOException {
+ private final String id;
+
+ public SocketPipeMirror(String id) throws IOException {
+ this.id = id;
inPipe = Pipe.open();
outPipe = Pipe.open();
in = Channels.newInputStream(inPipe.source());
try {
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (!readInThread.isInterrupted() && channel.isConnected()) {
- if (channel.read(buffer) < 0)
+ if (channel.read(buffer) < 0) {
+ in.close();
break;
+ }
buffer.flip();
inPipe.sink().write(buffer);
buffer.rewind();
}
} catch (AsynchronousCloseException e) {
// ignore
- // TODO make it cleaner
} catch (IOException e) {
e.printStackTrace();
}
- }, "Read in");
+ }, "JShell read " + id);
readInThread.start();
writeOutThread = new Thread(() -> {
try {
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (!writeOutThread.isInterrupted() && channel.isConnected()) {
- if (outPipe.source().read(buffer) < 0)
+ if (outPipe.source().read(buffer) < 0) {
+ out.close();
break;
+ }
buffer.flip();
channel.write(buffer);
buffer.rewind();
} catch (IOException e) {
e.printStackTrace();
}
- }, "Write out");
+ }, "JShell write " + id);
writeOutThread.start();
}
@Override
public void close() throws IOException {
// TODO make it more robust
- readInThread.interrupt();
- writeOutThread.interrupt();
+// readInThread.interrupt();
+// writeOutThread.interrupt();
in.close();
out.close();
+ try {
+ readInThread.join();
+ } catch (InterruptedException e) {
+ // silent
+ }
+ try {
+ writeOutThread.join();
+ } catch (InterruptedException e) {
+ // silent
+ }
}
public InputStream getInputStream() {