You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by gn...@apache.org on 2012/10/02 01:26:31 UTC

svn commit: r1392690 - /karaf/branches/karaf-2.2.x/client/src/main/java/org/apache/karaf/client/Main.java

Author: gnodet
Date: Mon Oct  1 23:26:31 2012
New Revision: 1392690

URL: http://svn.apache.org/viewvc?rev=1392690&view=rev
Log:
[KARAF-1814] Cursor keys do not work when using bin/client on windows

Modified:
    karaf/branches/karaf-2.2.x/client/src/main/java/org/apache/karaf/client/Main.java

Modified: karaf/branches/karaf-2.2.x/client/src/main/java/org/apache/karaf/client/Main.java
URL: http://svn.apache.org/viewvc/karaf/branches/karaf-2.2.x/client/src/main/java/org/apache/karaf/client/Main.java?rev=1392690&r1=1392689&r2=1392690&view=diff
==============================================================================
--- karaf/branches/karaf-2.2.x/client/src/main/java/org/apache/karaf/client/Main.java (original)
+++ karaf/branches/karaf-2.2.x/client/src/main/java/org/apache/karaf/client/Main.java Mon Oct  1 23:26:31 2012
@@ -16,8 +16,12 @@
  */
 package org.apache.karaf.client;
 
-import java.io.BufferedInputStream;
 import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InterruptedIOException;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
 
 import jline.Terminal;
 import org.apache.karaf.shell.console.jline.TerminalFactory;
@@ -27,8 +31,6 @@ import org.apache.sshd.SshClient;
 import org.apache.sshd.client.channel.ChannelShell;
 import org.apache.sshd.client.future.ConnectFuture;
 import org.apache.sshd.common.RuntimeSshException;
-
-import org.apache.sshd.common.util.NoCloseInputStream;
 import org.fusesource.jansi.AnsiConsole;
 import org.slf4j.impl.SimpleLogger;
 
@@ -119,7 +121,9 @@ public class Main {
 			} else {
                 terminal = new TerminalFactory().getTerminal();
  				channel = session.createChannel("shell");
-                channel.setIn(new NoCloseInputStream(System.in));
+                ConsoleInputStream in = new ConsoleInputStream(terminal.wrapInIfNeeded(System.in));
+                new Thread(in).start();
+                channel.setIn(in);
                 ((ChannelShell) channel).setupSensibleDefaultPty();
             }
             channel.setOut(AnsiConsole.wrapOutputStream(System.out));
@@ -146,4 +150,98 @@ public class Main {
         System.exit(0);
     }
 
+
+    private static class ConsoleInputStream extends InputStream implements Runnable {
+
+        private InputStream in;
+        private boolean eof = false;
+        private final BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(1024);
+
+        public ConsoleInputStream(InputStream in) {
+            this.in = in;
+        }
+
+        private int read(boolean wait) throws IOException
+        {
+            if (eof && queue.isEmpty()) {
+                return -1;
+            }
+            Integer i;
+            if (wait) {
+                try {
+                    i = queue.take();
+                } catch (InterruptedException e) {
+                    throw new InterruptedIOException();
+                }
+            } else {
+                i = queue.poll();
+            }
+            if (i == null) {
+                return -1;
+            }
+            return i;
+        }
+
+        @Override
+        public int read() throws IOException
+        {
+            return read(true);
+        }
+
+        @Override
+        public int read(byte b[], int off, int len) throws IOException
+        {
+            if (b == null) {
+                throw new NullPointerException();
+            } else if (off < 0 || len < 0 || len > b.length - off) {
+                throw new IndexOutOfBoundsException();
+            } else if (len == 0) {
+                return 0;
+            }
+
+            int nb = 1;
+            int i = read(true);
+            if (i < 0) {
+                return -1;
+            }
+            b[off++] = (byte) i;
+            while (nb < len) {
+                i = read(false);
+                if (i < 0) {
+                    return nb;
+                }
+                b[off++] = (byte) i;
+                nb++;
+            }
+            return nb;
+        }
+
+        @Override
+        public int available() throws IOException {
+            return queue.size();
+        }
+
+        public void run() {
+            try {
+                while (true) {
+                    try {
+                        int c = in.read();
+                        if (c == -1) {
+                            return;
+                        }
+                        queue.put(c);
+                    } catch (Throwable t) {
+                        return;
+                    }
+                }
+            } finally {
+                eof = true;
+                try {
+                    queue.put(-1);
+                } catch (InterruptedException e) {
+                }
+            }
+        }
+    }
+
 }