Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 15 | arune | 1 | package Macbeth.Utilities; |
| 2 | |||
| 3 | import java.io.PrintStream; |
||
| 4 | import java.io.PipedOutputStream; |
||
| 5 | import java.io.PrintWriter; |
||
| 6 | import java.util.List; |
||
| 7 | import java.util.Iterator; |
||
| 8 | import java.util.ArrayList; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * An adapter whose output is connected to multiple other streams. |
||
| 12 | * @author Jimmy |
||
| 13 | */ |
||
| 14 | public class MultipleStreamsAdapter extends PrintStream { |
||
| 15 | //a list of underlying PrintStreams |
||
| 16 | private List streams; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Creates a new instance of MultipleStreamsAdapter |
||
| 20 | * |
||
| 21 | * @param streams A list of PrintStreams to which this adapter should forward its data. |
||
| 22 | */ |
||
| 23 | public MultipleStreamsAdapter(List streams) { |
||
| 24 | //construct PrintWriter with non-used PipedOutputStream |
||
| 25 | super(new PipedOutputStream()); |
||
| 26 | //save reference to underlying stream |
||
| 27 | this.streams = streams; |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Creates a new instance of MultipleStreamsAdapter. |
||
| 32 | * @param stream1 A stream that immediately should be connected to the output of this adapter. |
||
| 33 | * @param stream2 A stream that immediately should be connected to the output of this adapter. |
||
| 34 | */ |
||
| 35 | public MultipleStreamsAdapter(PrintStream stream1, PrintStream stream2) { |
||
| 36 | this(); |
||
| 37 | streams.add(stream1); |
||
| 38 | streams.add(stream2); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Creates a new instance of MultipleStreamsAdapter. |
||
| 43 | */ |
||
| 44 | public MultipleStreamsAdapter() { |
||
| 45 | this(new ArrayList()); |
||
| 46 | } |
||
| 47 | |||
| 48 | |||
| 49 | /** |
||
| 50 | * Connects another stream to the output of this adapter. |
||
| 51 | * @param stream The stream. |
||
| 52 | */ |
||
| 53 | public void addStream(PrintStream stream) { |
||
| 54 | streams.add(stream); |
||
| 55 | } |
||
| 56 | |||
| 57 | |||
| 58 | /** |
||
| 59 | * Disconnects a stream from the output of this adapter. |
||
| 60 | * @param stream |
||
| 61 | */ |
||
| 62 | public void removeStream(PrintStream stream) { |
||
| 63 | streams.remove(stream); |
||
| 64 | } |
||
| 65 | |||
| 66 | |||
| 67 | private void distributeString(String str) { |
||
| 68 | Iterator it = streams.iterator(); |
||
| 69 | while (it.hasNext()) { |
||
| 70 | PrintStream p = (PrintStream) it.next(); |
||
| 71 | p.print(str); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | |||
| 76 | /* Override all standard print-methods: */ |
||
| 77 | |||
| 78 | public void println(char c[]) { |
||
| 79 | String str = new String(c, 0, c.length) + MySystem.lineBreak; |
||
| 80 | if (c != null) { |
||
| 81 | distributeString(str); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | public void print(char c[]) { |
||
| 86 | String str = new String(c, 0, c.length); |
||
| 87 | if (c != null) { |
||
| 88 | distributeString(str); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | public void println(double d) { |
||
| 93 | String str = d + MySystem.lineBreak; |
||
| 94 | distributeString(str); |
||
| 95 | } |
||
| 96 | |||
| 97 | public void print(double d) { |
||
| 98 | String str = Double.toString(d); |
||
| 99 | distributeString(str); |
||
| 100 | } |
||
| 101 | |||
| 102 | public void println(float f) { |
||
| 103 | String str = f + MySystem.lineBreak; |
||
| 104 | distributeString(str); |
||
| 105 | } |
||
| 106 | |||
| 107 | public void print(float f) { |
||
| 108 | String str = Float.toString(f); |
||
| 109 | distributeString(str); |
||
| 110 | } |
||
| 111 | |||
| 112 | public void println(char c) { |
||
| 113 | String str = c + MySystem.lineBreak; |
||
| 114 | distributeString(str); |
||
| 115 | } |
||
| 116 | |||
| 117 | public void print(char c) { |
||
| 118 | String str = Character.toString(c); |
||
| 119 | distributeString(str); |
||
| 120 | } |
||
| 121 | |||
| 122 | public void println(boolean b) { |
||
| 123 | String str = b + MySystem.lineBreak; |
||
| 124 | distributeString(str); |
||
| 125 | } |
||
| 126 | |||
| 127 | public void print(boolean b) { |
||
| 128 | String str = Boolean.toString(b); |
||
| 129 | distributeString(str); |
||
| 130 | } |
||
| 131 | |||
| 132 | public void println(int i) { |
||
| 133 | String str = i + MySystem.lineBreak; |
||
| 134 | distributeString(str); |
||
| 135 | } |
||
| 136 | |||
| 137 | public void print(int i) { |
||
| 138 | String str = Integer.toString(i); |
||
| 139 | distributeString(str); |
||
| 140 | } |
||
| 141 | |||
| 142 | public void println(String str) { |
||
| 143 | String string = (str != null ? str : "null") + MySystem.lineBreak; |
||
| 144 | distributeString(str); |
||
| 145 | } |
||
| 146 | |||
| 147 | public void print(String str) { |
||
| 148 | String string = (str != null ? str : "null"); |
||
| 149 | distributeString(str); |
||
| 150 | } |
||
| 151 | |||
| 152 | public void println(Object obj) { |
||
| 153 | String str = (obj != null ? obj.toString() : "null") + MySystem.lineBreak; |
||
| 154 | distributeString(str); |
||
| 155 | } |
||
| 156 | |||
| 157 | public void print(Object obj) { |
||
| 158 | String str = (obj != null ? obj.toString() : "null"); |
||
| 159 | distributeString(str); |
||
| 160 | } |
||
| 161 | |||
| 162 | } |