hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r936402 - in /httpcomponents/httpcore/trunk/httpcore/src: main/java/org/apache/http/impl/io/ test/java/org/apache/http/impl/io/
Date Wed, 21 Apr 2010 17:54:50 GMT
Author: olegk
Date: Wed Apr 21 17:54:49 2010
New Revision: 936402

URL: http://svn.apache.org/viewvc?rev=936402&view=rev
Log:
InputStream#available implementation for ChunkedInputStream & ContentLengthInputStream

Modified:
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/ChunkedInputStream.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/ContentLengthInputStream.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestChunkCoding.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestContentLengthInputStream.java

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/ChunkedInputStream.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/ChunkedInputStream.java?rev=936402&r1=936401&r2=936402&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/ChunkedInputStream.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/ChunkedInputStream.java
Wed Apr 21 17:54:49 2010
@@ -34,6 +34,7 @@ import org.apache.http.Header;
 import org.apache.http.HttpException;
 import org.apache.http.MalformedChunkCodingException;
 import org.apache.http.TruncatedChunkException;
+import org.apache.http.io.BufferInfo;
 import org.apache.http.io.SessionInputBuffer;
 import org.apache.http.util.CharArrayBuffer;
 import org.apache.http.util.ExceptionUtils;
@@ -99,6 +100,15 @@ public class ChunkedInputStream extends 
         this.state = CHUNK_LEN;
     }
 
+    public int available() throws IOException {
+        if (this.in instanceof BufferInfo) {
+            int len = ((BufferInfo) this.in).length();
+            return Math.min(len, this.chunkSize - this.pos); 
+        } else {
+            return 0;
+        }
+    }
+    
     /**
      * <p> Returns all the data in a chunked stream in coalesced form. A chunk
      * is followed by a CRLF. The method returns -1 as soon as a chunksize of 0

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/ContentLengthInputStream.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/ContentLengthInputStream.java?rev=936402&r1=936401&r2=936402&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/ContentLengthInputStream.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/ContentLengthInputStream.java
Wed Apr 21 17:54:49 2010
@@ -30,6 +30,7 @@ package org.apache.http.impl.io;
 import java.io.IOException;
 import java.io.InputStream;
 
+import org.apache.http.io.BufferInfo;
 import org.apache.http.io.SessionInputBuffer;
 
 /**
@@ -109,7 +110,15 @@ public class ContentLengthInputStream ex
         }
     }
 
-
+    public int available() throws IOException {
+        if (this.in instanceof BufferInfo) {
+            int len = ((BufferInfo) this.in).length();
+            return Math.min(len, (int) (this.contentLength - this.pos)); 
+        } else {
+            return 0;
+        }
+    }
+    
     /**
      * Read the next byte from the stream
      * @return The next byte or -1 if the end of stream has been reached.

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestChunkCoding.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestChunkCoding.java?rev=936402&r1=936401&r2=936402&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestChunkCoding.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestChunkCoding.java
Wed Apr 21 17:54:49 2010
@@ -140,6 +140,16 @@ public class TestChunkCoding extends Tes
 
         in.close();        
     }
+    
+    public void testAvailable() throws IOException {
+        String s = "5\r\n12345\r\n0\r\n";
+        ChunkedInputStream in = new ChunkedInputStream(
+                new SessionInputBufferMockup(
+                        EncodingUtils.getBytes(s, CONTENT_CHARSET)));
+        assertEquals(0, in.available());
+        in.read();
+        assertEquals(4, in.available());
+    }
 
     public void testChunkedInputStreamClose() throws IOException {
         String s = "5\r\n01234\r\n5\r\n56789\r\n0\r\n";

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestContentLengthInputStream.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestContentLengthInputStream.java?rev=936402&r1=936401&r2=936402&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestContentLengthInputStream.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestContentLengthInputStream.java
Wed Apr 21 17:54:49 2010
@@ -102,6 +102,14 @@ public class TestContentLengthInputStrea
         assertEquals(5, in.read(new byte[20]));
     }
 
+    public void testAvailable() throws IOException {
+        InputStream in = new ContentLengthInputStream(
+                new SessionInputBufferMockup(new byte[] {1, 2, 3}), 10L);
+        assertEquals(0, in.available());
+        in.read();
+        assertEquals(2, in.available());
+    }
+
     public void testClose() throws IOException {
         String correct = "1234567890123456";
         InputStream in = new ContentLengthInputStream(new SessionInputBufferMockup(



Mime
View raw message