langtools/src/jdk.jshell/share/classes/jdk/jshell/execution/PipeInputStream.java
author rfield
Wed, 08 Feb 2017 10:43:16 -0800
changeset 43758 868af3718a21
parent 41628 664e7664343d
permissions -rw-r--r--
8173845: JShell API: not patch compatible Reviewed-by: jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
     1
/*
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
     4
 *
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    10
 *
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    15
 * accompanied this code).
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    16
 *
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    20
 *
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    23
 * questions.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    24
 */
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    25
package jdk.jshell.execution;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    26
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39807
diff changeset
    27
import java.io.IOException;
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    28
import java.io.InputStream;
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39807
diff changeset
    29
import java.io.OutputStream;
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    30
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    31
/**
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    32
 *
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    33
 * @author Jan Lahoda
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    34
 */
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    35
class PipeInputStream extends InputStream {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    36
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    37
    private static final int INITIAL_SIZE = 128;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    38
    private int[] buffer = new int[INITIAL_SIZE];
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    39
    private int start;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    40
    private int end;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    41
    private boolean closed;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    42
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    43
    @Override
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39807
diff changeset
    44
    public synchronized int read() throws IOException {
41628
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    45
        if (start == end && !closed) {
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39807
diff changeset
    46
            inputNeeded();
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39807
diff changeset
    47
        }
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    48
        while (start == end) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    49
            if (closed) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    50
                return -1;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    51
            }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    52
            try {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    53
                wait();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    54
            } catch (InterruptedException ex) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    55
                //ignore
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    56
            }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    57
        }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    58
        try {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    59
            return buffer[start];
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    60
        } finally {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    61
            start = (start + 1) % buffer.length;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    62
        }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    63
    }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    64
41628
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    65
    @Override
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    66
    public synchronized int read(byte[] b, int off, int len) throws IOException {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    67
        if (b == null) {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    68
            throw new NullPointerException();
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    69
        } else if (off < 0 || len < 0 || len > b.length - off) {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    70
            throw new IndexOutOfBoundsException();
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    71
        } else if (len == 0) {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    72
            return 0;
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    73
        }
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    74
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    75
        int c = read();
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    76
        if (c == -1) {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    77
            return -1;
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    78
        }
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    79
        b[off] = (byte)c;
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    80
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    81
        int totalRead = 1;
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    82
        while (totalRead < len && start != end) {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    83
            int r = read();
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    84
            if (r == (-1))
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    85
                break;
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    86
            b[off + totalRead++] = (byte) r;
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    87
        }
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    88
        return totalRead;
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    89
    }
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    90
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39807
diff changeset
    91
    protected void inputNeeded() throws IOException {}
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39807
diff changeset
    92
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39807
diff changeset
    93
    private synchronized void write(int b) {
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    94
        if (closed) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    95
            throw new IllegalStateException("Already closed.");
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    96
        }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    97
        int newEnd = (end + 1) % buffer.length;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    98
        if (newEnd == start) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    99
            //overflow:
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   100
            int[] newBuffer = new int[buffer.length * 2];
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   101
            int rightPart = (end > start ? end : buffer.length) - start;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   102
            int leftPart = end > start ? 0 : start - 1;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   103
            System.arraycopy(buffer, start, newBuffer, 0, rightPart);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   104
            System.arraycopy(buffer, 0, newBuffer, rightPart, leftPart);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   105
            buffer = newBuffer;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   106
            start = 0;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   107
            end = rightPart + leftPart;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   108
            newEnd = end + 1;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   109
        }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   110
        buffer[end] = b;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   111
        end = newEnd;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   112
        notifyAll();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   113
    }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   114
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   115
    @Override
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   116
    public synchronized void close() {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   117
        closed = true;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   118
        notifyAll();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   119
    }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   120
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39807
diff changeset
   121
    public OutputStream createOutput() {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39807
diff changeset
   122
        return new OutputStream() {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39807
diff changeset
   123
            @Override public void write(int b) throws IOException {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39807
diff changeset
   124
                PipeInputStream.this.write(b);
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39807
diff changeset
   125
            }
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39807
diff changeset
   126
            @Override
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39807
diff changeset
   127
            public void write(byte[] b, int off, int len) throws IOException {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39807
diff changeset
   128
                for (int i = 0 ; i < len ; i++) {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39807
diff changeset
   129
                    write(Byte.toUnsignedInt(b[off + i]));
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39807
diff changeset
   130
                }
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39807
diff changeset
   131
            }
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39807
diff changeset
   132
            @Override
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39807
diff changeset
   133
            public void close() throws IOException {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39807
diff changeset
   134
                PipeInputStream.this.close();
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39807
diff changeset
   135
            }
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39807
diff changeset
   136
        };
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39807
diff changeset
   137
    }
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents: 39807
diff changeset
   138
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   139
}