Search code examples
javaphpproc-open

Can proc_open (php => java) have multiple input streams?


I'm using proc_open in php to call java application, send a large text to it for processing and capture a returned result. Is it possible to pass several text strings (input streams) instead of just one?

This is what I've got at the moment:

fwrite($pipes[0], $input);
fclose($pipes[0]);

$output = stream_get_contents($pipes[1]);
fclose($pipes[1]);

If I do something like this, java still recognizes it as one input stream:

fwrite($pipes[0], $input);
fwrite($pipes[0], $input1);
fwrite($pipes[0], $input2);
fclose($pipes[0]);

$output = stream_get_contents($pipes[1]);
fclose($pipes[1]);

So is something like this possible at all? If not, any alternatives? I can't use command line params because it's a large text with multiple lines.


Solution

  • It depends what you are trying to do, and what the java application expects.

    If you want the Java application to see the concatenation of $input, $input2 and $input3, then sure ... your code will do that.

    If you want the Java to be able to automatically see those inputs as distinct streams, then no. As far as the Java IO system is concerned, the bytes are just bytes. There are no natural boundaries ... apart from the ultimate end of the (combined) stream.

    If you want the Java to see one stream that it can then split into three streams, then it is possible, but it will require some programming effort.

    • On the PHP side, you have to add some kind of "framing" information to the stream that tells the Java side where one "stream" ends and the next one starts.

    • On the Java side, you have to look for / interpret that framing information.

    The framing could be done by sending a byte count for each stream followed by the bytes, or it could be done with marker characters or sequences that designate the end of a stream.