clear code by converting AtomicLong to long as GZIPInputStream is designed for single...
[idea/community.git] / platform / platform-api / src / com / intellij / util / io / CountingGZIPInputStream.java
1 /*
2  * Copyright 2000-2016 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.intellij.util.io;
17
18 import org.jetbrains.annotations.NotNull;
19
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.zip.GZIPInputStream;
23
24 /**
25  * A stream for reading compressed data in the GZIP file format.
26  * Total amount of compressed read bytes can be accessed via {@link #getCompressedBytesRead()}.
27  *
28  * Note that this implementation is not thread safe.
29  */
30 public class CountingGZIPInputStream extends GZIPInputStream {
31   private final CountingInputStream myInputStream;
32
33   private CountingGZIPInputStream(@NotNull CountingInputStream inputStream) throws IOException {
34     super(inputStream);
35     myInputStream = inputStream;
36   }
37
38   public long getCompressedBytesRead() {
39     return myInputStream.myBytesRead;
40   }
41
42   @NotNull
43   public static CountingGZIPInputStream create(@NotNull InputStream inputStream) throws IOException {
44     return new CountingGZIPInputStream(new CountingInputStream(inputStream));
45   }
46
47   private static class CountingInputStream extends InputStream {
48     private final InputStream myInputStream;
49     private long myBytesRead = 0;
50
51     public CountingInputStream(@NotNull InputStream inputStream) {
52       myInputStream = inputStream;
53     }
54
55     public int read() throws IOException {
56       int data = myInputStream.read();
57       myBytesRead++;
58       return data;
59     }
60
61     @Override
62     public int read(@NotNull byte[] b) throws IOException {
63       int bytesRead = myInputStream.read(b);
64       myBytesRead += bytesRead;
65       return bytesRead;
66     }
67
68     @Override
69     public int read(@NotNull byte[] b, int off, int len) throws IOException {
70       int bytesRead = myInputStream.read(b, off, len);
71       myBytesRead += bytesRead;
72       return bytesRead;
73     }
74
75     public long skip(long n) throws IOException {
76       long bytesSkipped = myInputStream.skip(n);
77       myBytesRead += bytesSkipped;
78       return bytesSkipped;
79     }
80
81     public int available() throws IOException {
82       return myInputStream.available();
83     }
84
85     public void close() throws IOException {
86       myInputStream.close();
87     }
88
89     @Override
90     public synchronized void mark(int readlimit) {
91       myInputStream.mark(readlimit);
92     }
93
94     @Override
95     public synchronized void reset() throws IOException {
96       myInputStream.reset();
97     }
98
99     @Override
100     public boolean markSupported() {
101       return myInputStream.markSupported();
102     }
103   }
104 }