a5affad9f14a677d7eeed828ebc49a39648e2de2
[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.concurrent.atomic.AtomicLong;
23 import java.util.zip.GZIPInputStream;
24
25 public class CountingGZIPInputStream extends GZIPInputStream {
26   private final CountingInputStream myInputStream;
27
28   private CountingGZIPInputStream(@NotNull CountingInputStream inputStream) throws IOException {
29     super(inputStream);
30     myInputStream = inputStream;
31   }
32
33   public long getCompressedBytesRead() {
34     return myInputStream.myBytesRead.get();
35   }
36
37   @NotNull
38   public static CountingGZIPInputStream create(@NotNull InputStream inputStream) throws IOException {
39     return new CountingGZIPInputStream(new CountingInputStream(inputStream));
40   }
41
42   private static class CountingInputStream extends InputStream {
43     private final InputStream myInputStream;
44     private final AtomicLong myBytesRead = new AtomicLong(0);
45
46     public CountingInputStream(@NotNull InputStream inputStream) {
47       myInputStream = inputStream;
48     }
49
50     public int read() throws IOException {
51       int data = myInputStream.read();
52       myBytesRead.incrementAndGet();
53       return data;
54     }
55
56     @Override
57     public int read(@NotNull byte[] b) throws IOException {
58       int bytesRead = myInputStream.read(b);
59       myBytesRead.addAndGet(bytesRead);
60       return bytesRead;
61     }
62
63     @Override
64     public int read(@NotNull byte[] b, int off, int len) throws IOException {
65       int bytesRead = myInputStream.read(b, off, len);
66       myBytesRead.addAndGet(bytesRead);
67       return bytesRead;
68     }
69
70     public long skip(long n) throws IOException {
71       long bytesSkipped = myInputStream.skip(n);
72       myBytesRead.addAndGet(bytesSkipped);
73       return bytesSkipped;
74     }
75
76     public int available() throws IOException {
77       return myInputStream.available();
78     }
79
80     public void close() throws IOException {
81       myInputStream.close();
82     }
83
84     @Override
85     public synchronized void mark(int readlimit) {
86       myInputStream.mark(readlimit);
87     }
88
89     @Override
90     public synchronized void reset() throws IOException {
91       myInputStream.reset();
92     }
93
94     @Override
95     public boolean markSupported() {
96       return myInputStream.markSupported();
97     }
98   }
99 }