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.compressors;
20
21 import java.io.InputStream;
22
23 /**
24 * Abstracts services for all compressor input streams.
25 */
26 public abstract class CompressorInputStream extends InputStream {
27
28 private long bytesRead;
29
30 /**
31 * Constructs a new instance.
32 */
33 public CompressorInputStream() {
34 // empty
35 }
36
37 /**
38 * Increments the counter of already read bytes. Doesn't increment if the EOF has been hit (read == -1)
39 *
40 * @param read the number of bytes read
41 * @since 1.1
42 */
43 protected void count(final int read) {
44 count((long) read);
45 }
46
47 /**
48 * Increments the counter of already read bytes. Doesn't increment if the EOF has been hit (read == -1)
49 *
50 * @param read the number of bytes read
51 */
52 protected void count(final long read) {
53 if (read != -1) {
54 bytesRead += read;
55 }
56 }
57
58 /**
59 * Gets the current number of bytes read from this stream.
60 *
61 * @return the number of read bytes
62 * @since 1.1
63 */
64 public long getBytesRead() {
65 return bytesRead;
66 }
67
68 /**
69 * Gets the current number of bytes read from this stream.
70 *
71 * @return the number of read bytes
72 * @deprecated this method may yield wrong results for large archives, use #getBytesRead instead
73 */
74 @Deprecated
75 public int getCount() {
76 return (int) bytesRead;
77 }
78
79 /**
80 * Gets the amount of raw or compressed bytes read by the stream.
81 *
82 * <p>
83 * This implementation invokes {@link #getBytesRead}.
84 * </p>
85 *
86 * <p>
87 * Provides half of {@link org.apache.commons.compress.utils.InputStreamStatistics} without forcing subclasses to implement the other half.
88 * </p>
89 *
90 * @return the amount of decompressed bytes returned by the stream
91 * @since 1.17
92 */
93 public long getUncompressedCount() {
94 return getBytesRead();
95 }
96
97 /**
98 * Decrements the counter of already read bytes.
99 *
100 * @param pushedBack the number of bytes pushed back.
101 * @since 1.7
102 */
103 protected void pushedBackBytes(final long pushedBack) {
104 bytesRead -= pushedBack;
105 }
106 }