Next version of JABA
[jabaws.git] / engine / compbio / engine / local / StreamGobbler.java
1 /* Copyright (c) 2009 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0     \r
4  * \r
5  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
6  *  Apache License version 2 as published by the Apache Software Foundation\r
7  * \r
8  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
9  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
10  *  License for more details.\r
11  * \r
12  *  A copy of the license is in apache_license.txt. It is also available here:\r
13  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 package compbio.engine.local;\r
19 \r
20 import java.io.BufferedReader;\r
21 import java.io.BufferedWriter;\r
22 import java.io.Closeable;\r
23 import java.io.IOException;\r
24 import java.io.InputStream;\r
25 import java.io.InputStreamReader;\r
26 import java.io.OutputStream;\r
27 import java.io.OutputStreamWriter;\r
28 \r
29 import org.apache.log4j.Logger;\r
30 \r
31 import compbio.util.SysPrefs;\r
32 import compbio.util.annotation.ThreadSafe;\r
33 \r
34 @ThreadSafe()\r
35 public class StreamGobbler implements Runnable {\r
36 \r
37         enum OutputType {\r
38                 OUTPUT, ERROR\r
39         };\r
40 \r
41         private static final Logger log = Logger.getLogger(StreamGobbler.class);\r
42 \r
43         final private InputStream is;\r
44         final private OutputStream os;\r
45         final private OutputType type;\r
46 \r
47         StreamGobbler(InputStream is, OutputStream os, OutputType type) {\r
48                 this.is = is;\r
49                 this.os = os;\r
50                 this.type = type;\r
51         }\r
52 \r
53         public void run() {\r
54 \r
55                 BufferedReader br = new BufferedReader(new InputStreamReader(is));\r
56                 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os));\r
57                 String line = null;\r
58                 try {\r
59                         while ((line = br.readLine()) != null && !Thread.interrupted()) {\r
60                                 log.trace(type + ">" + line);\r
61                                 writer.write(line + SysPrefs.newlinechar);\r
62                         }\r
63                         br.close();\r
64                         writer.close();\r
65                 } catch (IOException ioe) {\r
66                         log.error(ioe.getMessage(), ioe.getCause());\r
67                 } finally {\r
68                         closeSilently(br);\r
69                         closeSilently(writer);\r
70                 }\r
71         }\r
72 \r
73         private static void closeSilently(Closeable stream) {\r
74                 if (stream != null) {\r
75                         try {\r
76                                 stream.close();\r
77                         } catch (IOException e) {\r
78                                 log.error(e.getLocalizedMessage(), e.getCause());\r
79                         }\r
80                 }\r
81         }\r
82 }\r