Search in sources :

Example 61 with PipedInputStream

use of java.io.PipedInputStream in project gocd by gocd.

the class StreamPumperTest method shouldNotHaveExpiredTimeoutWhenCompleted.

@Test
public void shouldNotHaveExpiredTimeoutWhenCompleted() throws Exception {
    PipedOutputStream output = new PipedOutputStream();
    InputStream inputStream = new PipedInputStream(output);
    TestingClock clock = new TestingClock();
    StreamPumper pumper = new StreamPumper(inputStream, new TestConsumer(), "", "utf-8", clock);
    new Thread(pumper).start();
    output.write("line1\n".getBytes());
    output.flush();
    output.close();
    pumper.readToEnd();
    clock.addSeconds(2);
    assertThat(pumper.didTimeout(1L, TimeUnit.SECONDS), is(false));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PipedInputStream(java.io.PipedInputStream) InputStream(java.io.InputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) TestingClock(com.thoughtworks.go.util.TestingClock) Test(org.junit.Test)

Example 62 with PipedInputStream

use of java.io.PipedInputStream in project JMRI by JMRI.

the class RfidStreamPortControllerTest method setUp.

// The minimal setup for log4J
@Override
@Before
public void setUp() {
    apps.tests.Log4JFixture.setUp();
    JUnitUtil.resetInstanceManager();
    try {
        PipedInputStream tempPipe;
        tempPipe = new PipedInputStream();
        DataOutputStream ostream = new DataOutputStream(new PipedOutputStream(tempPipe));
        tempPipe = new PipedInputStream();
        DataInputStream istream = new DataInputStream(tempPipe);
        apc = new RfidStreamPortController(istream, ostream, "Test");
    } catch (java.io.IOException ioe) {
        Assert.fail("IOException creating stream");
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) DataInputStream(java.io.DataInputStream) Before(org.junit.Before)

Example 63 with PipedInputStream

use of java.io.PipedInputStream in project JMRI by JMRI.

the class SprogCSStreamPortControllerTest method setUp.

// The minimal setup for log4J
@Before
@Override
public void setUp() {
    apps.tests.Log4JFixture.setUp();
    JUnitUtil.resetInstanceManager();
    try {
        PipedInputStream tempPipe;
        tempPipe = new PipedInputStream();
        DataOutputStream ostream = new DataOutputStream(new PipedOutputStream(tempPipe));
        tempPipe = new PipedInputStream();
        DataInputStream istream = new DataInputStream(tempPipe);
        apc = new SprogCSStreamPortController(istream, ostream, "Test");
    } catch (java.io.IOException ioe) {
        Assert.fail("IOException creating stream");
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) DataInputStream(java.io.DataInputStream) Before(org.junit.Before)

Example 64 with PipedInputStream

use of java.io.PipedInputStream in project gradle by gradle.

the class ForwardClientInput method execute.

public void execute(final DaemonCommandExecution execution) {
    final PipedOutputStream inputSource = new PipedOutputStream();
    final PipedInputStream replacementStdin;
    try {
        replacementStdin = new PipedInputStream(inputSource);
    } catch (IOException e) {
        throw UncheckedException.throwAsUncheckedException(e);
    }
    execution.getConnection().onStdin(new StdinHandler() {

        public void onInput(ForwardInput input) {
            LOGGER.debug("Writing forwarded input on daemon's stdin.");
            try {
                inputSource.write(input.getBytes());
            } catch (IOException e) {
                LOGGER.warn("Received exception trying to forward client input.", e);
            }
        }

        public void onEndOfInput() {
            LOGGER.info("Closing daemon's stdin at end of input.");
            try {
                inputSource.close();
            } catch (IOException e) {
                LOGGER.warn("Problem closing output stream connected to replacement stdin", e);
            } finally {
                LOGGER.info("The daemon will no longer process any standard input.");
            }
        }
    });
    try {
        try {
            new StdinSwapper().swap(replacementStdin, new Callable<Void>() {

                public Void call() {
                    execution.proceed();
                    return null;
                }
            });
        } finally {
            execution.getConnection().onStdin(null);
            IOUtils.closeQuietly(replacementStdin);
            IOUtils.closeQuietly(inputSource);
        }
    } catch (Exception e) {
        throw UncheckedException.throwAsUncheckedException(e);
    }
}
Also used : StdinHandler(org.gradle.launcher.daemon.server.api.StdinHandler) ForwardInput(org.gradle.launcher.daemon.protocol.ForwardInput) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) StdinSwapper(org.gradle.util.StdinSwapper) IOException(java.io.IOException) UncheckedException(org.gradle.internal.UncheckedException)

Example 65 with PipedInputStream

use of java.io.PipedInputStream in project zeppelin by apache.

the class PySparkInterpreter method createGatewayServerAndStartScript.

private void createGatewayServerAndStartScript() {
    // create python script
    createPythonScript();
    port = findRandomOpenPortOnAllLocalInterfaces();
    gatewayServer = new GatewayServer(this, port);
    gatewayServer.start();
    // Run python shell
    // Choose python in the order of
    // PYSPARK_DRIVER_PYTHON > PYSPARK_PYTHON > zeppelin.pyspark.python
    String pythonExec = getProperty("zeppelin.pyspark.python");
    if (System.getenv("PYSPARK_PYTHON") != null) {
        pythonExec = System.getenv("PYSPARK_PYTHON");
    }
    if (System.getenv("PYSPARK_DRIVER_PYTHON") != null) {
        pythonExec = System.getenv("PYSPARK_DRIVER_PYTHON");
    }
    CommandLine cmd = CommandLine.parse(pythonExec);
    cmd.addArgument(scriptPath, false);
    cmd.addArgument(Integer.toString(port), false);
    cmd.addArgument(Integer.toString(getSparkInterpreter().getSparkVersion().toNumber()), false);
    executor = new DefaultExecutor();
    outputStream = new InterpreterOutputStream(logger);
    PipedOutputStream ps = new PipedOutputStream();
    in = null;
    try {
        in = new PipedInputStream(ps);
    } catch (IOException e1) {
        throw new InterpreterException(e1);
    }
    ins = new BufferedWriter(new OutputStreamWriter(ps));
    input = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, outputStream, in);
    executor.setStreamHandler(streamHandler);
    executor.setWatchdog(new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT));
    try {
        Map env = setupPySparkEnv();
        executor.execute(cmd, env, this);
        pythonscriptRunning = true;
    } catch (IOException e) {
        throw new InterpreterException(e);
    }
    try {
        input.write("import sys, getopt\n".getBytes());
        ins.flush();
    } catch (IOException e) {
        throw new InterpreterException(e);
    }
}
Also used : DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) InterpreterOutputStream(org.apache.zeppelin.interpreter.util.InterpreterOutputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedWriter(java.io.BufferedWriter) CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) OutputStreamWriter(java.io.OutputStreamWriter) GatewayServer(py4j.GatewayServer) Map(java.util.Map)

Aggregations

PipedInputStream (java.io.PipedInputStream)128 PipedOutputStream (java.io.PipedOutputStream)121 IOException (java.io.IOException)42 Test (org.junit.Test)33 BinaryDecoder (co.cask.cdap.common.io.BinaryDecoder)21 BinaryEncoder (co.cask.cdap.common.io.BinaryEncoder)21 ReflectionDatumReader (co.cask.cdap.internal.io.ReflectionDatumReader)17 TypeToken (com.google.common.reflect.TypeToken)17 DataInputStream (java.io.DataInputStream)13 DataOutputStream (java.io.DataOutputStream)13 InputStream (java.io.InputStream)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 Before (org.junit.Before)10 OutputStream (java.io.OutputStream)9 PrintStream (java.io.PrintStream)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 InputStreamReader (java.io.InputStreamReader)6 ImmutableList (com.google.common.collect.ImmutableList)5 List (java.util.List)5 ISymmetricEngine (org.jumpmind.symmetric.ISymmetricEngine)5