How can I catch errors when connecting to / from javas stdin / stdout

I have a Java application that takes data from stdinand writes the results to stdout. Like this:

app1 | java_app | app2

The amount of data moving through the pipe is large and takes a lot of time (hours and days are not something unusual or unexpected).

It seems that if he app1dies and closes his pipe end, I get an exception related to the pipe. However, he app2dies and closes his end of the channel. I do not get an exception. This means that it java_appcontinues to consume input, which will never generate a useful output, working for several hours or days.

The difference in the pipe exception messages appears to be related to System.inhow InputStream, and System.out- PrintStreamand PrintStream. I know that you can get the error state from PrintStreamwith stream.checkError()- but using this everywhere is inconvenient (and forces the stream of your output stream). I am happy to abandon the core functionality PrintStreamin order to improve error reporting.

Is there any other way to access stdout that is not wrapped in PrintStream, but is better instead OutputStream?

+4
source share
1 answer

It turns out that one of my colleagues found a good way to do this.

OutputStream stream = new FileOutputStream(FileDescriptor.out);
+1
source

Source: https://habr.com/ru/post/1544697/


All Articles