Search in sources :

Example 31 with PipedOutputStream

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

the class SimulatorAdapter method openPort.

@Override
public String openPort(String portName, String appName) {
    try {
        PipedOutputStream tempPipeI = new PipedOutputStream();
        pout = new DataOutputStream(tempPipeI);
        inpipe = new DataInputStream(new PipedInputStream(tempPipeI));
        PipedOutputStream tempPipeO = new PipedOutputStream();
        outpipe = new DataOutputStream(tempPipeO);
        pin = new DataInputStream(new PipedInputStream(tempPipeO));
    } catch (java.io.IOException e) {
        log.error("init (pipe): Exception: " + e.toString());
    }
    opened = true;
    // indicates OK return
    return null;
}
Also used : DataOutputStream(java.io.DataOutputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) DataInputStream(java.io.DataInputStream)

Example 32 with PipedOutputStream

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

the class ForkParserTest method testPoolSizeReached.

@Test
public void testPoolSizeReached() throws Exception {
    final ForkParser parser = new ForkParser(ForkParserTest.class.getClassLoader(), new ForkTestParser());
    try {
        final Semaphore barrier = new Semaphore(0);
        Thread[] threads = new Thread[parser.getPoolSize()];
        PipedOutputStream[] pipes = new PipedOutputStream[threads.length];
        final ParseContext context = new ParseContext();
        for (int i = 0; i < threads.length; i++) {
            final PipedInputStream input = new PipedInputStream() {

                @Override
                public synchronized int read() throws IOException {
                    barrier.release();
                    return super.read();
                }
            };
            pipes[i] = new PipedOutputStream(input);
            threads[i] = new Thread() {

                public void run() {
                    try {
                        ContentHandler o = new DefaultHandler();
                        parser.parse(input, o, new Metadata(), context);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            threads[i].start();
        }
        // Wait until all the background parsers have been started
        barrier.acquire(parser.getPoolSize());
        final ContentHandler o = new BodyContentHandler();
        Thread blocked = new Thread() {

            public void run() {
                try {
                    barrier.release();
                    InputStream stream = new ByteArrayInputStream(new byte[0]);
                    parser.parse(stream, o, new Metadata(), context);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        blocked.start();
        // Wait until the last thread is started, and then some to
        // make sure that it would have had a chance to start processing
        // data had it not been blocked.
        barrier.acquire();
        Thread.sleep(1000);
        assertEquals("", o.toString());
        for (int i = 0; i < threads.length; i++) {
            pipes[i].close();
            threads[i].join();
        }
        blocked.join();
        assertEquals("Hello, World!", o.toString().trim());
    } finally {
        parser.close();
    }
}
Also used : BodyContentHandler(org.apache.tika.sax.BodyContentHandler) ByteArrayInputStream(java.io.ByteArrayInputStream) PipedInputStream(java.io.PipedInputStream) InputStream(java.io.InputStream) Metadata(org.apache.tika.metadata.Metadata) PipedOutputStream(java.io.PipedOutputStream) Semaphore(java.util.concurrent.Semaphore) PipedInputStream(java.io.PipedInputStream) BodyContentHandler(org.apache.tika.sax.BodyContentHandler) ContentHandler(org.xml.sax.ContentHandler) IOException(java.io.IOException) TikaException(org.apache.tika.exception.TikaException) DefaultHandler(org.xml.sax.helpers.DefaultHandler) ByteArrayInputStream(java.io.ByteArrayInputStream) ParseContext(org.apache.tika.parser.ParseContext) Test(org.junit.Test)

Example 33 with PipedOutputStream

use of java.io.PipedOutputStream in project webservices-axiom by apache.

the class TestMTOMForwardStreaming method runTest.

@Override
protected void runTest() throws Throwable {
    DataSource ds1 = new TestDataSource('A', Runtime.getRuntime().maxMemory());
    DataSource ds2 = new TestDataSource('B', Runtime.getRuntime().maxMemory());
    // Programmatically create the original message
    SOAPFactory factory = metaFactory.getSOAP12Factory();
    final SOAPEnvelope orgEnvelope = factory.createSOAPEnvelope();
    SOAPBody orgBody = factory.createSOAPBody(orgEnvelope);
    OMElement orgBodyElement = factory.createOMElement("test", factory.createOMNamespace("urn:test", "p"), orgBody);
    OMElement orgData1 = factory.createOMElement("data", null, orgBodyElement);
    orgData1.addChild(factory.createOMText(new DataHandler(ds1), true));
    OMElement orgData2 = factory.createOMElement("data", null, orgBodyElement);
    orgData2.addChild(factory.createOMText(new DataHandler(ds2), true));
    final OMOutputFormat format = new OMOutputFormat();
    format.setDoOptimize(true);
    format.setSOAP11(false);
    final String contentType = format.getContentType();
    final PipedOutputStream pipe1Out = new PipedOutputStream();
    final PipedInputStream pipe1In = new PipedInputStream(pipe1Out);
    // Create the producer thread (simulating the client sending the MTOM message)
    Thread producerThread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                try {
                    orgEnvelope.serialize(pipe1Out, format);
                } finally {
                    pipe1Out.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    });
    producerThread.start();
    final PipedOutputStream pipe2Out = new PipedOutputStream();
    PipedInputStream pipe2In = new PipedInputStream(pipe2Out);
    // Create the forwarder thread (simulating the mediation engine that forwards the message)
    Thread forwarderThread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                try {
                    MultipartBody mb = MultipartBody.builder().setInputStream(pipe1In).setContentType(contentType).build();
                    SOAPEnvelope envelope = OMXMLBuilderFactory.createSOAPModelBuilder(metaFactory, mb).getSOAPEnvelope();
                    // the element is built. Therefore we need two different test executions.
                    if (buildSOAPPart) {
                        envelope.build();
                    }
                    // Usage of serializeAndConsume should enable streaming
                    envelope.serializeAndConsume(pipe2Out, format);
                } finally {
                    pipe2Out.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    });
    forwarderThread.start();
    try {
        MultipartBody mb = MultipartBody.builder().setInputStream(pipe2In).setContentType(contentType).build();
        SOAPEnvelope envelope = OMXMLBuilderFactory.createSOAPModelBuilder(metaFactory, mb).getSOAPEnvelope();
        OMElement bodyElement = envelope.getBody().getFirstElement();
        Iterator<OMElement> it = bodyElement.getChildElements();
        OMElement data1 = it.next();
        OMElement data2 = it.next();
        IOTestUtils.compareStreams(ds1.getInputStream(), ((PartDataHandler) ((OMText) data1.getFirstOMChild()).getDataHandler()).getPart().getInputStream(false));
        IOTestUtils.compareStreams(ds2.getInputStream(), ((PartDataHandler) ((OMText) data2.getFirstOMChild()).getDataHandler()).getPart().getInputStream(false));
    } finally {
        pipe2In.close();
    }
}
Also used : TestDataSource(org.apache.axiom.testutils.activation.TestDataSource) OMElement(org.apache.axiom.om.OMElement) PipedOutputStream(java.io.PipedOutputStream) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) DataHandler(javax.activation.DataHandler) PartDataHandler(org.apache.axiom.mime.PartDataHandler) PipedInputStream(java.io.PipedInputStream) SOAPFactory(org.apache.axiom.soap.SOAPFactory) DataSource(javax.activation.DataSource) TestDataSource(org.apache.axiom.testutils.activation.TestDataSource) SOAPBody(org.apache.axiom.soap.SOAPBody) PartDataHandler(org.apache.axiom.mime.PartDataHandler) MultipartBody(org.apache.axiom.mime.MultipartBody) OMOutputFormat(org.apache.axiom.om.OMOutputFormat)

Example 34 with PipedOutputStream

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

the class LIUSBServerAdapter method connect.

@Override
public synchronized void connect() throws Exception {
    opened = false;
    if (log.isDebugEnabled()) {
        log.debug("connect called");
    }
    // open the port in XPressNet mode
    try {
        bcastAdapter = new BroadCastPortAdapter(this);
        commAdapter = new CommunicationPortAdapter(this);
        bcastAdapter.connect();
        commAdapter.connect();
        pout = commAdapter.getOutputStream();
        PipedOutputStream tempPipeO = new PipedOutputStream();
        outpipe = new DataOutputStream(tempPipeO);
        pin = new DataInputStream(new PipedInputStream(tempPipeO));
        opened = true;
    } catch (java.io.IOException e) {
        log.error("init (pipe): Exception: " + e.toString());
        ConnectionStatus.instance().setConnectionState(this.getSystemConnectionMemo().getUserName(), m_HostName, ConnectionStatus.CONNECTION_DOWN);
        // re-throw so this can be seen externally.
        throw e;
    } catch (Exception ex) {
        log.error("init (connect): Exception: " + ex.toString());
        ConnectionStatus.instance().setConnectionState(this.getSystemConnectionMemo().getUserName(), m_HostName, ConnectionStatus.CONNECTION_DOWN);
        // re-throw so this can be seen externally.
        throw ex;
    }
    keepAliveTimer();
    if (opened) {
        ConnectionStatus.instance().setConnectionState(this.getSystemConnectionMemo().getUserName(), m_HostName, ConnectionStatus.CONNECTION_UP);
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) PipedOutputStream(java.io.PipedOutputStream) IOException(java.io.IOException) PipedInputStream(java.io.PipedInputStream) DataInputStream(java.io.DataInputStream) IOException(java.io.IOException)

Example 35 with PipedOutputStream

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

the class SimulatorAdapter method openPort.

@Override
public String openPort(String portName, String appName) {
    try {
        PipedOutputStream tempPipeI = new PipedOutputStream();
        pout = new DataOutputStream(tempPipeI);
        inpipe = new DataInputStream(new PipedInputStream(tempPipeI));
        PipedOutputStream tempPipeO = new PipedOutputStream();
        outpipe = new DataOutputStream(tempPipeO);
        pin = new DataInputStream(new PipedInputStream(tempPipeO));
    } catch (java.io.IOException e) {
        log.error("init (pipe): Exception: " + e.toString());
    }
    opened = true;
    // indicates OK return
    return null;
}
Also used : DataOutputStream(java.io.DataOutputStream) PipedOutputStream(java.io.PipedOutputStream) IOException(java.io.IOException) PipedInputStream(java.io.PipedInputStream) DataInputStream(java.io.DataInputStream)

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