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 * https://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 /** 037 * Creates a {@code CountingOutputStream} filter built on top of the specified underlying output stream. 038 * 039 * @param out the underlying output stream to be assigned to the field {@code this.out} for later use, or {@code null} if this instance is to be created 040 * without an underlying stream. 041 */ 042 public CountingOutputStream(final OutputStream out) { 043 super(out); 044 } 045 046 /** 047 * Increments the counter of already written bytes. Doesn't increment if the EOF has been hit (written == -1) 048 * 049 * @param written the number of bytes written 050 */ 051 protected void count(final long written) { 052 if (written != -1) { 053 bytesWritten += written; 054 } 055 } 056 057 /** 058 * Returns the current number of bytes written to this stream. 059 * 060 * @return the number of written bytes 061 */ 062 public long getBytesWritten() { 063 return bytesWritten; 064 } 065 066 @Override 067 public void write(final byte[] b) throws IOException { 068 write(b, 0, b.length); 069 } 070 071 @Override 072 public void write(final byte[] b, final int off, final int len) throws IOException { 073 out.write(b, off, len); 074 count(len); 075 } 076 077 @Override 078 public void write(final int b) throws IOException { 079 out.write(b); 080 count(1); 081 } 082}