Search in sources :

Example 66 with PipedInputStream

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

the class PythonInterpreter method createGatewayServerAndStartScript.

private void createGatewayServerAndStartScript() throws UnknownHostException {
    createPythonScript();
    if (System.getenv("ZEPPELIN_HOME") != null) {
        py4jLibPath = System.getenv("ZEPPELIN_HOME") + File.separator + ZEPPELIN_PY4JPATH;
        pythonLibPath = System.getenv("ZEPPELIN_HOME") + File.separator + ZEPPELIN_PYTHON_LIBS;
    } else {
        Path workingPath = Paths.get("..").toAbsolutePath();
        py4jLibPath = workingPath + File.separator + ZEPPELIN_PY4JPATH;
        pythonLibPath = workingPath + File.separator + ZEPPELIN_PYTHON_LIBS;
    }
    port = findRandomOpenPortOnAllLocalInterfaces();
    gatewayServer = new GatewayServer(this, port, GatewayServer.DEFAULT_PYTHON_PORT, InetAddress.getByName("0.0.0.0"), InetAddress.getByName("0.0.0.0"), GatewayServer.DEFAULT_CONNECT_TIMEOUT, GatewayServer.DEFAULT_READ_TIMEOUT, (List) null);
    gatewayServer.start();
    // Run python shell
    String pythonCmd = getPythonCommand();
    CommandLine cmd = CommandLine.parse(pythonCmd);
    if (!pythonCmd.endsWith(".py")) {
        // PythonDockerInterpreter set pythoncmd with script
        cmd.addArgument(getScriptPath(), false);
    }
    cmd.addArgument(Integer.toString(port), false);
    cmd.addArgument(getLocalIp(), 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 = EnvironmentUtils.getProcEnvironment();
        if (!env.containsKey("PYTHONPATH")) {
            env.put("PYTHONPATH", py4jLibPath + File.pathSeparator + pythonLibPath);
        } else {
            env.put("PYTHONPATH", env.get("PYTHONPATH") + File.pathSeparator + py4jLibPath + File.pathSeparator + pythonLibPath);
        }
        logger.info("cmd = {}", cmd.toString());
        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 : Path(java.nio.file.Path) 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) List(java.util.List) OutputStreamWriter(java.io.OutputStreamWriter) GatewayServer(py4j.GatewayServer) Map(java.util.Map)

Example 67 with PipedInputStream

use of java.io.PipedInputStream in project jetty.project by eclipse.

the class HttpTesterTest method testResponsesSplitInput.

@Test
public void testResponsesSplitInput() throws Exception {
    PipedOutputStream src = new PipedOutputStream();
    PipedInputStream stream = new PipedInputStream(src) {

        @Override
        public synchronized int read(byte[] b, int off, int len) throws IOException {
            if (available() == 0)
                return 0;
            return super.read(b, off, len);
        }
    };
    HttpTester.Input in = HttpTester.from(stream);
    src.write(("HTTP/1.1 200 OK\r\n" + "Header: value\r\n" + "Transfer-Encoding: chunked\r\n" + "\r\n" + "A\r\n" + "0123456789\r\n" + "6\r\n" + "ABC").getBytes(StandardCharsets.ISO_8859_1));
    HttpTester.Response response = HttpTester.parseResponse(in);
    assertThat(response, nullValue());
    src.write(("DEF\r\n" + "0\r\n" + "\r\n" + "HTTP/1.1 400 OK\r\n" + "Next: response\r\n" + "Content-Length: 16\r\n" + "\r\n" + "0123456789").getBytes(StandardCharsets.ISO_8859_1));
    response = HttpTester.parseResponse(in);
    assertThat(response.getVersion(), is(HttpVersion.HTTP_1_1));
    assertThat(response.getStatus(), is(200));
    assertThat(response.getReason(), is("OK"));
    assertThat(response.get("Header"), is("value"));
    assertThat(response.getContent(), is("0123456789ABCDEF"));
    response = HttpTester.parseResponse(in);
    assertThat(response, nullValue());
    src.write(("ABCDEF").getBytes(StandardCharsets.ISO_8859_1));
    response = HttpTester.parseResponse(in);
    assertThat(response.getVersion(), is(HttpVersion.HTTP_1_1));
    assertThat(response.getStatus(), is(400));
    assertThat(response.getReason(), is("OK"));
    assertThat(response.get("Next"), is("response"));
    assertThat(response.getContent(), is("0123456789ABCDEF"));
}
Also used : PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) Test(org.junit.Test)

Example 68 with PipedInputStream

use of java.io.PipedInputStream in project stetho by facebook.

the class GunzippingOutputStream method create.

public static GunzippingOutputStream create(OutputStream finalOut) throws IOException {
    PipedInputStream pipeIn = new PipedInputStream();
    PipedOutputStream pipeOut = new PipedOutputStream(pipeIn);
    Future<Void> copyFuture = sExecutor.submit(new GunzippingCallable(pipeIn, finalOut));
    return new GunzippingOutputStream(pipeOut, copyFuture);
}
Also used : PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream)

Example 69 with PipedInputStream

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

the class DeepCopier method copyParallel.

/**
	 * This conserves heap memory!!!!! Produce a deep copy of the given object.
	 * Serializes the object through a pipe between two threads. Recommended for
	 * very large objects. The current thread is used for serializing the
	 * original object in order to respect any synchronization the caller may
	 * have around it, and a new thread is used for deserializing the copy.
	 *
	 */
public static <T> T copyParallel(T original) {
    try {
        PipedOutputStream outputStream = new PipedOutputStream();
        PipedInputStream inputStream = new PipedInputStream(outputStream);
        ObjectOutputStream ois = new ObjectOutputStream(outputStream);
        Receiver<T> receiver = new Receiver<T>(inputStream);
        try {
            ois.writeObject(original);
        } finally {
            ois.close();
        }
        return receiver.getResult();
    } catch (IOException iox) {
        // doesn't seem likely to happen as these streams are in memory
        throw new RuntimeException(iox);
    }
}
Also used : PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream)

Example 70 with PipedInputStream

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

the class PrimitiveDataTypeTest method setup.

@Before
public void setup() throws Exception {
    in = new PipedInputStream(1000);
    out = new PipedOutputStream(in);
    mIn = new DataInputViewStreamWrapper(in);
    mOut = new DataOutputViewStreamWrapper(out);
}
Also used : DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) Before(org.junit.Before)

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