001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.commons.compress.utils;
020
021import java.io.FilterOutputStream;
022import java.io.IOException;
023import java.io.OutputStream;
024
025/**
026 * Stream that tracks the number of bytes read.
027 *
028 * @since 1.3
029 * @NotThreadSafe
030 * @deprecated Use {@link org.apache.commons.io.output.CountingOutputStream}.
031 */
032@Deprecated
033public class CountingOutputStream extends FilterOutputStream {
034    private long bytesWritten;
035
036    public CountingOutputStream(final OutputStream out) {
037        super(out);
038    }
039
040    /**
041     * Increments the counter of already written bytes. Doesn't increment if the EOF has been hit (written == -1)
042     *
043     * @param written the number of bytes written
044     */
045    protected void count(final long written) {
046        if (written != -1) {
047            bytesWritten += written;
048        }
049    }
050
051    /**
052     * Returns the current number of bytes written to this stream.
053     *
054     * @return the number of written bytes
055     */
056    public long getBytesWritten() {
057        return bytesWritten;
058    }
059
060    @Override
061    public void write(final byte[] b) throws IOException {
062        write(b, 0, b.length);
063    }
064
065    @Override
066    public void write(final byte[] b, final int off, final int len) throws IOException {
067        out.write(b, off, len);
068        count(len);
069    }
070
071    @Override
072    public void write(final int b) throws IOException {
073        out.write(b);
074        count(1);
075    }
076}