Search in sources :

Example 1 with PipedOutputStream

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

the class WarURLConnection method substitueManifest.

/**
     * Use PipedOuputStream and PipedInputStream to do the transformation without making
     * a new temporary file ust to replace the manifest.
     * @param newmanifest The new manifest
     * @param rawIn The file input stream or equivalent. not the jar input stream.
     */
public static InputStream substitueManifest(final Manifest newmanifest, final InputStream rawIn) throws IOException {
    final PipedOutputStream pOut = new PipedOutputStream();
    PipedInputStream pIn = new PipedInputStream(pOut);
    Runnable run = new Runnable() {

        public void run() {
            JarInputStream jin = null;
            JarOutputStream dest = null;
            try {
                jin = new JarInputStream(rawIn, false);
                dest = new JarOutputStream(pOut, newmanifest);
                ZipEntry next = jin.getNextEntry();
                while (next != null) {
                    if (next.getName().equalsIgnoreCase(JarFile.MANIFEST_NAME)) {
                        continue;
                    }
                    dest.putNextEntry(next);
                    if (next.getSize() > 0) {
                        IO.copy(jin, dest, next.getSize());
                    }
                    next = jin.getNextJarEntry();
                }
            } catch (IOException ioe) {
                ioe.printStackTrace();
            } finally {
                if (dest != null)
                    IO.close(dest);
                if (jin != null)
                    IO.close(jin);
                IO.close(pOut);
            }
        }
    };
    Thread th = new Thread(run);
    th.start();
    return pIn;
}
Also used : JarInputStream(java.util.jar.JarInputStream) ZipEntry(java.util.zip.ZipEntry) JarOutputStream(java.util.jar.JarOutputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException)

Example 2 with PipedOutputStream

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

the class ReadLineInputStreamTest method before.

