Debian Codegraph @

src/com/hypirion/io/Pipe.java

raw
1 /*
2 * Copyright (c) 2013 Jean Niklas L'orange. All rights reserved.
3 *
4 * The use and distribution terms for this software are covered by the
5 * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
6 * which can be found in the file LICENSE at the root of this distribution.
7 *
8 * By using this software in any fashion, you are agreeing to be bound by
9 * the terms of this license.
10 *
11 * You must not remove this notice, or any other, from this software.
12 */
13
14 package com.hypirion.io;
15
16 import java.io.InputStream;
17 import java.io.OutputStream;
18 import java.io.IOException;
19 import java.io.Writer;
20 import java.io.Reader;
21
22 /**
23 * A Pipe is a link between an input stream and an output stream or a reader or
24 * writer: The pipe, once started, will asynchronously redirect all data
25 * received from the input source to the output source until it is paused,
26 * stopped or the input source is empty.
27 * <p>
28 * Common usage of pipes includes asynchronous zipping of data from two data
29 * sources, redirecting output and input to subprocesses and for loggers you
30 * want to just pipe from and input source.
31 *
32 * @author Jean Niklas L'orange
33 * @since <code>com.hypirion.io 0.2.0</code>
34 */
35 public class Pipe {
36
37 /**
38 * The default size of a pipe's buffer.
39 * @since <code>com.hypirion.io 0.2.0</code>
40 */
41 public static final int DEFAULT_BUFFER_SIZE = 1024;
42
43 private final Thread pumper;
44 private final Object lock;
45 private volatile boolean currentlyRunning, stopped;
46
47 /**
48 * Creates a new pipe, which redirects data from the stream <code>in</code>
49 * to the stream <code>out</code> once started.
50 *
51 * @param in the input stream to read from.
52 * @param out the output stream to write out to.
53 */
54 public Pipe(InputStream in, OutputStream out) {
55 this(in, out, DEFAULT_BUFFER_SIZE);
56 }
57
58 /**
59 * Creates a new pipe with buffer size <code>bufsize</code>, which redirects
60 * data from the stream <code>in</code> to the stream <code>out</code> once
61 * started.
62 *
63 * @param in the input stream to read from.
64 * @param out the output stream to write out to.
65 * @param bufsize the buffer size of the pipe.
66 */
67 public Pipe(InputStream in, OutputStream out, int bufsize) {
68 Runnable pt = new PipeOutputStream(in, out, bufsize);
69 lock = new Object();
70 currentlyRunning = false;
71 stopped = false;
72 pumper = new Thread(pt);
73 pumper.setName(String.format("PipeThread %d", this.hashCode()));
74 pumper.setDaemon(true);
75 pumper.start();
76 }
77
78 /**
79 * Creates a new pipe, which redirects data from the reader <code>in</code>
80 * to the writer <code>out</code> once started.
81 *
82 * @param in the reader to read from.
83 * @param out the writer to write out to.
84 */
85 public Pipe(Reader in, Writer out) {
86 this(in, out, DEFAULT_BUFFER_SIZE);
87 }
88
89 /**
90 * Creates a new pipe with buffer size <code>bufsize</code>, which redirects
91 * data from the reader <code>in</code> to the writer <code>out</code> once
92 * started.
93 *
94 * @param in the reader to read from.
95 * @param out the writer to write out to.
96 * @param bufsize the buffer size of the pipe.
97 */
98 public Pipe(Reader in, Writer out, int bufsize) {
99 Runnable pt = new PipeWriter(in, out, bufsize);
100 lock = new Object();
101 currentlyRunning = false;
102 stopped = false;
103 pumper = new Thread(pt);
104 pumper.setName(String.format("PipeThread %d", this.hashCode()));
105 pumper.setDaemon(true);
106 pumper.start();
107 }
108
109 /**
110 * Waits for this pipe to finish piping. This happens when this pipe is
111 * stopped and the last blocking read has finished.
112 *
113 * @exception InterruptedException if this thread is interrupted while
114 * waiting.
115 *
116 * @see #stop()
117 * @see #stop(boolean)
118 */
119 public void join() throws InterruptedException {
120 pumper.join();
121 }
122
123 /**
124 * Starts up this pipe. If this pipe has not yet been started or has been
125 * paused, this method will (re)start the pipe. When this pipe is stopped,
126 * this method will do nothing.
127 */
128 public synchronized void start() {
129 if (!stopped) {
130 synchronized (lock) {
131 currentlyRunning = true;
132 lock.notify(); // Wake up the pumper if it's waiting.
133 }
134 }
135 }
136
137 /**
138 * Pauses this pipe. Will block until the current blocking read by this pipe
139 * has finished, regardless of whether this pipe has been stopped or not. If
140 * this pipe has already been paused or stopped, and there is no blocking
141 * read waiting, will immediatly return.
142 *
143 * @exception InterruptedException if this thread is interrupted while
144 * waiting.
145 *
146 * @see #pause(boolean)
147 */
148 public synchronized void pause() throws InterruptedException {
149 pause(true);
150 }
151
152 /**
153 * Pauses this pipe. If <code>block</code> is true, will wait until the
154 * current blocking read by this pipe has finished. Will otherwise notify
155 * this pipe and return immediately.
156 *
157 * @param block whether to wait for the blocking read (if any) or not.
158 *
159 * @exception InterruptedException if this thread is interrupted while
160 * waiting.
161 */
162 public synchronized void pause(boolean block) throws InterruptedException {
163 if (!stopped) {
164 synchronized (lock) {
165 currentlyRunning = false;
166 lock.notify();
167 if (block) {
168 lock.wait();
169 // Wait for signal from pumper, which will start after we
170 // release the lock
171 }
172 }
173 } else if (block) {
174 join();
175 }
176 }
177
178 /**
179 * Stops this pipe. Will wait until the last blocking read has finished.
180 *
181 * @exception InterruptedException if this thread is interrupted while
182 * waiting.
183 *
184 * @see #stop(boolean)
185 */
186 public synchronized void stop() throws InterruptedException {
187 stop(true);
188 }
189
190 /**
191 * Stops this pipe. Will wait until the last blocking read has finished. If
192 * <code>block</code> is true, will wait until the current blocking read by
193 * this pipe has finished. Will otherwise notify this pipe and return
194 * immediately.
195 *
196 * @param block whether to wait for the blocking read (if any) or not.
197 *
198 * @exception InterruptedException if this thread is interrupted while
199 * waiting.
200 */
201 public synchronized void stop(boolean block) throws InterruptedException {
202 if (!stopped) {
203 synchronized (lock) {
204 currentlyRunning = false;
205 stopped = true;
206 lock.notify();
207 }
208 }
209 if (block) {
210 join();
211 }
212 }
213
214 private class PipeOutputStream implements Runnable {
215 private final byte[] data;
216 private final InputStream in;
217 private final OutputStream out;
218
219 public PipeOutputStream(InputStream in, OutputStream out, int bufsize) {
220 data = new byte[bufsize];
221 this.in = in;
222 this.out = out;
223 }
224
225 @Override
226 public void run() {
227 try {
228 outer:
229 while (true) {
230 synchronized (lock) {
231 while (!currentlyRunning) {
232 if (stopped) {
233 break outer;
234 }
235 lock.wait();
236 lock.notify();
237 }
238 }
239 int count = in.read(data);
240 if (count < 0) {
241 break;
242 }
243 out.write(data, 0, count);
244 out.flush();
245 }
246 }
247 catch (Exception e) {
248 // Die silently for now.
249 }
250 }
251 }
252
253 private class PipeWriter implements Runnable {
254 private final char[] data;
255 private final Reader in;
256 private final Writer out;
257
258 public PipeWriter(Reader in, Writer out, int bufsize) {
259 data = new char[bufsize];
260 this.in = in;
261 this.out = out;
262 }
263
264 @Override
265 public void run() {
266 try {
267 outer:
268 while (true) {
269 synchronized (lock) {
270 while (!currentlyRunning) {
271 if (stopped) {
272 break outer;
273 }
274 lock.wait();
275 lock.notify();
276 }
277 }
278 int count = in.read(data);
279 if (count < 0) {
280 break;
281 }
282 out.write(data, 0, count);
283 out.flush();
284 }
285 }
286 catch (Exception e) {
287 // Die silently for now.
288 }
289 }
290 }
291 }