Hi there,
I have a scenario where I want to read the input stream of a process(/usr/bin/program ran using java process builder) as soon as the program writes to stdout/console.
In general the program /usr/bin/program writes data to console/stdout as soon as there are bytes avaialble even with say 100 bytes. But the same program when I ran
using Java process builder and read the input stream, I do not get the data as soon as program writes to stdout/console but after 4K of data is available on the input stream.
I need the data(every byte written) to be available in the input stream in real time.
I have tried at least these two options. Any help would be greatly appreciated.
Option 1:
process = new ProcessBuilder().redirectErrorStream(true).command ("/usr/bin/ntfsubscribe", "-s").start();
InputStream is = process.getInputStream();
ReadableByteChannel channel = Channels.newChannel(is);
ByteBuffer buf = ByteBuffer.allocate(2048);
int bytesRead = channel.read(buf);
while (bytesRead != -1) {
System.out.println("Read " + bytesRead);
buf.flip();
while(buf.hasRemaining()){
System.out.print((char) buf.get());
}
buf.clear();
bytesRead = channel.read(buf);
}
Option 2:
BufferedReader input = new BufferedReader(new InputStreamReader(is));
String line = "";
while ((line = input.readLine()) != null) {
System.out.println(line);
}
Thanks