| 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 | |
| 21 | import java.io.FilterInputStream; |
| 22 | import java.io.IOException; |
| 23 | import java.io.InputStream; |
| 24 | |
| 25 | /** |
| 26 | * Input stream that tracks the number of bytes read. |
| 27 | * |
| 28 | * @since 1.3 |
| 29 | * @NotThreadSafe |
| 30 | * @deprecated Use {@link org.apache.commons.io.input.CountingInputStream}. |
| 31 | */ |
| 32 | @Deprecated |
| 33 | public class CountingInputStream extends FilterInputStream { |
| 34 | private long bytesRead; |
| 35 | |
| 36 | /** |
| 37 | * Creates a {@code CountingInputStream} by assigning the argument {@code in} to the field {@code this.in} so as to remember it for later use. |
| 38 | * |
| 39 | * @param in the underlying input stream, or {@code null} if this instance is to be created without an underlying stream. |
| 40 | */ |
| 41 | public CountingInputStream(final InputStream in) { |
| 42 | super(in); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Increments the counter of already read bytes. Doesn't increment if the EOF has been hit (read == -1) |
| 47 | * |
| 48 | * @param read the number of bytes read |
| 49 | */ |
| 50 | protected final void count(final long read) { |
| 51 | if (read != -1) { |
| 52 | bytesRead += read; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Returns the current number of bytes read from this stream. |
| 58 | * |
| 59 | * @return the number of read bytes |
| 60 | */ |
| 61 | public long getBytesRead() { |
| 62 | return bytesRead; |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public int read() throws IOException { |
| 67 | final int r = in.read(); |
| 68 | if (r >= 0) { |
| 69 | count(1); |
| 70 | } |
| 71 | return r; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public int read(final byte[] b) throws IOException { |
| 76 | return read(b, 0, b.length); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public int read(final byte[] b, final int off, final int len) throws IOException { |
| 81 | if (len == 0) { |
| 82 | return 0; |
| 83 | } |
| 84 | final int r = in.read(b, off, len); |
| 85 | if (r >= 0) { |
| 86 | count(r); |
| 87 | } |
| 88 | return r; |
| 89 | } |
| 90 | } |