< prev index next >

src/jdk.jshell/share/classes/jdk/internal/jshell/tool/ConsoleIOContext.java

Print this page
rev 3613 : imported patch 8131023

@@ -55,10 +55,12 @@
 import jdk.internal.jline.WindowsTerminal;
 import jdk.internal.jline.console.ConsoleReader;
 import jdk.internal.jline.console.KeyMap;
 import jdk.internal.jline.console.UserInterruptException;
 import jdk.internal.jline.console.completer.Completer;
+import jdk.internal.jline.console.history.History;
+import jdk.internal.jline.console.history.MemoryHistory;
 import jdk.internal.jline.extra.EditingHistory;
 import jdk.internal.jshell.tool.StopDetectingInputStream.State;
 
 class ConsoleIOContext extends IOContext {
 

@@ -66,10 +68,11 @@
 
     final JShellTool repl;
     final StopDetectingInputStream input;
     final ConsoleReader in;
     final EditingHistory history;
+    final MemoryHistory userInputHistory = new MemoryHistory();
 
     String prefix = "";
 
     ConsoleIOContext(JShellTool repl, InputStream cmdin, PrintStream cmdout) throws Exception {
         this.repl = repl;

@@ -297,10 +300,13 @@
             throw new IllegalStateException(ex);
         }
     }
 
     public void beforeUserCode() {
+        synchronized (this) {
+            inputBytes = null;
+        }
         input.setState(State.BUFFER);
     }
 
     public void afterUserCode() {
         input.setState(State.WAIT);

@@ -378,10 +384,40 @@
         } catch (IOException ex) {
             ex.printStackTrace();
         }
     }
 
+    private byte[] inputBytes;
+    private int inputBytesPointer;
+
+    @Override
+    public synchronized int readUserInput() {
+        while (inputBytes == null || inputBytes.length <= inputBytesPointer) {
+            boolean prevHandleUserInterrupt = in.getHandleUserInterrupt();
+            History prevHistory = in.getHistory();
+
+            try {
+                input.setState(State.WAIT);
+                in.setHandleUserInterrupt(true);
+                in.setHistory(userInputHistory);
+                inputBytes = (in.readLine("") + System.getProperty("line.separator")).getBytes();
+                inputBytesPointer = 0;
+            } catch (IOException ex) {
+                ex.printStackTrace();
+                return -1;
+            } catch (UserInterruptException ex) {
+                repl.state.stop();
+                return -1;
+            } finally {
+                in.setHistory(prevHistory);
+                in.setHandleUserInterrupt(prevHandleUserInterrupt);
+                input.setState(State.BUFFER);
+            }
+        }
+        return inputBytes[inputBytesPointer++];
+    }
+
     /**
      * A possible action which the user can choose to perform.
      */
     public interface Fix {
         /**
< prev index next >