Handle Input using StreamGobbler
Asked Answered
D

1

7

I have been through the StreamGobbler and I understand the usage and the reason on why it has been implemented. However the scenarios covered are only those wherein there could be an output from the command / handling errors.

I do not find any scenario wherein StreamGobbler is used to handle inputs. For example, in mailx , I have to specify the body of the email, which I have done in the following format

Process proc = Runtime.getRuntime().exec(cmd);
OutputStreamWriter osw = new OutputStreamWriter(proc.getOutputStream());
osw.write(mailBody);
osw.close();

How can this be handled through StreamGobbler, or it is not required to be handled through it?

Durr answered 4/9, 2012 at 6:56 Comment(0)
S
7

Ideally, you would employ the StreamGobbler on your error stream (in a separate thread) if you are already expecting something on InputStream, to look into when the process.waitFor() returns a non-zero value to find out the error message. If you are not interested in the InputStream, then you can read the ErrorStream directly in your code, once you are done with giving your inputs to the command.

Process proc = Runtime.getRuntime().exec(cmd)
// Start a stream gobbler to read the error stream.
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream());
errorGobbler.start();

OutputStreamWriter osw = new OutputStreamWriter(proc.getOutputStream())
osw.write(mailBody)
osw.close();

int exitStatus = proc.waitFor();
if (0 != exitStatus) {
    /*
     * If you had not used a StreamGobbler to read the errorStream, you wouldn't have
     * had a chance to know what went wrong with this command execution.
     */
    LOG.warn("Error while sending email: " + errorGobbler.getContent());
}
Suksukarno answered 4/9, 2012 at 7:18 Comment(7)
So in this case , I am not interested with the InputStream for the mailx command. But as a failsafe should I include it? Also regarding the mailbody which I have mentioned above, this would not be part of the StreamGobbler right ?Durr
Not as a failsafe, but to informed about errors if any, you should always read the error stream. As you said, inputstream is optional if you are not expecting any output from your command. No, the mail body is your input to the process.Suksukarno
I am still confused. My command looks like this mailx -s mymail mailsubject mailtoaddress so when I would execute it and would not be expecting anything in the input stream. So the mailbody has nothing to do with the StreamGobbler,and does need to be passed to it. Is my understanding right , please correct me if I am wrongDurr
Right, all you need to pass to StreamGobbler is proc.getOutputStream(). please see the edit for how I would use it.Suksukarno
In the edit above , is the proc.getOutputStream() requried to be passed to the StreamGobbler. Something like this StreamGobbler StreamGobbler outputGobbler = new StreamGobbler(proc.getOutputStream()); outputGobbler.start();Durr
No, not needed. In fact, should not, as StreamGobbler is for inputstreams (input/error).Suksukarno
Thanks a lot @Suksukarno This really cleared my concept of StreamGobbler. Hope this discussion also helps the others.Durr

© 2022 - 2024 — McMap. All rights reserved.