001/* 002 * NativeAppLauncher.java 003 * $Source: /cvsroot2/open/projects/WebARTS/ca/bc/webarts/tools/StreamGobbler.java,v $ 004 * $Revision: 1214 $ $Date: 2018-03-02 22:04:16 -0800 (Fri, 02 Mar 2018) $ $Locker: $ 005 * Copyright 2003 (C) WebARTS Design. All rights reserved. 006 * 007 * This program is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU General Public License 009 * as published by the Free Software Foundation; either version 2 010 * of the License, or any later version. 011 * 012 * This program is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 015 * GNU General Public License for more details. 016 * 017 * You should have received a copy of the GNU General Public License 018 * along with this program; if not, write to the Free Software 019 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 020 */ 021 022package ca.bc.webarts.tools; 023import java.io.*; 024 025 026/** 027 * A simple class to take an input stream and dump it out to the commandline 028 * with a prepended string. It is designed to run in its own thread so it can 029 * pump the data silently in the background. 030 * 031 * @author TGutwin 032 */ 033public class StreamGobbler extends Thread 034{ 035 /** Flag so we can signal the control/signal the threads end. **/ 036 public boolean finishedGobbling_ = false; 037 038 /** 039 * The class InputStream that gets echoed to the designated output stream 040 * (default=System.out). 041 **/ 042 InputStream is_ = null; 043 044 /** The location of any output. **/ 045 PrintStream ps_ = System.out; 046 047 /** The location of any output. **/ 048 OutputStream os_ = System.out; 049 050 /** The string to get prepended to the output from the InputStream. **/ 051 String type_ = ""; 052 053 /** signals if we are gobbling input instead of the default outputstreams. **/ 054 boolean inputGobbler_ = false; 055 056 /** the lin sep chars **/ 057 String lineSep_ = System.getProperty("line.separator"); 058 059 /** the input from in stream **/ 060 String input_ = null; 061 062 /** the thread aborted flag. **/ 063 boolean aborted_ = false; 064 065 /** a control flag that turns on/off the capturing of the input stream **/ 066 boolean capture_ = false; 067 068 /** 069 * The buffer used to store the input stream data IFF the capture has been 070 * turned on with the capture_ flag. 071 **/ 072 StringBuffer captureBuffer_ = new StringBuffer(); 073 074 075 /** 076 * A Simple Constructor for the StreamGobbler object WITHOUT assigning a 077 * stream to gobble. YOU HAVE TO ASSIGN THE InputStream before using this object. 078 * It that ends up using 079 * System.out as the Output PrintStream. So it simply dumps the monitored 080 * InputStream to System.out. 081 * 082 */ 083 public StreamGobbler() 084 { 085 086 } 087 088 089 /** 090 * A Simple Constructor for the StreamGobbler object that ends up using 091 * System.out as the Output PrintStream. So it simply dumps the monitored 092 * InputStream to System.out. 093 * 094 * @param inputstream The stream to watch for input. 095 * @param string The string to get prepended to the output from the 096 * InputStream before it gets pumped into the output 097 * PrintStream. 098 */ 099 public StreamGobbler(InputStream inputstream, String string) 100 { 101 is_ = inputstream; 102 if (string != null) 103 { 104 type_ = string; 105 } 106 } 107 108 109 /** 110 * Constructor for the StreamGobbler object that requires all parameters to 111 * be spec'd. 112 * 113 * @param inputstream The stream to watch for input. 114 * @param string The string to get prepended to the output from the 115 * InputStream before it gets pumped into the output 116 * PrintStream. 117 * @param os An output PrintStream to send the data to. 118 * 119 */ 120 public StreamGobbler(InputStream inputstream, String string, PrintStream os) 121 { 122 is_ = inputstream; 123 if (os != null) 124 ps_ = os; 125 else 126 capture_ = true; 127 if (string != null) 128 { 129 type_ = string; 130 } 131 } 132 133 134 /** 135 * Constructor for the StreamGobbler object that requires all parameters to 136 * be spec'd. 137 * 138 * @param inputstream The stream to watch for input. 139 * @param ps An output PrintStream to send the data to, If 140 * this is null it simply captures the output. 141 * 142 */ 143 public StreamGobbler(InputStream inputstream, PrintStream ps) 144 { 145 is_ = inputstream; 146 if (ps != null) 147 ps_ = ps; 148 else 149 capture_ = true; 150 } 151 152 153 /** Main processing method for the StreamGobbler object */ 154 public void run() 155 { 156 if (!inputGobbler_) 157 { 158 if (type_ != null) 159 { 160 try 161 { 162 InputStreamReader inputstreamreader 163 = new InputStreamReader(is_); 164 if (inputstreamreader != null) 165 { 166 // set up the output stream 167 BufferedReader bufferedreader 168 = new BufferedReader(inputstreamreader); 169 if (bufferedreader != null) 170 { 171 Object object = null; 172 String string; 173 int retry = 0; 174 while (retry < 25) 175 { 176 try 177 { 178 while (!finishedGobbling_ && 179 (string = bufferedreader.readLine()) != null ) 180 { 181 if (capture_) 182 { 183 captureBuffer_.append(string); 184 captureBuffer_.append(lineSep_); 185 } 186 if (ps_ !=null) 187 { 188 ps_.print(type_ ); 189 ps_.println(string); 190 } 191 } 192 } 193 catch (IOException ioEx) 194 { 195 ioEx.printStackTrace(); 196 //finishedGobbling_ = true; 197 } 198 199 retry++; 200 } 201 //ps_.println(type_ + "> stream now done ("+retry+")."); 202 bufferedreader.close(); 203 } 204 inputstreamreader.close(); 205 Object object = null; 206 } 207 Object object = null; 208 finishedGobbling_ = true; 209 } 210 catch (Exception exception) 211 { 212 exception.printStackTrace(); 213 finishedGobbling_ = true; 214 } 215 } 216 } 217 else 218 { 219 // we are gobbling and simply dumping the inputstream to the outputstream 220 try 221 { 222 if (os_ !=null) // then transfer the stream to the output 223 { 224 BufferedWriter out = new BufferedWriter( 225 new OutputStreamWriter(os_)); 226 if(input_ != null) 227 { 228 for(int i = 0; i < input_.length(); i++) 229 { 230 char ch = input_.charAt(i); 231 if(ch == '\n') 232 out.write(lineSep_); 233 else 234 out.write(ch); 235 } 236 } 237 out.close(); 238 } 239 240 } 241 catch(Exception e) 242 { 243 if(!aborted_) 244 { 245 System.out.println(e.toString()); 246 } 247 } 248 finally 249 { 250 //threadDone(); 251 } 252 } 253 } 254 255 256 public void abort() 257 { 258 aborted_ = true; 259 ps_.close(); 260 } 261 262 263 /** 264 * Set Method for class field 'type_' which sets the string to prepend to the output. 265 * 266 * @param is the value to set this class field to. 267 * 268 **/ 269 public void setPrependString(String prepend) 270 { 271 if (prepend != null) 272 this.type_ = prepend; 273 else 274 this.type_ = ""; 275 } // setPrependString Method 276 277 278 /** 279 * Get Method for class field 'type_' which is the string to prepend to the output. 280 * 281 * @return String - The value the class field 'type_'. 282 * 283 **/ 284 public String getPrependString() 285 { 286 return type_; 287 } // getIs_ Method 288 289 290 /** 291 * Set Method for class field 'is_'. 292 * 293 * @param is is the value to set this class field to. 294 * 295 **/ 296 public void setInputStream(InputStream is) 297 { 298 this.is_ = is; 299 } // setIs_ Method 300 301 302 /** 303 * Get Method for class field 'is_'. 304 * 305 * @return InputStream - The value the class field 'is_'. 306 * 307 **/ 308 public InputStream getInputStream() 309 { 310 return is_; 311 } // getIs_ Method 312 313 314 /** 315 * Set Method for class field 'ps_'. 316 * 317 * @param ps_ is the value to set this class field to. 318 * 319 **/ 320 public void setOs_(PrintStream ps_) 321 { 322 this.ps_ = ps_; 323 } // setOs_ Method 324 325 326 /** 327 * Get Method for class field 'ps_'. 328 * 329 * @return PrintStream - The value the class field 'ps_'. 330 * 331 **/ 332 public PrintStream getOs_() 333 { 334 return ps_; 335 } // getOs_ Method 336 337 338 /** 339 * Gets the inputStreams captured data. 340 * 341 * @return StringBuffer - The value the class field 'captureBuffer_'. 342 * 343 **/ 344 public String getCapturedOutput() 345 { 346 return captureBuffer_.toString(); 347 } // getCaptureBuffer_ Method 348 349 350 /** 351 * Set Method for class field 'capture_'. It turns on or off this classes 352 * ability to capture and internally stor the InputStream data. 353 * 354 * @param capture_ is the value to set this class field to. 355 * 356 **/ 357 public void setCapture(boolean capture) 358 { 359 this.capture_ = capture; 360 } // setCapture_ Method 361 362 363 /** 364 * Get Method for class field 'capture_'. 365 * 366 * @return boolean - The value the class field 'capture_'. 367 * 368 **/ 369 public boolean getCapture() 370 { 371 return capture_; 372 } // getCapture_ Method 373 374}