001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.commons.io;
020
021import java.io.IOException;
022import java.io.Writer;
023
024public class ForkWriter extends Writer {
025
026        private final Writer w1;
027        private final Writer w2;
028
029        public ForkWriter(Writer w1, Writer w2) {
030                this.w1=w1;
031                this.w2=w2;
032        }
033
034        @Override
035        public Writer append(char c) throws IOException {
036                try {
037                        w1.write(c);
038                }
039                finally {
040                        w2.write(c);
041                }
042                return this;
043        }
044
045        @Override
046        public Writer append(CharSequence csq, int start, int end) throws IOException {
047                try {
048                        w1.write(csq.toString(), start, end);
049                }
050                finally {
051                        w2.write(csq.toString(), start, end);
052                }
053                return this;
054        }
055
056        @Override
057        public Writer append(CharSequence csq) throws IOException {
058                try {
059                        w1.write(csq.toString());
060                }
061                finally {
062                        w2.write(csq.toString());
063                }
064                return this;
065        }
066
067        @Override
068        public void write(char[] cbuf) throws IOException {
069                try {
070                        w1.write(cbuf);
071                }
072                finally {
073                        w2.write(cbuf);
074                }
075        }
076
077        @Override
078        public void write(int c) throws IOException {
079                try {
080                        w1.write(c);
081                }
082                finally {
083                        w2.write(c);
084                }
085        }
086
087        @Override
088        public void write(String str, int off, int len) throws IOException {
089                try {
090                        w1.write(str, off, len);
091                }
092                finally {
093                        w2.write(str, off, len);
094                }
095        }
096
097        @Override
098        public void write(String str) throws IOException {
099                try {
100                        w1.write(str);
101                }
102                finally {
103                        w2.write(str);
104                }
105        }
106
107        @Override
108        public void close() throws IOException {
109                try {
110                        w1.close();
111                }
112                finally {
113                        w2.close();
114                }
115        }
116
117        @Override
118        public void flush() throws IOException {
119
120                try {
121                        w1.flush();
122                }
123                finally {
124                        w2.flush();
125                }
126        }
127
128        @Override
129        public void write(char[] cbuf, int off, int len) throws IOException {
130
131                try {
132                        w1.write(cbuf, off, len);
133                }
134                finally {
135                        w2.write(cbuf, off, len);
136                }
137        }
138
139}