void sendMessage(String message, OutputStream os) {
try {
ExternalLibrary.sendMessage(message, os);
} catch (IOException e) {
e.printStackTrace();
}
}
I have the above bit of code that uses an external method to send a message through an output stream. If there's an IO exception (eg a broken pipe), that method will perform some logging and throw than IOException. I want to prevent the logging from happening if there's a broken pipe. The logging performed by the method is vague and difficult to look through, so I only want to call it if I can be reasonably sure the pipe isn't broken. Something like:
void sendMessage(String message, OutputStream os) {
if (!os.isClosed()) {
try {
ExternalLibrary.sendMessage(message, os);
} catch (IOException e) {
e.printStackTrace();
}
} else {
logger.error("Broken pipe");
}
}
Since you are in a MultiThreaded environment you can never be sure if a asynchrouius task invalidate the OutputStream throu a different Thread.
A simplified example:
You start the sending of the message. Right after sending the message your PC is running out of RAM, the OutputStream breaks because it can not allocate more RAM - voila your stream is broken. This is happening after the calling of the function but before the leave of the function.
The concept of "broken" is unknown to a OutputStream! Also a Pipe is a different kind of design. You mean Streams.
Only the method write(*)
is able to produce a IOException
what can be used to express a broken pipe.
Unfortunately the method write(*)
is called inside ExternalLibrary.sendMessage(message, os);
.
That is too late for your task to
call it if I can be reasonably sure the pipe isn't broken
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.