1 |
package tools; |
2 |
|
3 |
import java.io.IOException; |
4 |
import java.io.OutputStream; |
5 |
|
6 |
/** |
7 |
* Package: tools |
8 |
* Class: MultiOutputStream |
9 |
* Author: Henning Stehr, stehr@molgen.mpg.de |
10 |
* Date: 9/Mar/2006 |
11 |
* |
12 |
* An OutputStream which allows to write to multiple output streams. |
13 |
* Invoking a method of MultiOutputStream invokes the same method |
14 |
* with the same parameters in each of the specified output streams. |
15 |
* |
16 |
* Code example: |
17 |
* url1 = "http://gelb:9123"; |
18 |
* url2 = "http://blau:9123"; |
19 |
* file1 = "test.log"; |
20 |
* OutputStream os1 = new PymolServerOutputStream(url1); |
21 |
* OutputStream os2 = new PymolServerOutputStream(url2); |
22 |
* OutputStream os3 = new FileOutputStream(file1); |
23 |
* OutputStream multi = new MultiOutputStream(os1, os2, os3); |
24 |
* PrintWriter multiOut = new PrintWriter(multi, true); |
25 |
* multiOut.println("load /project/StruPPi/PDBs/mainPDB/1RX4.pdb, hello"); |
26 |
* |
27 |
* Changelog: |
28 |
* 2006/03/09 first created by HS |
29 |
*/ |
30 |
public class MultiOutputStream extends OutputStream { |
31 |
|
32 |
OutputStream[] streams; |
33 |
|
34 |
/** |
35 |
* Creates a new MultiOutputStream. |
36 |
* @param outputStreams The output streams which should be contained |
37 |
* in this MultiOutputStream. |
38 |
*/ |
39 |
public MultiOutputStream(OutputStream...outputStreams) { |
40 |
this.streams = outputStreams; |
41 |
} |
42 |
|
43 |
/** |
44 |
* Writes a byte to all output streams contained in this |
45 |
* MultiOutputStream. |
46 |
* @param The byte to be written to the output streams |
47 |
*/ |
48 |
@Override |
49 |
public void write(int arg0) throws IOException { |
50 |
for(OutputStream stream : streams) { |
51 |
stream.write(arg0); |
52 |
} |
53 |
} |
54 |
|
55 |
/** |
56 |
* Flushes all output streams contained in this |
57 |
* MultiOutputStream. |
58 |
*/ |
59 |
@Override |
60 |
public void flush() throws IOException { |
61 |
for(OutputStream stream : streams) { |
62 |
stream.flush(); |
63 |
} |
64 |
} |
65 |
|
66 |
/** |
67 |
* Closes all output streams contained in this |
68 |
* MultiOutputStream. |
69 |
*/ |
70 |
@Override |
71 |
public void close() throws IOException { |
72 |
for(OutputStream stream : streams) { |
73 |
stream.close(); |
74 |
} |
75 |
} |
76 |
|
77 |
} |