From 6eb3f5ca771f7d39a3d99f10cbc29e6de9eff8f9 Mon Sep 17 00:00:00 2001 From: Yeo Kheng Meng Date: Thu, 28 Nov 2013 00:08:00 +0800 Subject: [PATCH] Corrected bug where stdout was not restored when needed --- docs-to-pdf-converter/src/MainClass.java | 28 ++++++++++++++++++------ 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/docs-to-pdf-converter/src/MainClass.java b/docs-to-pdf-converter/src/MainClass.java index 2f0e896..f7c4bf4 100644 --- a/docs-to-pdf-converter/src/MainClass.java +++ b/docs-to-pdf-converter/src/MainClass.java @@ -17,6 +17,8 @@ public enum DOC_TYPE { PPT, PPTX } + + private static PrintStream originalStdout = null; public static void main(String[] args){ @@ -25,17 +27,26 @@ public static void main(String[] args){ try{ converter = processArguments(args); } catch ( IllegalArgumentException e){ + if(originalStdout != null) { + System.setOut(originalStdout); + } System.out.println("\n\nInput file not specified."); return; } if(converter == null){ + if(originalStdout != null) { + System.setOut(originalStdout); + } System.out.println("Unable to determine type of input file."); } else { try { converter.convert(); } catch (Exception e) { + if(originalStdout != null) { + System.setOut(originalStdout); + } e.printStackTrace(); } } @@ -73,13 +84,6 @@ public static Converter processArguments(String[] args) throws IllegalArgumentEx outPath = changeExtensionToPDF(inPath); } - if(!shouldShowMessages){ - System.setOut(new PrintStream(new OutputStream() { - public void write(int b) { - //DO NOTHING - } - })); - } if(values.type == null){ if(inPath.endsWith("doc")){ @@ -113,6 +117,16 @@ public void write(int b) { } + + if(!shouldShowMessages){ + originalStdout = System.out; + + System.setOut(new PrintStream(new OutputStream() { + public void write(int b) { + //DO NOTHING + } + })); + } } catch (CmdLineException e) { // handling of wrong arguments