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.FilterInputStream; 022import java.io.IOException; 023import java.io.InputStream; 024 025/** 026 * Input stream that tracks the number of bytes read. 027 * 028 * @since 1.3 029 * @NotThreadSafe 030 * @deprecated Use {@link org.apache.commons.io.input.CountingInputStream}. 031 */ 032@Deprecated 033public class CountingInputStream extends FilterInputStream { 034 private long bytesRead; 035 036 public CountingInputStream(final InputStream in) { 037 super(in); 038 } 039 040 /** 041 * Increments the counter of already read bytes. Doesn't increment if the EOF has been hit (read == -1) 042 * 043 * @param read the number of bytes read 044 */ 045 protected final void count(final long read) { 046 if (read != -1) { 047 bytesRead += read; 048 } 049 } 050 051 /** 052 * Returns the current number of bytes read from this stream. 053 * 054 * @return the number of read bytes 055 */ 056 public long getBytesRead() { 057 return bytesRead; 058 } 059 060 @Override 061 public int read() throws IOException { 062 final int r = in.read(); 063 if (r >= 0) { 064 count(1); 065 } 066 return r; 067 } 068 069 @Override 070 public int read(final byte[] b) throws IOException { 071 return read(b, 0, b.length); 072 } 073 074 @Override 075 public int read(final byte[] b, final int off, final int len) throws IOException { 076 if (len == 0) { 077 return 0; 078 } 079 final int r = in.read(b, off, len); 080 if (r >= 0) { 081 count(r); 082 } 083 return r; 084 } 085}