You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by "garydgregory (via GitHub)" <gi...@apache.org> on 2024/08/16 18:04:14 UTC

[PR] Input streams returned by DecompressingEntity.getContent() should support mark(int) and reset() [httpcomponents-client]

garydgregory opened a new pull request, #567:
URL: https://github.com/apache/httpcomponents-client/pull/567

   support mark(int) and reset() if the underlying stream does
   
   - Pass calls to the underlying InputStream for mark(int)
   - Pass calls to the underlying InputStream for reset()
   - LazyDecompressingInputStream extends FilterInputStream
   - Reimplement internals fluently
   - Reuse Closer.close(Closeable)
   - Tested locally with git master 5.3-beta2-SNAPSHOT


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org


Re: [PR] Input streams returned by DecompressingEntity.getContent() should support mark(int) and reset() [httpcomponents-client]

Posted by "garydgregory (via GitHub)" <gi...@apache.org>.
garydgregory commented on code in PR #567:
URL: https://github.com/apache/httpcomponents-client/pull/567#discussion_r1720782879


##########
httpclient5/src/main/java/org/apache/hc/client5/http/entity/LazyDecompressingInputStream.java:
##########
@@ -83,19 +83,29 @@ public boolean markSupported() {
 
     @Override
     public int available() throws IOException {
-        initWrapper();
-        return wrapperStream.available();
+        return initWrapper().available();
     }
 
     @Override
     public void close() throws IOException {
         try {
-            if (wrapperStream != null) {
-                wrapperStream.close();
-            }
+            Closer.close(wrapperStream);
         } finally {
-            wrappedStream.close();
+            super.close();
+        }
+    }
+
+    @Override
+    public synchronized void mark(final int readlimit) {
+        try {
+            initWrapper().mark(readlimit);
+        } catch (final IOException e) {
+            throw new UncheckedIOException(e);
         }
     }
 
+    @Override
+    public synchronized void reset() throws IOException {

Review Comment:
   OK, I merged with this option.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org


Re: [PR] Input streams returned by DecompressingEntity.getContent() should support mark(int) and reset() [httpcomponents-client]

Posted by "garydgregory (via GitHub)" <gi...@apache.org>.
garydgregory commented on code in PR #567:
URL: https://github.com/apache/httpcomponents-client/pull/567#discussion_r1720766623


##########
httpclient5/src/main/java/org/apache/hc/client5/http/entity/LazyDecompressingInputStream.java:
##########
@@ -83,19 +83,29 @@ public boolean markSupported() {
 
     @Override
     public int available() throws IOException {
-        initWrapper();
-        return wrapperStream.available();
+        return initWrapper().available();
     }
 
     @Override
     public void close() throws IOException {
         try {
-            if (wrapperStream != null) {
-                wrapperStream.close();
-            }
+            Closer.close(wrapperStream);
         } finally {
-            wrappedStream.close();
+            super.close();
+        }
+    }
+
+    @Override
+    public synchronized void mark(final int readlimit) {

Review Comment:
   Hm, I guess the whole class isn't thread-safe to start with due to `initWrapper()`. I'll rework the PR.
   
   These `synchronized` come from the Java 8, 11, and 17 contracts.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org


Re: [PR] Input streams returned by DecompressingEntity.getContent() should support mark(int) and reset() [httpcomponents-client]

Posted by "garydgregory (via GitHub)" <gi...@apache.org>.
garydgregory merged PR #567:
URL: https://github.com/apache/httpcomponents-client/pull/567


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org


Re: [PR] Input streams returned by DecompressingEntity.getContent() should support mark(int) and reset() [httpcomponents-client]

Posted by "ok2c (via GitHub)" <gi...@apache.org>.
ok2c commented on code in PR #567:
URL: https://github.com/apache/httpcomponents-client/pull/567#discussion_r1720742876


##########
httpclient5/src/main/java/org/apache/hc/client5/http/entity/LazyDecompressingInputStream.java:
##########
@@ -83,19 +83,29 @@ public boolean markSupported() {
 
     @Override
     public int available() throws IOException {
-        initWrapper();
-        return wrapperStream.available();
+        return initWrapper().available();
     }
 
     @Override
     public void close() throws IOException {
         try {
-            if (wrapperStream != null) {
-                wrapperStream.close();
-            }
+            Closer.close(wrapperStream);
         } finally {
-            wrappedStream.close();
+            super.close();
+        }
+    }
+
+    @Override
+    public synchronized void mark(final int readlimit) {

Review Comment:
   @garydgregory No more `synchronized` for us going forward. Thread synchronization must be implemented using locks in order to be compatible with Java 21 fibers



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org


Re: [PR] Input streams returned by DecompressingEntity.getContent() should support mark(int) and reset() [httpcomponents-client]

Posted by "garydgregory (via GitHub)" <gi...@apache.org>.
garydgregory commented on code in PR #567:
URL: https://github.com/apache/httpcomponents-client/pull/567#discussion_r1720769799


##########
httpclient5/src/main/java/org/apache/hc/client5/http/entity/LazyDecompressingInputStream.java:
##########
@@ -83,19 +83,29 @@ public boolean markSupported() {
 
     @Override
     public int available() throws IOException {
-        initWrapper();
-        return wrapperStream.available();
+        return initWrapper().available();
     }
 
     @Override
     public void close() throws IOException {
         try {
-            if (wrapperStream != null) {
-                wrapperStream.close();
-            }
+            Closer.close(wrapperStream);
         } finally {
-            wrappedStream.close();
+            super.close();
+        }
+    }
+
+    @Override
+    public synchronized void mark(final int readlimit) {

Review Comment:
   Thank you both for the reviews.
   
   The simplest change and implementation that keeps in line with what to have today is to remove the `synchronized` keyword and let the delegate's implementation do what it will. If we wanted mark/reset to be thread-safe, we could make the whole class thread-safe like this:
   
   ```java
      private final ReentrantLock lock = new ReentrantLock();
      ...
       /**
        * Thread-safe initialization of the wrapped stream.
        *
        * @return the wrapper stream.
        * @throws IOException if an IO error occurs.
        */
       private InputStream initWrapper() throws IOException {
           if (wrapperStream == null) {
               lock.lock();
               try {
                   wrapperStream = wrapperStream != null ? wrapperStream : inputStreamFactory.create(in);
               } finally {
                   lock.unlock();
               }
           }
           return wrapperStream;
       }
   ```
   but then each call to the stream would lock-unlock.
   
   So, either:
   - Don't use `synchronized` and that's it.
   - Only have mark/reset use lock-unlock, or
   - Implement using the above thread-safe `initWrapper()`?
   
   For now, the PR takes the simplest route.
   WDYT?
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org


Re: [PR] Input streams returned by DecompressingEntity.getContent() should support mark(int) and reset() [httpcomponents-client]

Posted by "arturobernalg (via GitHub)" <gi...@apache.org>.
arturobernalg commented on code in PR #567:
URL: https://github.com/apache/httpcomponents-client/pull/567#discussion_r1720664106


##########
httpclient5/src/main/java/org/apache/hc/client5/http/entity/LazyDecompressingInputStream.java:
##########
@@ -83,19 +83,29 @@ public boolean markSupported() {
 
     @Override
     public int available() throws IOException {
-        initWrapper();
-        return wrapperStream.available();
+        return initWrapper().available();
     }
 
     @Override
     public void close() throws IOException {
         try {
-            if (wrapperStream != null) {
-                wrapperStream.close();
-            }
+            Closer.close(wrapperStream);
         } finally {
-            wrappedStream.close();
+            super.close();
+        }
+    }
+
+    @Override
+    public synchronized void mark(final int readlimit) {

Review Comment:
   Shouldn't we use ReentrantLock instead?



##########
httpclient5/src/main/java/org/apache/hc/client5/http/entity/LazyDecompressingInputStream.java:
##########
@@ -83,19 +83,29 @@ public boolean markSupported() {
 
     @Override
     public int available() throws IOException {
-        initWrapper();
-        return wrapperStream.available();
+        return initWrapper().available();
     }
 
     @Override
     public void close() throws IOException {
         try {
-            if (wrapperStream != null) {
-                wrapperStream.close();
-            }
+            Closer.close(wrapperStream);
         } finally {
-            wrappedStream.close();
+            super.close();
+        }
+    }
+
+    @Override
+    public synchronized void mark(final int readlimit) {
+        try {
+            initWrapper().mark(readlimit);
+        } catch (final IOException e) {
+            throw new UncheckedIOException(e);
         }
     }
 
+    @Override
+    public synchronized void reset() throws IOException {

Review Comment:
   same here



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org


Re: [PR] Input streams returned by DecompressingEntity.getContent() should support mark(int) and reset() [httpcomponents-client]

Posted by "ok2c (via GitHub)" <gi...@apache.org>.
ok2c commented on code in PR #567:
URL: https://github.com/apache/httpcomponents-client/pull/567#discussion_r1720782617


##########
httpclient5/src/main/java/org/apache/hc/client5/http/entity/LazyDecompressingInputStream.java:
##########
@@ -83,19 +83,29 @@ public boolean markSupported() {
 
     @Override
     public int available() throws IOException {
-        initWrapper();
-        return wrapperStream.available();
+        return initWrapper().available();
     }
 
     @Override
     public void close() throws IOException {
         try {
-            if (wrapperStream != null) {
-                wrapperStream.close();
-            }
+            Closer.close(wrapperStream);
         } finally {
-            wrappedStream.close();
+            super.close();
+        }
+    }
+
+    @Override
+    public synchronized void mark(final int readlimit) {

Review Comment:
   > * Don't use `synchronized` and that's it.
   
   I would go for this one.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org