Search in sources :

Example 1 with DisconnectableInputStream

use of org.gradle.util.internal.DisconnectableInputStream in project gradle by gradle.

the class InputForwarder method start.

public InputForwarder start() {
    lifecycleLock.lock();
    try {
        if (started) {
            throw new IllegalStateException("input forwarder has already been started");
        }
        disconnectableInput = new DisconnectableInputStream(input, bufferSize);
        outputBuffer = new LineBufferingOutputStream(handler, SystemProperties.getInstance().getLineSeparator(), bufferSize);
        forwardingExecuter = executorFactory.create("Forward input");
        forwardingExecuter.execute(new Runnable() {

            @Override
            public void run() {
                byte[] buffer = new byte[bufferSize];
                int readCount;
                Throwable readFailure = null;
                try {
                    while (true) {
                        try {
                            readCount = disconnectableInput.read(buffer, 0, bufferSize);
                            if (readCount < 0) {
                                break;
                            }
                        } catch (AsynchronousCloseException e) {
                            break;
                        } catch (IOException e) {
                            readFailure = e;
                            break;
                        }
                        outputBuffer.write(buffer, 0, readCount);
                    }
                    // will flush any unterminated lines out synchronously
                    outputBuffer.flush();
                } catch (IOException e) {
                    // should not happen
                    throw UncheckedException.throwAsUncheckedException(e);
                } finally {
                    handler.endOfStream(readFailure);
                }
            }
        });
        started = true;
    } finally {
        lifecycleLock.unlock();
    }
    return this;
}
Also used : DisconnectableInputStream(org.gradle.util.internal.DisconnectableInputStream) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) LineBufferingOutputStream(org.gradle.internal.io.LineBufferingOutputStream) IOException(java.io.IOException)

Example 2 with DisconnectableInputStream

use of org.gradle.util.internal.DisconnectableInputStream in project gradle by gradle.

the class ContinuousBuildActionExecutor method createCancellableOperationManager.

private CancellableOperationManager createCancellableOperationManager(BuildRequestMetaData requestContext, BuildCancellationToken cancellationToken) {
    final CancellableOperationManager cancellableOperationManager;
    if (requestContext.isInteractive()) {
        if (!(System.in instanceof DisconnectableInputStream)) {
            System.setIn(new DisconnectableInputStream(System.in));
        }
        DisconnectableInputStream inputStream = (DisconnectableInputStream) System.in;
        cancellableOperationManager = new DefaultCancellableOperationManager(executorFactory.create("Cancel signal monitor"), inputStream, cancellationToken);
    } else {
        cancellableOperationManager = new PassThruCancellableOperationManager(cancellationToken);
    }
    return cancellableOperationManager;
}
Also used : DisconnectableInputStream(org.gradle.util.internal.DisconnectableInputStream) DefaultCancellableOperationManager(org.gradle.execution.DefaultCancellableOperationManager) DefaultCancellableOperationManager(org.gradle.execution.DefaultCancellableOperationManager) PassThruCancellableOperationManager(org.gradle.execution.PassThruCancellableOperationManager) CancellableOperationManager(org.gradle.execution.CancellableOperationManager) PassThruCancellableOperationManager(org.gradle.execution.PassThruCancellableOperationManager)

Example 3 with DisconnectableInputStream

use of org.gradle.util.internal.DisconnectableInputStream in project gradle by gradle.

the class ForwardStdinStreamsHandler method connectStreams.

@Override
public void connectStreams(Process process, String processName, Executor executor) {
    this.executor = executor;
    /*
            There's a potential problem here in that DisconnectableInputStream reads from input in the background.
            This won't automatically stop when the process is over. Therefore, if input is not closed then this thread
            will run forever. It would be better to ensure that this thread stops when the process does.
         */
    InputStream instr = new DisconnectableInputStream(input);
    standardInputWriter = new ExecOutputHandleRunner("write standard input to " + processName, instr, process.getOutputStream(), completed);
}
Also used : DisconnectableInputStream(org.gradle.util.internal.DisconnectableInputStream) DisconnectableInputStream(org.gradle.util.internal.DisconnectableInputStream) InputStream(java.io.InputStream)

Aggregations

DisconnectableInputStream (org.gradle.util.internal.DisconnectableInputStream)3 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 AsynchronousCloseException (java.nio.channels.AsynchronousCloseException)1 CancellableOperationManager (org.gradle.execution.CancellableOperationManager)1 DefaultCancellableOperationManager (org.gradle.execution.DefaultCancellableOperationManager)1 PassThruCancellableOperationManager (org.gradle.execution.PassThruCancellableOperationManager)1 LineBufferingOutputStream (org.gradle.internal.io.LineBufferingOutputStream)1