001: package com.quadcap.io;
002:
003: /* Copyright 2002 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.IOException;
042: import java.io.Writer;
043:
044: import java.util.HashMap;
045: import java.util.Iterator;
046:
047: /**
048: *
049: * @author Stan Bailes
050: */
051: public class TeeWriter extends Writer {
052: class WriterBinding {
053: Writer w;
054: boolean passFlush = true;
055: boolean passClose = true;
056: boolean autoFlush = false;
057: boolean enabled = true;
058:
059: WriterBinding(Writer w) {
060: this .w = w;
061: }
062: }
063:
064: HashMap writers = new HashMap();
065:
066: /**
067: * Public constructor
068: */
069: public TeeWriter() {
070: }
071:
072: public TeeWriter(Writer w) {
073: addWriter("", w);
074: }
075:
076: /**
077: * Write some characters to the buffer and the downstream writer
078: */
079: public void write(char[] buf, int off, int len) throws IOException {
080: Iterator iter = writers.values().iterator();
081: while (iter.hasNext()) {
082: WriterBinding wb = (WriterBinding) iter.next();
083: if (wb.enabled) {
084: wb.w.write(buf, off, len);
085: if (wb.autoFlush) {
086: wb.w.flush();
087: }
088: }
089: }
090: }
091:
092: /**
093: * Flush (the downstream writer)
094: */
095: public void flush() throws IOException {
096: Iterator iter = writers.values().iterator();
097: while (iter.hasNext()) {
098: WriterBinding wb = (WriterBinding) iter.next();
099: if (wb.enabled && wb.passFlush)
100: wb.w.flush();
101: }
102: }
103:
104: /**
105: * Close (the downstream writer)
106: */
107: public void close() throws IOException {
108: Iterator iter = writers.values().iterator();
109: while (iter.hasNext()) {
110: WriterBinding wb = (WriterBinding) iter.next();
111: if (wb.enabled && wb.passClose)
112: wb.w.close();
113: }
114: }
115:
116: /**
117: * Add a new writer
118: */
119: public void addWriter(String name, Writer w) {
120: WriterBinding wb = new WriterBinding(w);
121: writers.put(name, wb);
122: }
123:
124: /**
125: * Remove a writer
126: */
127: public void removeWriter(String name) {
128: writers.remove(name);
129: }
130:
131: public void setPassClose(String w, boolean b) throws IOException {
132: getBinding(w).passClose = b;
133: }
134:
135: public void setPassFlush(String w, boolean b) throws IOException {
136: getBinding(w).passFlush = b;
137: }
138:
139: public void setAutoFlush(String w, boolean b) throws IOException {
140: getBinding(w).autoFlush = b;
141: }
142:
143: public void setEnabled(String w, boolean b) throws IOException {
144: getBinding(w).enabled = b;
145: }
146:
147: final private WriterBinding getBinding(String w) throws IOException {
148: WriterBinding wb = (WriterBinding) writers.get(w);
149: if (wb == null) {
150: throw new IOException("No writer: " + w);
151: }
152: return wb;
153: }
154:
155: }
|