1 /* Copyright (c) 2009 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0
\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
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
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
15 * Any republication or derived work distributed in source code form
\r
16 * must include this copyright and license notice.
\r
18 package compbio.engine.local;
\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
29 import org.apache.log4j.Logger;
\r
31 import compbio.util.SysPrefs;
\r
32 import compbio.util.annotation.ThreadSafe;
\r
35 public class StreamGobbler implements Runnable {
\r
41 private static final Logger log = Logger.getLogger(StreamGobbler.class);
\r
43 final private InputStream is;
\r
44 final private OutputStream os;
\r
45 final private OutputType type;
\r
47 StreamGobbler(InputStream is, OutputStream os, OutputType type) {
\r
55 BufferedReader br = new BufferedReader(new InputStreamReader(is));
\r
56 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os));
\r
59 while ((line = br.readLine()) != null && !Thread.interrupted()) {
\r
60 log.trace(type + ">" + line);
\r
61 writer.write(line + SysPrefs.newlinechar);
\r
65 } catch (IOException ioe) {
\r
66 log.error(ioe.getMessage(), ioe.getCause());
\r
69 closeSilently(writer);
\r
73 private static void closeSilently(Closeable stream) {
\r
74 if (stream != null) {
\r
77 } catch (IOException e) {
\r
78 log.error(e.getLocalizedMessage(), e.getCause());
\r