langtools/src/jdk.jshell/share/classes/jdk/jshell/execution/PipeInputStream.java
changeset 40767 c7908e8c786b
parent 39807 ba0ff343d241
child 41628 664e7664343d
equal deleted inserted replaced
40766:5e7d12c4fe70 40767:c7908e8c786b
    22  * or visit www.oracle.com if you need additional information or have any
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 package jdk.jshell.execution;
    25 package jdk.jshell.execution;
    26 
    26 
       
    27 import java.io.IOException;
    27 import java.io.InputStream;
    28 import java.io.InputStream;
       
    29 import java.io.OutputStream;
    28 
    30 
    29 /**
    31 /**
    30  *
    32  *
    31  * @author Jan Lahoda
    33  * @author Jan Lahoda
    32  */
    34  */
    37     private int start;
    39     private int start;
    38     private int end;
    40     private int end;
    39     private boolean closed;
    41     private boolean closed;
    40 
    42 
    41     @Override
    43     @Override
    42     public synchronized int read() {
    44     public synchronized int read() throws IOException {
       
    45         if (start == end) {
       
    46             inputNeeded();
       
    47         }
    43         while (start == end) {
    48         while (start == end) {
    44             if (closed) {
    49             if (closed) {
    45                 return -1;
    50                 return -1;
    46             }
    51             }
    47             try {
    52             try {
    55         } finally {
    60         } finally {
    56             start = (start + 1) % buffer.length;
    61             start = (start + 1) % buffer.length;
    57         }
    62         }
    58     }
    63     }
    59 
    64 
    60     public synchronized void write(int b) {
    65     protected void inputNeeded() throws IOException {}
       
    66 
       
    67     private synchronized void write(int b) {
    61         if (closed) {
    68         if (closed) {
    62             throw new IllegalStateException("Already closed.");
    69             throw new IllegalStateException("Already closed.");
    63         }
    70         }
    64         int newEnd = (end + 1) % buffer.length;
    71         int newEnd = (end + 1) % buffer.length;
    65         if (newEnd == start) {
    72         if (newEnd == start) {
    83     public synchronized void close() {
    90     public synchronized void close() {
    84         closed = true;
    91         closed = true;
    85         notifyAll();
    92         notifyAll();
    86     }
    93     }
    87 
    94 
       
    95     public OutputStream createOutput() {
       
    96         return new OutputStream() {
       
    97             @Override public void write(int b) throws IOException {
       
    98                 PipeInputStream.this.write(b);
       
    99             }
       
   100             @Override
       
   101             public void write(byte[] b, int off, int len) throws IOException {
       
   102                 for (int i = 0 ; i < len ; i++) {
       
   103                     write(Byte.toUnsignedInt(b[off + i]));
       
   104                 }
       
   105             }
       
   106             @Override
       
   107             public void close() throws IOException {
       
   108                 PipeInputStream.this.close();
       
   109             }
       
   110         };
       
   111     }
       
   112 
    88 }
   113 }