simplify read bytes counting (following IDEA-CR-12695)
authorSergey Simonchik <sergey.simonchik@jetbrains.com>
Wed, 10 Aug 2016 21:52:08 +0000 (00:52 +0300)
committerSergey Simonchik <sergey.simonchik@jetbrains.com>
Wed, 10 Aug 2016 21:52:08 +0000 (00:52 +0300)
platform/platform-api/src/com/intellij/util/io/CountingGZIPInputStream.java [moved from platform/platform-api/src/com/intellij/util/io/CompressedBytesReadAwareGZIPInputStream.java with 66% similarity]
platform/platform-api/src/com/intellij/util/io/HttpRequests.java
platform/platform-api/src/com/intellij/util/net/NetUtils.java

similarity index 66%
rename from platform/platform-api/src/com/intellij/util/io/CompressedBytesReadAwareGZIPInputStream.java
rename to platform/platform-api/src/com/intellij/util/io/CountingGZIPInputStream.java
index 6c1a487e1ee14a34377e09f6a84963830d30e81c..a5affad9f14a677d7eeed828ebc49a39648e2de2 100644 (file)
@@ -22,10 +22,10 @@ import java.io.InputStream;
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.zip.GZIPInputStream;
 
-public class CompressedBytesReadAwareGZIPInputStream extends GZIPInputStream {
-  private final BytesReadAwareInputStream myInputStream;
+public class CountingGZIPInputStream extends GZIPInputStream {
+  private final CountingInputStream myInputStream;
 
-  private CompressedBytesReadAwareGZIPInputStream(@NotNull BytesReadAwareInputStream inputStream) throws IOException {
+  private CountingGZIPInputStream(@NotNull CountingInputStream inputStream) throws IOException {
     super(inputStream);
     myInputStream = inputStream;
   }
@@ -35,45 +35,41 @@ public class CompressedBytesReadAwareGZIPInputStream extends GZIPInputStream {
   }
 
   @NotNull
-  public static CompressedBytesReadAwareGZIPInputStream create(@NotNull InputStream inputStream) throws IOException {
-    return new CompressedBytesReadAwareGZIPInputStream(new BytesReadAwareInputStream(inputStream));
+  public static CountingGZIPInputStream create(@NotNull InputStream inputStream) throws IOException {
+    return new CountingGZIPInputStream(new CountingInputStream(inputStream));
   }
 
-  private static class BytesReadAwareInputStream extends InputStream {
+  private static class CountingInputStream extends InputStream {
     private final InputStream myInputStream;
     private final AtomicLong myBytesRead = new AtomicLong(0);
 
-    public BytesReadAwareInputStream(@NotNull InputStream inputStream) {
+    public CountingInputStream(@NotNull InputStream inputStream) {
       myInputStream = inputStream;
     }
 
     public int read() throws IOException {
-      long bytesReadBefore = myBytesRead.get();
       int data = myInputStream.read();
-      myBytesRead.compareAndSet(bytesReadBefore, bytesReadBefore + 1);
+      myBytesRead.incrementAndGet();
       return data;
     }
 
     @Override
     public int read(@NotNull byte[] b) throws IOException {
-      long bytesReadBefore = myBytesRead.get();
       int bytesRead = myInputStream.read(b);
-      myBytesRead.compareAndSet(bytesReadBefore, bytesReadBefore + bytesRead);
+      myBytesRead.addAndGet(bytesRead);
       return bytesRead;
     }
 
     @Override
     public int read(@NotNull byte[] b, int off, int len) throws IOException {
-      long bytesReadBefore = myBytesRead.get();
       int bytesRead = myInputStream.read(b, off, len);
-      myBytesRead.compareAndSet(bytesReadBefore, bytesReadBefore + bytesRead);
+      myBytesRead.addAndGet(bytesRead);
       return bytesRead;
     }
 
     public long skip(long n) throws IOException {
-      long bytesReadBefore = myBytesRead.get();
       long bytesSkipped = myInputStream.skip(n);
-      myBytesRead.compareAndSet(bytesReadBefore, bytesReadBefore + bytesSkipped);
+      myBytesRead.addAndGet(bytesSkipped);
       return bytesSkipped;
     }
 
index 3db597dcec5ddad25581e9d7a524fe2f88e42dc1..819101865ae59c8cdf4a7a45ef8d4625cb065421 100644 (file)
@@ -279,7 +279,7 @@ public final class HttpRequests {
         myInputStream = getConnection().getInputStream();
         if (myBuilder.myGzip && "gzip".equalsIgnoreCase(getConnection().getContentEncoding())) {
           //noinspection IOResourceOpenedButNotSafelyClosed
-          myInputStream = CompressedBytesReadAwareGZIPInputStream.create(myInputStream);
+          myInputStream = CountingGZIPInputStream.create(myInputStream);
         }
       }
       return myInputStream;
index 1c02684465ac6041406e2ba139552fd56aa66bcc..e8e9405c42b8ef098db2404fbe6613ab12f97b2c 100644 (file)
@@ -21,7 +21,7 @@ import com.intellij.openapi.progress.ProgressIndicator;
 import com.intellij.openapi.util.SystemInfo;
 import com.intellij.util.ObjectUtils;
 import com.intellij.util.SystemProperties;
-import com.intellij.util.io.CompressedBytesReadAwareGZIPInputStream;
+import com.intellij.util.io.CountingGZIPInputStream;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
@@ -188,7 +188,7 @@ public class NetUtils {
         indicator.setIndeterminate(true);
       }
     }
-    CompressedBytesReadAwareGZIPInputStream gzipStream = ObjectUtils.tryCast(inputStream, CompressedBytesReadAwareGZIPInputStream.class);
+    CountingGZIPInputStream gzipStream = ObjectUtils.tryCast(inputStream, CountingGZIPInputStream.class);
     final byte[] buffer = new byte[8 * 1024];
     int count;
     int bytesWritten = 0;