I'm trying to pipe text between two java programs. For the sake of simplicity I'm presenting this code:
import java.io.DataInputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) throws IOException {
DataInputStream stdin = new DataInputStream(System.in);
String completeText = "";
while (stdin.available() > 0) {
byte[] tempByte = { stdin.readByte() };
completeText += new String(tempByte);
}
System.out.println(completeText);
}
}
When doing the following on linux or windows, the text seems to get omitted as if the pipe was blocking or lost quite random. Sometimes everything gets through, sometimes not:
echo "omg wtf" | java Test | java Test
Any ideas on this? The slower the cpu the more often the text gets through, it seems. Is "available" returning the wrong result for any reason when the input is piped from java System.out.println()?
Cheers!