Skip to content

Commit

Permalink
Allow java classes to close stdout/stderr
Browse files Browse the repository at this point in the history
When the client calls a class, where either stdout or stderr is closed
the socket connection to the client is closed and no further data is
sent. This includes data transmitted over the other stream and exit
codes.
  • Loading branch information
vhristov committed Feb 10, 2014
1 parent 4cac9ae commit 71981b7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Expand Up @@ -27,7 +27,10 @@ public static void main (String[] args) {
exitCode = Integer.parseInt(args[0]);
} catch (Exception e) {}
}
// Close stdout to test the exit code is returned properly
// even in such case
System.out.close();
System.exit(exitCode);
}

}
}
Expand Up @@ -74,4 +74,14 @@ public void write(byte[] b, int offset, int len) throws IOException {
}
flush();
}

/**
* @see java.io.OutputStream.close()
*
* Implement an empty close function, to allow the client to close
* the stdout and/or stderr, without this closing the connection
* socket to the client.
*/
public void close() {
}
}

4 comments on commit 71981b7

@jimpurbrick
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when methods are called on a closed OutputStream? Should NGOutputStream set a flag in close() and override the other OutputStream methods to emulate the closed OutputStream behaviour when the flag is set?

@vhristov
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I am don't know. Basically in C if someone closes the stdout fd, it cannot write anymore to it.

From http://docs.oracle.com/javase/6/docs/api/java/io/OutputStream.html#close()

A closed stream cannot perform output operations and cannot be reopened.

I am not sure, but does this means any subsequent writes to the stream will throw an exception, and writing to the stream?

@vhristov
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, a quick test shows a an IOException:
having this:

System.out.println("test");
System.out.close();
System.out.println("test1");

Results into this:

Exception occurred: java.io.IOException (to be caught at: java.io.PrintStream.write(), line=530 bci=58
...
Exception occurred: java.io.IOException (to be caught at: java.io.PrintStream.newLine(), line=549 bci=48)

Will add a "close" flag and throw an exception if set, and will see what happens.

@vhristov
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, check this one: 2785ce9 (in branch for-issue23).

Please sign in to comment.