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.OutputStream;
023
024/**
025 * 
026 */
027public final class CountingOutputStream extends OutputStream {
028    
029    private final OutputStream os;
030    private int count=0;
031
032    /**
033     * @param os
034     */
035    public CountingOutputStream(OutputStream os) {
036        this.os=os;
037    }
038    
039    @Override
040    public void close() throws IOException {
041        os.close();
042    }
043
044    @Override
045    public void flush() throws IOException {
046        os.flush();
047    }
048
049    @Override
050    public void write(byte[] b, int off, int len) throws IOException {
051        count+=len;
052        os.write(b, off, len);
053    }
054
055    @Override
056    public void write(byte[] b) throws IOException {
057        count+=b.length;
058        os.write(b);
059    }
060
061    @Override
062    public void write(int b) throws IOException {
063        count++;
064        os.write(b);
065    }
066
067    /**
068     * @return Returns the count.
069     */
070    public int getCount() {
071        return count;
072    }
073
074}