00001
package com.quadcap.io;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.IOException;
00042
import java.io.Writer;
00043
00044
import java.util.HashMap;
00045
import java.util.Iterator;
00046
00047
00048
00049
00050
00051 public class TeeWriter extends Writer {
00052 class WriterBinding {
00053 Writer
w;
00054 boolean passFlush =
true;
00055 boolean passClose =
true;
00056 boolean autoFlush =
false;
00057 boolean enabled =
true;
00058 WriterBinding(Writer w) {
this.w = w; }
00059 }
00060
00061 HashMap
writers =
new HashMap();
00062
00063
00064
00065
00066 public TeeWriter() {}
00067
00068 public TeeWriter(Writer w) {
addWriter(
"", w); }
00069
00070
00071
00072
00073 public void write(
char[] buf,
int off,
int len)
throws IOException {
00074 Iterator iter =
writers.values().iterator();
00075
while (iter.hasNext()) {
00076
WriterBinding wb = (
WriterBinding)iter.next();
00077
if (wb.
enabled) {
00078 wb.
w.write(buf, off, len);
00079
if (wb.
autoFlush) {
00080 wb.
w.flush();
00081 }
00082 }
00083 }
00084 }
00085
00086
00087
00088
00089 public void flush() throws IOException {
00090 Iterator iter =
writers.values().iterator();
00091
while (iter.hasNext()) {
00092
WriterBinding wb = (
WriterBinding)iter.next();
00093
if (wb.
enabled && wb.
passFlush) wb.
w.flush();
00094 }
00095 }
00096
00097
00098
00099
00100 public void close() throws IOException {
00101 Iterator iter =
writers.values().iterator();
00102
while (iter.hasNext()) {
00103
WriterBinding wb = (
WriterBinding)iter.next();
00104
if (wb.
enabled && wb.
passClose) wb.
w.close();
00105 }
00106 }
00107
00108
00109
00110
00111 public void addWriter(String name, Writer w) {
00112
WriterBinding wb =
new WriterBinding(w);
00113
writers.put(name, wb);
00114 }
00115
00116
00117
00118
00119 public void removeWriter(String name) {
00120
writers.remove(name);
00121 }
00122
00123 public void setPassClose(String w,
boolean b)
throws IOException {
00124
getBinding(w).
passClose = b;
00125 }
00126
00127 public void setPassFlush(String w,
boolean b)
throws IOException {
00128
getBinding(w).
passFlush = b;
00129 }
00130
00131 public void setAutoFlush(String w,
boolean b)
throws IOException {
00132
getBinding(w).
autoFlush = b;
00133 }
00134
00135 public void setEnabled(String w,
boolean b)
throws IOException {
00136
getBinding(w).
enabled = b;
00137 }
00138
00139 final private WriterBinding getBinding(String w)
throws IOException {
00140
WriterBinding wb = (
WriterBinding)
writers.get(w);
00141
if (wb == null) {
00142
throw new IOException(
"No writer: " + w);
00143 }
00144
return wb;
00145 }
00146
00147 }