01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.admin.common;
05:
06: import org.apache.commons.io.IOUtils;
07:
08: import java.io.BufferedReader;
09: import java.io.InputStream;
10: import java.io.InputStreamReader;
11:
12: /**
13: * Reads lines from an InputStream until either EOF is reached or an
14: * IOException is raised.
15: */
16:
17: public class InputStreamDrainer extends Thread {
18: private InputStream m_stream;
19: private StringBuffer m_buffer;
20:
21: private static final String LINE_SEP = System
22: .getProperty("line.separator");
23:
24: public InputStreamDrainer(InputStream stream) {
25: m_stream = stream;
26: }
27:
28: public void run() {
29: InputStreamReader streamReader = new InputStreamReader(m_stream);
30: BufferedReader bufferedReader = new BufferedReader(streamReader);
31: String line;
32:
33: m_buffer = new StringBuffer();
34:
35: while (true) {
36: try {
37: if ((line = bufferedReader.readLine()) == null) {
38: IOUtils.closeQuietly(bufferedReader);
39: return;
40: }
41: m_buffer.append(line + LINE_SEP);
42: } catch (Exception e) {
43: IOUtils.closeQuietly(bufferedReader);
44: return;
45: }
46: }
47: }
48:
49: public String getBufferContent() {
50: return m_buffer.toString();
51: }
52: }
|