From 71981b7b9a7652236b3e355591d5bc2e0ad15502 Mon Sep 17 00:00:00 2001 From: Vladislav Hristov Date: Mon, 10 Feb 2014 21:46:37 +0200 Subject: [PATCH] Allow java classes to close stdout/stderr 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. --- .../com/martiansoftware/nailgun/examples/Exit.java | 5 ++++- .../com/martiansoftware/nailgun/NGOutputStream.java | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/nailgun-examples/src/main/java/com/martiansoftware/nailgun/examples/Exit.java b/nailgun-examples/src/main/java/com/martiansoftware/nailgun/examples/Exit.java index b13027ca..c524d68f 100644 --- a/nailgun-examples/src/main/java/com/martiansoftware/nailgun/examples/Exit.java +++ b/nailgun-examples/src/main/java/com/martiansoftware/nailgun/examples/Exit.java @@ -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); } -} \ No newline at end of file +} diff --git a/nailgun-server/src/main/java/com/martiansoftware/nailgun/NGOutputStream.java b/nailgun-server/src/main/java/com/martiansoftware/nailgun/NGOutputStream.java index 591c2442..0b90e774 100644 --- a/nailgun-server/src/main/java/com/martiansoftware/nailgun/NGOutputStream.java +++ b/nailgun-server/src/main/java/com/martiansoftware/nailgun/NGOutputStream.java @@ -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() { + } }