001: /*==============================================================================
002: * Copyright 1996-2000 Texas Instruments Inc. All rights reserved.
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package ti.io;
020:
021: import java.io.Writer;
022: import java.io.IOException;
023:
024: /**
025: * This is a writer that can split what is written to it to two
026: * different output Writers.
027: *
028: * @author Rob Clark
029: * @version 0.1
030: */
031: public class TeeWriter extends Writer {
032: private Writer w1;
033: private Writer w2;
034:
035: /*=======================================================================*/
036: /**
037: * Class Constructor.
038: *
039: * @param w1 the first of the two output writers
040: * @param w2 the second of the two output writers
041: */
042: public TeeWriter(Writer w1, Writer w2) {
043: this .w1 = w1;
044: this .w2 = w2;
045: }
046:
047: /*=======================================================================*/
048: /**
049: * Write a portion of an array of characters.
050: *
051: * @param cbuf Array of characters
052: * @param off Offset from which to start writing characters
053: * @param len Number of characters to write
054: * @exception IOException If an I/O error occurs
055: */
056: public void write(char[] cbuf, int off, int len) throws IOException {
057: if (w1 != null) {
058: w1.write(cbuf, off, len);
059: }
060:
061: if (w2 != null) {
062: w2.write(cbuf, off, len);
063: }
064: }
065:
066: /*=======================================================================*/
067: /**
068: * Flush both the output writers.
069: *
070: * @exception IOException - If an I/O error occurs
071: */
072: public void flush() throws IOException {
073: if (w1 != null) {
074: w1.flush();
075: }
076:
077: if (w2 != null) {
078: w2.flush();
079: }
080: }
081:
082: /*=======================================================================*/
083: /**
084: * Close both the output writers.
085: *
086: * @exception IOException - If an I/O error occurs
087: */
088: public void close() throws IOException {
089: if (w1 != null) {
090: w1.close();
091: }
092:
093: if (w2 != null) {
094: w2.close();
095: }
096: }
097:
098: /*=======================================================================*/
099: /**
100: * Set the primary output writer.
101: * @param w1 the first of the two output writers
102: */
103: public void setPrimaryWriter(Writer w1) {
104: this .w1 = w1;
105: }
106:
107: /*=======================================================================*/
108: /**
109: * Set the secondary output writer.
110: * @param w2 the second of the two output writers
111: */
112: public void setSecondaryWriter(Writer w2) {
113: this.w2 = w2;
114: }
115: }
|