StreamGobbler.java 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package com.fdkankan.common.utils;
  2. import java.io.*;
  3. public class StreamGobbler extends Thread {
  4. InputStream is;
  5. String type;
  6. OutputStream os;
  7. public StreamGobbler(InputStream is, String type) {
  8. this(is, type, null);
  9. }
  10. StreamGobbler(InputStream is, String type, OutputStream redirect) {
  11. this.is = is;
  12. this.type = type;
  13. this.os = redirect;
  14. }
  15. public void run() {
  16. InputStreamReader isr = null;
  17. BufferedReader br = null;
  18. PrintWriter pw = null;
  19. try {
  20. if (os != null)
  21. pw = new PrintWriter(os);
  22. isr = new InputStreamReader(is);
  23. br = new BufferedReader(isr);
  24. String line=null;
  25. while ( (line = br.readLine()) != null) {
  26. if (pw != null)
  27. pw.println(line);
  28. System.out.println(type + ">" + line);
  29. }
  30. if (pw != null)
  31. pw.flush();
  32. } catch (IOException ioe) {
  33. ioe.printStackTrace();
  34. } finally{
  35. try {
  36. if(pw!=null)
  37. {
  38. pw.close();
  39. }
  40. if(br!=null)
  41. {
  42. br.close();
  43. }
  44. if(isr!=null)
  45. {
  46. isr.close();
  47. }
  48. } catch (IOException e) {
  49. e.printStackTrace();
  50. }
  51. }
  52. }
  53. }