KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > admin > common > InputStreamDrainer


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.admin.common;
5
6 import org.apache.commons.io.IOUtils;
7
8 import java.io.BufferedReader JavaDoc;
9 import java.io.InputStream JavaDoc;
10 import java.io.InputStreamReader JavaDoc;
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 JavaDoc {
18   private InputStream JavaDoc m_stream;
19   private StringBuffer JavaDoc m_buffer;
20       
21   private static final String JavaDoc LINE_SEP = System.getProperty("line.separator");
22   
23   public InputStreamDrainer(InputStream JavaDoc stream) {
24     m_stream = stream;
25   }
26     
27   public void run() {
28     InputStreamReader JavaDoc streamReader = new InputStreamReader JavaDoc(m_stream);
29     BufferedReader JavaDoc bufferedReader = new BufferedReader JavaDoc(streamReader);
30     String JavaDoc line;
31     
32     m_buffer = new StringBuffer JavaDoc();
33     
34     while(true) {
35       try {
36         if((line = bufferedReader.readLine()) == null) {
37           IOUtils.closeQuietly(bufferedReader);
38           return;
39         }
40         m_buffer.append(line+LINE_SEP);
41       } catch(Exception JavaDoc e) {
42         IOUtils.closeQuietly(bufferedReader);
43         return;
44       }
45     }
46   }
47   
48   public String JavaDoc getBufferContent() {
49     return m_buffer.toString();
50   }
51 }
52
Popular Tags