< prev index next >

src/jdk.jshell/share/classes/jdk/jshell/execution/PipeInputStream.java

Print this page
rev 3613 : imported patch 8131023

@@ -22,11 +22,13 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
 package jdk.jshell.execution;
 
+import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 
 /**
  *
  * @author Jan Lahoda
  */

@@ -37,11 +39,14 @@
     private int start;
     private int end;
     private boolean closed;
 
     @Override
-    public synchronized int read() {
+    public synchronized int read() throws IOException {
+        if (start == end) {
+            inputNeeded();
+        }
         while (start == end) {
             if (closed) {
                 return -1;
             }
             try {

@@ -55,11 +60,13 @@
         } finally {
             start = (start + 1) % buffer.length;
         }
     }
 
-    public synchronized void write(int b) {
+    protected void inputNeeded() throws IOException {}
+
+    private synchronized void write(int b) {
         if (closed) {
             throw new IllegalStateException("Already closed.");
         }
         int newEnd = (end + 1) % buffer.length;
         if (newEnd == start) {

@@ -83,6 +90,24 @@
     public synchronized void close() {
         closed = true;
         notifyAll();
     }
 
+    public OutputStream createOutput() {
+        return new OutputStream() {
+            @Override public void write(int b) throws IOException {
+                PipeInputStream.this.write(b);
+            }
+            @Override
+            public void write(byte[] b, int off, int len) throws IOException {
+                for (int i = 0 ; i < len ; i++) {
+                    write(Byte.toUnsignedInt(b[off + i]));
+                }
+            }
+            @Override
+            public void close() throws IOException {
+                PipeInputStream.this.close();
+            }
+        };
+    }
+
 }
< prev index next >