CountingInputStream.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  *   https://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */
  19. package org.apache.commons.compress.utils;

  20. import java.io.FilterInputStream;
  21. import java.io.IOException;
  22. import java.io.InputStream;

  23. /**
  24.  * Input stream that tracks the number of bytes read.
  25.  *
  26.  * @since 1.3
  27.  * @NotThreadSafe
  28.  * @deprecated Use {@link org.apache.commons.io.input.CountingInputStream}.
  29.  */
  30. @Deprecated
  31. public class CountingInputStream extends FilterInputStream {
  32.     private long bytesRead;

  33.     /**
  34.      * Creates a {@code CountingInputStream} by assigning the argument {@code in} to the field {@code this.in} so as to remember it for later use.
  35.      *
  36.      * @param in the underlying input stream, or {@code null} if this instance is to be created without an underlying stream.
  37.      */
  38.     public CountingInputStream(final InputStream in) {
  39.         super(in);
  40.     }

  41.     /**
  42.      * Increments the counter of already read bytes. Doesn't increment if the EOF has been hit (read == -1)
  43.      *
  44.      * @param read the number of bytes read
  45.      */
  46.     protected final void count(final long read) {
  47.         if (read != -1) {
  48.             bytesRead += read;
  49.         }
  50.     }

  51.     /**
  52.      * Returns the current number of bytes read from this stream.
  53.      *
  54.      * @return the number of read bytes
  55.      */
  56.     public long getBytesRead() {
  57.         return bytesRead;
  58.     }

  59.     @Override
  60.     public int read() throws IOException {
  61.         final int r = in.read();
  62.         if (r >= 0) {
  63.             count(1);
  64.         }
  65.         return r;
  66.     }

  67.     @Override
  68.     public int read(final byte[] b) throws IOException {
  69.         return read(b, 0, b.length);
  70.     }

  71.     @Override
  72.     public int read(final byte[] b, final int off, final int len) throws IOException {
  73.         if (len == 0) {
  74.             return 0;
  75.         }
  76.         final int r = in.read(b, off, len);
  77.         if (r >= 0) {
  78.             count(r);
  79.         }
  80.         return r;
  81.     }
  82. }