@Before
public void before() throws Exception {
    _queue.clear();
    _pin = new PipedInputStream();
    _pout = new PipedOutputStream(_pin);
    _in = new ReadLineInputStream(_pin);
    _writer = new Thread() {

        @Override
        public void run() {
            try {
                OutputStream out = _pout;
                while (out != null) {
                    String s = _queue.poll(100, TimeUnit.MILLISECONDS);
                    if (s != null) {
                        if ("__CLOSE__".equals(s))
                            _pout.close();
                        else {
                            _pout.write(s.getBytes(StandardCharsets.UTF_8));
                            Thread.sleep(50);
                        }
                    }
                    out = _pout;
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                _writer = null;
            }
        }
    };
    _writer.start();
}
Also used : OutputStream(java.io.OutputStream) PipedOutputStream(java.io.PipedOutputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) Before(org.junit.Before)

Example 3 with PipedOutputStream

use of java.io.PipedOutputStream in project hadoop by apache.

the class TestTools method checkOutput.

private void checkOutput(String[] args, String pattern, PrintStream out, Class<?> clazz) {
    ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
    PrintStream oldOut = System.out;
    PrintStream oldErr = System.err;
    try {
        PipedOutputStream pipeOut = new PipedOutputStream();
        PipedInputStream pipeIn = new PipedInputStream(pipeOut, PIPE_BUFFER_SIZE);
        if (out == System.out) {
            System.setOut(new PrintStream(pipeOut));
        } else if (out == System.err) {
            System.setErr(new PrintStream(pipeOut));
        }
        if (clazz == DelegationTokenFetcher.class) {
            expectDelegationTokenFetcherExit(args);
        } else if (clazz == JMXGet.class) {
            expectJMXGetExit(args);
        } else if (clazz == DFSAdmin.class) {
            expectDfsAdminPrint(args);
        }
        pipeOut.close();
        ByteStreams.copy(pipeIn, outBytes);
        pipeIn.close();
        assertTrue(new String(outBytes.toByteArray()).contains(pattern));
    } catch (Exception ex) {
        fail("checkOutput error " + ex);
    } finally {
        System.setOut(oldOut);
        System.setErr(oldErr);
    }
}
Also used : PrintStream(java.io.PrintStream) PipedOutputStream(java.io.PipedOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PipedInputStream(java.io.PipedInputStream) JMXGet(org.apache.hadoop.hdfs.tools.JMXGet) ExitException(org.apache.hadoop.util.ExitUtil.ExitException)

Example 4 with PipedOutputStream

use of java.io.PipedOutputStream in project textdb by TextDB.

the class TextQLParserTest method string2InputStream.

/**
     * Create an InputStream that contains an given String.
     * @param s the string to be available in the resulting InputStream
     * @return an InputStream containing the string s
     */
private InputStream string2InputStream(String s) {
    try {
        //create a piped input stream to write to and a piped output stream to return as result
        PipedOutputStream pos = new PipedOutputStream();
        PipedInputStream pis = new PipedInputStream(pos);
        //print the string to the stream
        PrintStream ppos = new PrintStream(pos);
        ppos.print(s);
        ppos.close();
        //return the generated InputStream
        return pis;
    } catch (IOException e) {
        //return null if an IOException is thrown
        return null;
    }
}
Also used : PrintStream(java.io.PrintStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException)

Example 5 with PipedOutputStream

use of java.io.PipedOutputStream in project tesb-studio-se by Talend.

the class RuntimeConsoleUtil method loadConsole.

public static void loadConsole() {
    clearConsole();
    RuntimeClient client = new RuntimeClient();
    PipedInputStream pis = new PipedInputStream();
    PipedOutputStream pos = new PipedOutputStream();
    try {
        pos.connect(pis);
    } catch (IOException e) {
        ExceptionHandler.process(e);
    }
    client.setInputStream(pis);
    Thread consoleThread = new Thread("Runtime Console Input") {

        @Override
        public void run() {
            InputStream is = findConsole().getInputStream();
            int count = 0;
            byte[] bs = new byte[1024];
            try {
                while ((count = is.read(bs)) > 0) {
                    // remore duplicate \r\n for Windows
                    if (count > 1 && bs[count - 1] == 10 && bs[count - 2] == 13) {
                        count--;
                    }
                    pos.write(Arrays.copyOf(bs, count));
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    pos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    };
    consoleThread.start();
    Thread connectThread = new Thread("Runtime Console Connector") {

        @Override
        public void run() {
            IPreferenceStore store = ESBRunContainerPlugin.getDefault().getPreferenceStore();
            String etcLocation = store.getString(RunContainerPreferenceInitializer.P_ESB_RUNTIME_LOCATION);
            String host = store.getString(RunContainerPreferenceInitializer.P_ESB_RUNTIME_HOST);
            System.setProperty("karaf.etc", etcLocation + "/etc");
            String[] karafArgs = new String[] { "-h", host };
            try {
                client.connect(karafArgs);
            } catch (Exception e) {
                ExceptionHandler.process(e);
            }
        }
    };
    connectThread.start();
}
Also used : RuntimeClient(org.talend.designer.esb.runcontainer.ui.console.RuntimeClient) PipedInputStream(java.io.PipedInputStream) InputStream(java.io.InputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) IOException(java.io.IOException)

Aggregations

PipedOutputStream (java.io.PipedOutputStream)227 PipedInputStream (java.io.PipedInputStream)204 IOException (java.io.IOException)91 Test (org.junit.Test)55 InputStream (java.io.InputStream)28 OutputStream (java.io.OutputStream)24 BinaryDecoder (co.cask.cdap.common.io.BinaryDecoder)21 BinaryEncoder (co.cask.cdap.common.io.BinaryEncoder)21 PrintStream (java.io.PrintStream)21 ByteArrayOutputStream (java.io.ByteArrayOutputStream)19 ReflectionDatumReader (co.cask.cdap.internal.io.ReflectionDatumReader)17 TypeToken (com.google.common.reflect.TypeToken)17 InputStreamReader (java.io.InputStreamReader)16 DataInputStream (java.io.DataInputStream)14 DataOutputStream (java.io.DataOutputStream)14 BufferedReader (java.io.BufferedReader)13 Before (org.junit.Before)12 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ExecutorService (java.util.concurrent.ExecutorService)8 ArrayList (java.util.ArrayList)7