1 21 22 package org.opensubsystems.core.util; 23 24 import java.io.BufferedReader ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.InputStreamReader ; 28 import java.util.logging.Level ; 29 import java.util.logging.Logger ; 30 31 40 public class StreamGobbler extends Thread 41 { 42 44 47 protected InputStream m_inputStream; 48 49 52 protected String m_streamName; 53 54 58 protected boolean m_bCapture; 59 60 63 protected long m_lMaxCaptureSize; 64 65 68 protected StringBuffer m_sbStreamContent; 69 70 72 75 private static Logger s_logger = Log.getInstance(StreamGobbler.class); 76 77 79 85 public StreamGobbler( 86 InputStream is, 87 String name 88 ) 89 { 90 this(is, name, false, 0); 91 } 92 93 102 public StreamGobbler( 103 InputStream is, 104 String 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 (); 116 } 117 else 118 { 119 m_sbStreamContent = null; 120 } 121 } 122 123 125 128 public void run() 129 { 130 try 131 { 132 InputStreamReader isr = new InputStreamReader (m_inputStream); 133 BufferedReader br = new BufferedReader (isr); 134 135 String line; 136 StringBuffer debugger = new StringBuffer (m_streamName); 137 int iDebuggerDefaultLength; 138 String 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 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 ioExc) 169 { 170 s_logger.log(Level.WARNING, 171 "Unexpected exception while reading stream " 172 + m_streamName, ioExc); 173 } 174 } 175 176 180 public String getStreamContent() 181 { 182 return m_sbStreamContent.toString(); 183 } 184 } 185 | Popular Tags |