8267459: Pasting Unicode characters into JShell does not work.

Reviewed-by: vromero
diff --git a/src/jdk.internal.le/share/classes/jdk/internal/org/jline/utils/NonBlocking.java b/src/jdk.internal.le/share/classes/jdk/internal/org/jline/utils/NonBlocking.java
index 597e5f2..6d37a20 100644
--- a/src/jdk.internal.le/share/classes/jdk/internal/org/jline/utils/NonBlocking.java
+++ b/src/jdk.internal.le/share/classes/jdk/internal/org/jline/utils/NonBlocking.java
@@ -214,10 +214,27 @@
                     if (l < 0) {
                         return l;
                     } else {
-                        ByteBuffer bytes = ByteBuffer.wrap(buf, 0, l);
+                        ByteBuffer currentBytes;
+                        if (bytes.hasRemaining()) {
+                            int transfer = bytes.remaining();
+                            byte[] newBuf = new byte[l + transfer];
+                            bytes.get(newBuf, 0, transfer);
+                            System.arraycopy(buf, 0, newBuf, transfer, l);
+                            currentBytes = ByteBuffer.wrap(newBuf);
+                            bytes.position(0);
+                            bytes.limit(0);
+                        } else {
+                            currentBytes = ByteBuffer.wrap(buf, 0, l);
+                        }
                         CharBuffer chars = CharBuffer.wrap(b);
-                        decoder.decode(bytes, chars, false);
+                        decoder.decode(currentBytes, chars, false);
                         chars.flip();
+                        if (currentBytes.hasRemaining()) {
+                            int pos = bytes.position();
+                            bytes.limit(bytes.limit() + currentBytes.remaining());
+                            bytes.put(currentBytes);
+                            bytes.position(pos);
+                        }
                         return chars.remaining();
                     }
                 }
diff --git a/test/langtools/jdk/jshell/PasteAndMeasurementsUITest.java b/test/langtools/jdk/jshell/PasteAndMeasurementsUITest.java
index 932be4f..409965e 100644
--- a/test/langtools/jdk/jshell/PasteAndMeasurementsUITest.java
+++ b/test/langtools/jdk/jshell/PasteAndMeasurementsUITest.java
@@ -23,7 +23,7 @@
 
 /**
  * @test
- * @bug 8182297 8242919
+ * @bug 8182297 8242919 8267459
  * @summary Verify that pasting multi-line snippets works properly.
  * @library /tools/lib
  * @modules
@@ -90,4 +90,18 @@
             waitOutput(out,       "int i;");
         });
     }
+
+    public void testBracketedPasteNonAscii() throws Exception {
+        Field cons = System.class.getDeclaredField("cons");
+        cons.setAccessible(true);
+        Constructor console = Console.class.getDeclaredConstructor();
+        console.setAccessible(true);
+        cons.set(null, console.newInstance());
+        doRunTest((inputSink, out) -> {
+            inputSink.write(LineReaderImpl.BRACKETED_PASTE_BEGIN +
+                            "int \u010d;" +
+                            LineReaderImpl.BRACKETED_PASTE_END);
+            waitOutput(out,       "int \uffc4\uff8d;"); //UTF-8 encoding of \u010d
+        });
+    }
 }
diff --git a/test/langtools/jdk/jshell/UITesting.java b/test/langtools/jdk/jshell/UITesting.java
index 71200cc..848c0d5 100644
--- a/test/langtools/jdk/jshell/UITesting.java
+++ b/test/langtools/jdk/jshell/UITesting.java
@@ -28,6 +28,7 @@
 import java.io.PrintStream;
 import java.io.Writer;
 import java.lang.reflect.Field;
+import java.nio.charset.StandardCharsets;
 import java.text.MessageFormat;
 import java.util.HashMap;
 import java.util.Locale;
@@ -98,7 +99,7 @@
             }
         });
 
-        Writer inputSink = new OutputStreamWriter(input.createOutput()) {
+        Writer inputSink = new OutputStreamWriter(input.createOutput(), StandardCharsets.UTF_8) {
             @Override
             public void write(String str) throws IOException {
                 super.write(str);