1 37 90 package net.sourceforge.cruisecontrol.util; 91 92 import java.io.BufferedReader ; 93 import java.io.IOException ; 94 import java.io.InputStream ; 95 import java.io.InputStreamReader ; 96 import java.io.PrintWriter ; 97 98 106 public class StreamPumper implements Runnable { 107 108 private BufferedReader in; 109 private StreamConsumer consumer = null; 110 private PrintWriter out = new PrintWriter (System.out); 111 private static final int SIZE = 1024; 112 113 public StreamPumper(InputStream in, PrintWriter writer) { 114 this(in); 115 out = writer; 116 } 117 118 public StreamPumper(InputStream in) { 119 this.in = new BufferedReader (new InputStreamReader (in), SIZE); 120 } 121 122 public StreamPumper(InputStream in, StreamConsumer consumer) { 123 this(in); 124 this.consumer = consumer; 125 } 126 127 public StreamPumper(InputStream in, PrintWriter writer, 128 StreamConsumer consumer) { 129 this(in); 130 this.out = writer; 131 this.consumer = consumer; 132 } 133 134 public void run() { 135 try { 136 String s = in.readLine(); 137 while (s != null) { 138 consumeLine(s); 139 if (out != null) { 140 out.println(s); 141 out.flush(); 142 } 143 144 s = in.readLine(); 145 } 146 } catch (IOException e) { 147 } finally { 149 try { 150 in.close(); 151 } catch (IOException e) { 152 } 154 } 155 } 156 157 public void flush() { 158 out.flush(); 159 } 160 161 public void close() { 162 flush(); 163 out.close(); 164 } 165 166 private void consumeLine(String line) { 167 if (consumer != null) { 168 consumer.consumeLine(line); 169 } 170 } 171 } 172 | Popular Tags |