CountingInputStream.java

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

  18. import static org.apache.commons.io.IOUtils.EOF;

  19. import java.io.IOException;
  20. import java.io.InputStream;

  21. /**
  22.  * A decorating input stream that counts the number of bytes that have passed
  23.  * through the stream so far.
  24.  * <p>
  25.  * A typical use case would be during debugging, to ensure that data is being
  26.  * read as expected.
  27.  * </p>
  28.  * @deprecated Use {@link BoundedInputStream} (unbounded by default).
  29.  */
  30. @Deprecated
  31. public class CountingInputStream extends ProxyInputStream {

  32.     /** The count of bytes read. */
  33.     private long count;

  34.     /**
  35.      * Constructs a new CountingInputStream.
  36.      *
  37.      * @param in  the InputStream to delegate to
  38.      */
  39.     public CountingInputStream(final InputStream in) {
  40.         super(in);
  41.     }

  42.     CountingInputStream(final InputStream in, final ProxyInputStream.AbstractBuilder<?, ?> builder) {
  43.         super(in, builder);
  44.     }

  45.     CountingInputStream(final ProxyInputStream.AbstractBuilder<?, ?> builder) throws IOException {
  46.         super(builder);
  47.     }

  48.     /**
  49.      * Adds the number of read bytes to the count.
  50.      *
  51.      * @param n number of bytes read, or -1 if no more bytes are available
  52.      * @throws IOException Not thrown here but subclasses may throw.
  53.      * @since 2.0
  54.      */
  55.     @Override
  56.     protected synchronized void afterRead(final int n) throws IOException {
  57.         if (n != EOF) {
  58.             count += n;
  59.         }
  60.         super.afterRead(n);
  61.     }

  62.     /**
  63.      * Gets number of bytes that have passed through this stream.
  64.      * <p>
  65.      * NOTE: This method is an alternative for {@code getCount()}
  66.      * and was added because that method returns an integer which will
  67.      * result in incorrect count for files over 2GB.
  68.      * </p>
  69.      *
  70.      * @return the number of bytes accumulated
  71.      * @since 1.3
  72.      */
  73.     public synchronized long getByteCount() {
  74.         return count;
  75.     }

  76.     /**
  77.      * Gets number of bytes that have passed through this stream.
  78.      * <p>
  79.      * This method throws an ArithmeticException if the
  80.      * count is greater than can be expressed by an {@code int}.
  81.      * See {@link #getByteCount()} for a method using a {@code long}.
  82.      * </p>
  83.      *
  84.      * @return the number of bytes accumulated
  85.      * @throws ArithmeticException if the byte count is too large
  86.      * @deprecated Use {@link #getByteCount()}.
  87.      */
  88.     @Deprecated
  89.     public int getCount() {
  90.         final long result = getByteCount();
  91.         if (result > Integer.MAX_VALUE) {
  92.             throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int");
  93.         }
  94.         return (int) result;
  95.     }

  96.     /**
  97.      * Resets the byte count back to 0.
  98.      * <p>
  99.      * NOTE: This method is an alternative for {@code resetCount()}
  100.      * and was added because that method returns an integer which will
  101.      * result in incorrect count for files over 2GB.
  102.      * </p>
  103.      *
  104.      * @return the count previous to resetting
  105.      * @since 1.3
  106.      */
  107.     public synchronized long resetByteCount() {
  108.         final long tmp = count;
  109.         count = 0;
  110.         return tmp;
  111.     }

  112.     /**
  113.      * Resets the byte count back to 0.
  114.      * <p>
  115.      * This method throws an ArithmeticException if the
  116.      * count is greater than can be expressed by an {@code int}.
  117.      * See {@link #resetByteCount()} for a method using a {@code long}.
  118.      * </p>
  119.      *
  120.      * @return the count previous to resetting
  121.      * @throws ArithmeticException if the byte count is too large
  122.      * @deprecated Use {@link #resetByteCount()}.
  123.      */
  124.     @Deprecated
  125.     public int resetCount() {
  126.         final long result = resetByteCount();
  127.         if (result > Integer.MAX_VALUE) {
  128.             throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int");
  129.         }
  130.         return (int) result;
  131.     }

  132.     /**
  133.      * Skips the stream over the specified number of bytes, adding the skipped
  134.      * amount to the count.
  135.      *
  136.      * @param length  the number of bytes to skip
  137.      * @return the actual number of bytes skipped
  138.      * @throws IOException if an I/O error occurs.
  139.      * @see java.io.InputStream#skip(long)
  140.      */
  141.     @Override
  142.     public synchronized long skip(final long length) throws IOException {
  143.         final long skip = super.skip(length);
  144.         count += skip;
  145.         return skip;
  146.     }

  147. }