use of java.io.PipedInputStream 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();
}
}
use of java.io.PipedInputStream 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();
}
}
use of java.io.PipedInputStream 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);
}
}
use of java.io.PipedInputStream 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;
}
use of java.io.PipedInputStream in project symmetric-ds by JumpMind.
the class InternalTransportManager method getFilePushTransport.
public IOutgoingWithResponseTransport getFilePushTransport(final Node targetNode, final Node sourceNode, String securityToken, String registrationUrl) throws IOException {
final PipedOutputStream pushOs = new PipedOutputStream();
final PipedInputStream pushIs = new PipedInputStream(pushOs);
final PipedOutputStream respOs = new PipedOutputStream();
final PipedInputStream respIs = new PipedInputStream(respOs);
runAtClient(targetNode.getSyncUrl(), pushIs, respOs, new IClientRunnable() {
public void run(ISymmetricEngine engine, InputStream is, OutputStream os) throws Exception {
// This should be basically what the push servlet does ...
engine.getFileSyncService().loadFilesFromPush(sourceNode.getNodeId(), is, os);
}
});
return new InternalOutgoingWithResponseTransport(pushOs, respIs);
}
Aggregations