hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1787541 - in /httpcomponents/httpcore/trunk/httpcore5/src: main/java/org/apache/hc/core5/http/impl/io/IdentityInputStream.java test/java/org/apache/hc/core5/http/impl/io/TestIdentityInputStream.java
Date Sat, 18 Mar 2017 09:46:36 GMT
Author: olegk
Date: Sat Mar 18 09:46:36 2017
New Revision: 1787541

URL: http://svn.apache.org/viewvc?rev=1787541&view=rev
Log:
HTTPCORE-449: IdentityInputStream#available to delegate to InputStream#available of the underlying
input stream in case the internal buffer is empty

Modified:
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/IdentityInputStream.java
    httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/impl/io/TestIdentityInputStream.java

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/IdentityInputStream.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/IdentityInputStream.java?rev=1787541&r1=1787540&r2=1787541&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/IdentityInputStream.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/IdentityInputStream.java
Sat Mar 18 09:46:36 2017
@@ -67,7 +67,11 @@ public class IdentityInputStream extends
 
     @Override
     public int available() throws IOException {
-        return this.buffer.length();
+        if (this.closed) {
+            return 0;
+        }
+        final int n = this.buffer.length();
+        return n > 0 ? n : this.inputStream.available();
     }
 
     @Override

Modified: httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/impl/io/TestIdentityInputStream.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/impl/io/TestIdentityInputStream.java?rev=1787541&r1=1787540&r2=1787541&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/impl/io/TestIdentityInputStream.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/impl/io/TestIdentityInputStream.java
Sat Mar 18 09:46:36 2017
@@ -30,6 +30,7 @@ package org.apache.hc.core5.http.impl.io
 import java.io.ByteArrayInputStream;
 
 import org.apache.hc.core5.http.StreamClosedException;
+import org.apache.hc.core5.http.impl.BasicHttpTransportMetrics;
 import org.apache.hc.core5.http.io.SessionInputBuffer;
 import org.junit.Assert;
 import org.junit.Test;
@@ -85,11 +86,23 @@ public class TestIdentityInputStream {
     public void testAvailable() throws Exception {
         final byte[] input = new byte[] {'a', 'b', 'c'};
         final ByteArrayInputStream inputStream = new ByteArrayInputStream(input);
-        final SessionInputBuffer inbuffer = new SessionInputBufferImpl(16);
+        final SessionInputBuffer inbuffer = new SessionInputBufferImpl(new BasicHttpTransportMetrics(),
16, 16, 1024, null);
         final IdentityInputStream in = new IdentityInputStream(inbuffer, inputStream);
         in.read();
         Assert.assertEquals(2, in.available());
         in.close();
     }
 
+    @Test
+    public void testAvailableInStream() throws Exception {
+        final byte[] input = new byte[] {'a', 'b', 'c', 'd', 'e', 'f'};
+        final ByteArrayInputStream inputStream = new ByteArrayInputStream(input);
+        final SessionInputBuffer inbuffer = new SessionInputBufferImpl(new BasicHttpTransportMetrics(),
16, 0, 1024, null);
+        final IdentityInputStream in = new IdentityInputStream(inbuffer, inputStream);
+        final byte[] tmp = new byte[3];
+        Assert.assertEquals(3, in.read(tmp));
+        Assert.assertEquals(3, in.available());
+        in.close();
+    }
+
 }



Mime
View raw message