| 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.Writer; |
| 19 |
import java.io.Reader; |
| 20 |
import java.io.Closeable; |
| 21 |
import java.io.IOException; |
| 22 |
|
| 23 |
/** |
| 24 |
* A ClosingPipe is a link between an input stream and an output stream or a |
| 25 |
* reader or writer: The pipe, once started, will asynchronously redirect all |
| 26 |
* data received from the input source to the output source until it is paused, |
| 27 |
* stopped or the input source is empty. When the input source has been closed, |
| 28 |
* or the pipe has been stopped, a ClosingPipe will close the output source |
| 29 |
* afterwards. |
| 30 |
* <p> |
| 31 |
* Common usage of pipes includes asynchronous zipping of data from two data |
| 32 |
* sources, redirecting output and input to subprocesses and for loggers you |
| 33 |
* want to just pipe from and input source. |
| 34 |
* |
| 35 |
* @author Jean Niklas L'orange |
| 36 |
* @since <code>com.hypirion.io 0.2.0</code> |
| 37 |
*/ |
| 38 |
public class ClosingPipe extends Pipe { |
| 39 |
|
| 40 |
protected final Thread closer; |
| 41 |
|
| 42 |
/** |
| 43 |
* Creates a new closing pipe, which redirects data from the stream |
| 44 |
* <code>in</code> to the stream <code>out</code> once started. |
| 45 |
* |
| 46 |
* @param in the input stream to read from. |
| 47 |
* @param out the output stream to write out to. |
| 48 |
*/ |
| 49 |
public ClosingPipe(InputStream in, OutputStream out) { |
| 50 |
this(in, out, DEFAULT_BUFFER_SIZE); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Creates a new closing pipe with buffer size <code>bufsize</code>, which |
| 55 |
* redirects data from the stream <code>in</code> to the stream |
| 56 |
* <code>out</code> once started. |
| 57 |
* |
| 58 |
* @param in the input stream to read from. |
| 59 |
* @param out the output stream to write out to. |
| 60 |
* @param bufsize the buffer size of the pipe. |
| 61 |
*/ |
| 62 |
public ClosingPipe(InputStream in, OutputStream out, int bufsize) { |
| 63 |
super(in, out, bufsize); |
| 64 |
closer = setupCloser(out); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Creates a new closing pipe, which redirects data from the reader |
| 69 |
* <code>in</code> to the writer <code>out</code> once started. |
| 70 |
* |
| 71 |
* @param in the reader to read from. |
| 72 |
* @param out the writer to write out to. |
| 73 |
*/ |
| 74 |
public ClosingPipe(Reader in, Writer out) { |
| 75 |
this(in, out, DEFAULT_BUFFER_SIZE); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Creates a new closing pipe with buffer size <code>bufsize</code>, which |
| 80 |
* redirects data from the reader <code>in</code> to the writer |
| 81 |
* <code>out</code> once started. |
| 82 |
* |
| 83 |
* @param in the reader to read from. |
| 84 |
* @param out the writer to write out to. |
| 85 |
* @param bufsize the buffer size of the pipe. |
| 86 |
*/ |
| 87 |
public ClosingPipe(Reader in, Writer out, int bufsize) { |
| 88 |
super(in, out, bufsize); |
| 89 |
closer = setupCloser(out); |
| 90 |
} |
| 91 |
|
| 92 |
private Thread setupCloser(Closeable out) { |
| 93 |
Runnable pt = new OutCloser(out); |
| 94 |
Thread closer = new Thread(pt); |
| 95 |
closer.setName(String.format("ClosingPipeCloser %d", this.hashCode())); |
| 96 |
closer.setDaemon(true); |
| 97 |
closer.start(); |
| 98 |
return closer; |
| 99 |
} |
| 100 |
|
| 101 |
private class OutCloser implements Runnable { |
| 102 |
private final Closeable out; |
| 103 |
|
| 104 |
public OutCloser(Closeable out) { |
| 105 |
this.out = out; |
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
public void run() { |
| 110 |
try { |
| 111 |
ClosingPipe.this.join(); |
| 112 |
out.close(); |
| 113 |
} |
| 114 |
catch (InterruptedException ie) { |
| 115 |
run(); // We're not giving up that easily. |
| 116 |
} |
| 117 |
catch (IOException ioe) { |
| 118 |
// The closable is somehow broken, leave it be. |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
} |