KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > util > StreamGobbler


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: StreamGobbler.java,v 1.4 2007/01/07 06:14:00 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.util;
23
24 import java.io.BufferedReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.InputStreamReader JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30
31 /**
32  * Class to read or capture specified input stream. This class was insipred by
33  * article http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
34  *
35  * @version $Id: StreamGobbler.java,v 1.4 2007/01/07 06:14:00 bastafidli Exp $
36  * @author Miro Halas
37  * @code.reviewer Miro Halas
38  * @code.reviewed 1.2 2004/12/18 06:18:25 bastafidli
39  */

40 public class StreamGobbler extends Thread JavaDoc
41 {
42    // Attributes ///////////////////////////////////////////////////////////////
43

44    /**
45     * Input stream to read.
46     */

47    protected InputStream JavaDoc m_inputStream;
48
49    /**
50     * Name of the stream.
51     */

52    protected String JavaDoc m_streamName;
53    
54    /**
55     * If true then the stream will be captured (carefull about memory) and can
56     * be read.
57     */

58    protected boolean m_bCapture;
59    
60    /**
61     * Maximal size of the stream to be held in memory.
62     */

63    protected long m_lMaxCaptureSize;
64    
65    /**
66     * Content of the stream if it should be captured
67     */

68    protected StringBuffer JavaDoc m_sbStreamContent;
69     
70    // Cached values ////////////////////////////////////////////////////////////
71

72    /**
73     * Logger for this class
74     */

75    private static Logger JavaDoc s_logger = Log.getInstance(StreamGobbler.class);
76
77    // Constructors /////////////////////////////////////////////////////////////
78

79    /**
80     * Constructor. The stream will not be captured.
81     *
82     * @param is - input stream to read or capture
83     * @param name - name of the stream
84     */

85    public StreamGobbler(
86       InputStream JavaDoc is,
87       String JavaDoc name
88    )
89    {
90       this(is, name, false, 0);
91    }
92
93    /**
94     * Constructor
95     *
96     * @param is - input stream to read or capture
97     * @param name - name of the stream
98     * @param bCapture - if true then the stream will be captured (carefull about
99     * memory) and can be read
100     * @param lMaxCaptureSize - maximal size of the stream to be held in memory.
101     */

102    public StreamGobbler(
103       InputStream JavaDoc is,
104       String JavaDoc name,
105       boolean bCapture,
106       long lMaxCaptureSize
107    )
108    {
109       m_inputStream = is;
110       m_streamName = name;
111       m_bCapture = bCapture;
112       m_lMaxCaptureSize = lMaxCaptureSize;
113       if (m_bCapture)
114       {
115          m_sbStreamContent = new StringBuffer JavaDoc();
116       }
117       else
118       {
119          m_sbStreamContent = null;
120       }
121    }
122     
123    // Logic ////////////////////////////////////////////////////////////////////
124

125    /**
126     * {@inheritDoc}
127     */

128    public void run()
129    {
130       try
131       {
132          InputStreamReader JavaDoc isr = new InputStreamReader JavaDoc(m_inputStream);
133          BufferedReader JavaDoc br = new BufferedReader JavaDoc(isr);
134
135          String JavaDoc line;
136          StringBuffer JavaDoc debugger = new StringBuffer JavaDoc(m_streamName);
137          int iDebuggerDefaultLength;
138          String JavaDoc strLineSeparator = GlobalConstants.getLineSeparator();
139          int iLineSeparatorLength = strLineSeparator.length();
140          int iEndOfLine;
141          
142          debugger.append(": ");
143          iDebuggerDefaultLength = debugger.length();
144          while ((line = br.readLine()) != null)
145          {
146             debugger.delete(0, iDebuggerDefaultLength);
147             debugger.append(line);
148             s_logger.finest(debugger.toString());
149             if (m_bCapture)
150             {
151                while (m_sbStreamContent.length() + line.length() > m_lMaxCaptureSize)
152                {
153                   // lets tream the content so that new line fits in
154
iEndOfLine = m_sbStreamContent.indexOf(strLineSeparator);
155                   if (iEndOfLine == -1)
156                   {
157                      m_sbStreamContent.delete(0, m_sbStreamContent.length());
158                   }
159                   else
160                   {
161                      m_sbStreamContent.delete(0, iEndOfLine + iLineSeparatorLength);
162                   }
163                }
164                m_sbStreamContent.append(line);
165             }
166          }
167       }
168       catch (IOException JavaDoc ioExc)
169       {
170          s_logger.log(Level.WARNING,
171                                "Unexpected exception while reading stream "
172                                + m_streamName, ioExc);
173       }
174    }
175
176    /**
177     * @return String - the content of the captured stream or null if you choose
178     * to do not capture it.
179     */

180    public String JavaDoc getStreamContent()
181    {
182       return m_sbStreamContent.toString();
183    }
184 }
185
Popular Tags