hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r234276 - in /jakarta/httpclient/trunk/http-common/src: java/org/apache/http/entity/ java/org/apache/http/impl/entity/ java/org/apache/http/io/ test/org/apache/http/io/
Date Sun, 21 Aug 2005 17:17:08 GMT
Author: olegk
Date: Sun Aug 21 10:16:55 2005
New Revision: 234276

URL: http://svn.apache.org/viewcvs?rev=234276&view=rev
Log:
Refactored content length delimited input stream and related classes

Modified:
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/entity/InputStreamEntity.java
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/entity/BasicHttpEntity.java
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/entity/DefaultEntityGenerator.java
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ChunkedInputStream.java
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ContentLengthInputStream.java
    jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestChunkCoding.java
    jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestContentLengthInputStream.java

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/entity/InputStreamEntity.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/entity/InputStreamEntity.java?rev=234276&r1=234275&r2=234276&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/entity/InputStreamEntity.java
(original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/entity/InputStreamEntity.java
Sun Aug 21 10:16:55 2005
@@ -34,7 +34,6 @@
 import java.io.OutputStream;
 
 import org.apache.http.HttpEntity;
-import org.apache.http.io.ContentLengthInputStream;
 
 /**
  * <p>
@@ -48,6 +47,7 @@
 public class InputStreamEntity implements HttpEntity {
 
     private final static String DEFAULT_CONTENT_TYPE = "application/octet-stream";
+    private final static int BUFFER_SIZE = 2048;
 
     private final InputStream content;
     private final long length;
@@ -105,14 +105,16 @@
             throw new IllegalArgumentException("Output stream may not be null");
         }
         InputStream instream = this.content;
-        if (this.length >= 0) {
-            instream = new ContentLengthInputStream(instream, this.length);
-        }
-        int l;
-        byte[] tmp = new byte[1024];
-        while ((l = instream.read(tmp)) != -1) {
-            outstream.write(tmp, 0, l);
-        }
+        byte[] buffer = new byte[BUFFER_SIZE];
+        long remaining = this.length;
+    	while (remaining > 0) {
+    	    int l = instream.read(buffer, 0, (int)Math.min(BUFFER_SIZE, remaining));
+    	    if (l == -1) {
+    	    	break;
+    	    }
+            outstream.write(buffer, 0, l);
+    	    remaining -= l;
+    	}
         return true;
     }
     

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/entity/BasicHttpEntity.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/entity/BasicHttpEntity.java?rev=234276&r1=234275&r2=234276&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/entity/BasicHttpEntity.java
(original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/entity/BasicHttpEntity.java
Sun Aug 21 10:16:55 2005
@@ -34,7 +34,6 @@
 import java.io.OutputStream;
 
 import org.apache.http.HttpMutableEntity;
-import org.apache.http.io.ContentLengthInputStream;
 
 /**
  * <p>
@@ -109,11 +108,8 @@
             return true;
         }
         InputStream instream = this.content;
-        if (this.length >= 0) {
-            instream = new ContentLengthInputStream(instream, this.length);
-        }
         int l;
-        byte[] tmp = new byte[1024];
+        byte[] tmp = new byte[2048];
         while ((l = instream.read(tmp)) != -1) {
             outstream.write(tmp, 0, l);
         }

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/entity/DefaultEntityGenerator.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/entity/DefaultEntityGenerator.java?rev=234276&r1=234275&r2=234276&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/entity/DefaultEntityGenerator.java
(original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/entity/DefaultEntityGenerator.java
Sun Aug 21 10:16:55 2005
@@ -198,7 +198,7 @@
         super();
     }
 
-    private InputStream getRawInputStream(final HttpDataReceiver datareceiver) {
+    private InputStream getIdentityInputStream(final HttpDataReceiver datareceiver) {
         // This is a (quite ugly) performance hack
         if (datareceiver instanceof InputStreamHttpDataReceiver) {
             // If we are dealing with the compatibility wrapper
@@ -248,7 +248,7 @@
             if (IDENTITY_ENCODING.equalsIgnoreCase(transferEncodingHeader.getValue())) {
                 entity.setChunked(false);
                 entity.setContentLength(-1);
-                entity.setContent(getRawInputStream(datareceiver));                     
      
+                entity.setContent(getIdentityInputStream(datareceiver));                
           
             } else if ((len > 0) && (CHUNKED_ENCODING.equalsIgnoreCase(encodings[len
- 1].getName()))) { 
                 entity.setChunked(true);
                 entity.setContentLength(-1);
@@ -267,7 +267,7 @@
                 }
                 entity.setChunked(false);
                 entity.setContentLength(-1);
-                entity.setContent(getRawInputStream(datareceiver));                     
      
+                entity.setContent(getIdentityInputStream(datareceiver));                
           
             }
         } else if (contentLengthHeader != null) {
             long contentlen = -1;
@@ -289,15 +289,15 @@
             }
             entity.setChunked(false);
             entity.setContentLength(contentlen);
-            InputStream instream = getRawInputStream(datareceiver);            
             if (contentlen >= 0) {
-                instream = new ContentLengthInputStream(instream, contentlen);
+            	entity.setContent(new ContentLengthInputStream(datareceiver, contentlen));
+            } else {
+            	entity.setContent(getIdentityInputStream(datareceiver));
             }
-            entity.setContent(instream);
         } else {
             entity.setChunked(false);
             entity.setContentLength(-1);
-            entity.setContent(getRawInputStream(datareceiver));                         
  
+            entity.setContent(getIdentityInputStream(datareceiver));                    
       
         }
         if (contentTypeHeader != null) {
             entity.setContentType(contentTypeHeader.getValue());    

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ChunkedInputStream.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ChunkedInputStream.java?rev=234276&r1=234275&r2=234276&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ChunkedInputStream.java
(original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ChunkedInputStream.java
Sun Aug 21 10:16:55 2005
@@ -151,10 +151,6 @@
         this.pos = 0;
     }
 
-    public ChunkedInputStream(final InputStream instream) throws IOException {
-        this(new InputStreamHttpDataReceiver(instream));
-    }
-
     /**
      * <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
@@ -170,16 +166,15 @@
      * @see HttpMethod#getResponseFooters()
      */
     public int read() throws IOException {
-
-        if (closed) {
+        if (this.closed) {
             throw new IOException("Attempted read from closed stream.");
         }
-        if (eof) {
+        if (this.eof) {
             return -1;
         } 
-        if (pos >= chunkSize) {
+        if (this.pos >= this.chunkSize) {
             nextChunk();
-            if (eof) { 
+            if (this.eof) { 
                 return -1;
             }
         }

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ContentLengthInputStream.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ContentLengthInputStream.java?rev=234276&r1=234275&r2=234276&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ContentLengthInputStream.java
(original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ContentLengthInputStream.java
Sun Aug 21 10:16:55 2005
@@ -67,6 +67,7 @@
  */
 public class ContentLengthInputStream extends InputStream {
     
+	private static int BUFFER_SIZE = 2048;
     /**
      * The maximum number of bytes that can be read from the stream. Subsequent
      * read operations will return -1.
@@ -82,7 +83,7 @@
     /**
      * Wrapped input stream that all calls are delegated to.
      */
-    private InputStream wrappedStream = null;
+    private HttpDataReceiver in = null;
 
     /**
      * Creates a new length limited stream
@@ -93,7 +94,7 @@
      * 
      * @since 3.0
      */
-    public ContentLengthInputStream(final InputStream in, long contentLength) {
+    public ContentLengthInputStream(final HttpDataReceiver in, long contentLength) {
         super();
         if (in == null) {
             throw new IllegalArgumentException("Input stream may not be null");
@@ -101,7 +102,7 @@
         if (contentLength < 0) {
             throw new IllegalArgumentException("Content length may not be negative");
         }
-        this.wrappedStream = in;
+        this.in = in;
         this.contentLength = contentLength;
     }
 
@@ -115,7 +116,9 @@
     public void close() throws IOException {
         if (!closed) {
             try {
-                ChunkedInputStream.exhaustInputStream(this);
+                byte buffer[] = new byte[BUFFER_SIZE];
+                while (this.in.read(buffer) >= 0) {
+                }
             } finally {
                 // close after above so that we don't throw an exception trying
                 // to read after closed!
@@ -140,7 +143,7 @@
             return -1;
         }
         pos++;
-        return this.wrappedStream.read();
+        return this.in.read();
     }
 
     /**
@@ -167,7 +170,7 @@
         if (pos + len > contentLength) {
             len = (int) (contentLength - pos);
         }
-        int count = this.wrappedStream.read(b, off, len);
+        int count = this.in.read(b, off, len);
         pos += count;
         return count;
     }
@@ -193,16 +196,24 @@
      * @see InputStream#skip(long)
      */
     public long skip(long n) throws IOException {
+    	if (n <= 0) {
+    	    return 0;
+    	}
+    	byte[] buffer = new byte[BUFFER_SIZE];
         // make sure we don't skip more bytes than are 
         // still available
-        long length = Math.min(n, contentLength - pos);
+        long remaining = Math.min(n, this.contentLength - this.pos); 
         // skip and keep track of the bytes actually skipped
-        length = this.wrappedStream.skip(length);
-        // only add the skipped bytes to the current position
-        // if bytes were actually skipped
-        if (length > 0) {
-            pos += length;
-        }
-        return length;
+        long count = 0;
+    	while (remaining > 0) {
+    	    int l = read(buffer, 0, (int)Math.min(BUFFER_SIZE, remaining));
+    	    if (l == -1) {
+    	    	break;
+    	    }
+    	    count += l;
+    	    remaining -= l;
+    	}
+    	this.pos += count;
+    	return count;
     }
 }

Modified: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestChunkCoding.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestChunkCoding.java?rev=234276&r1=234275&r2=234276&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestChunkCoding.java
(original)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestChunkCoding.java
Sun Aug 21 10:16:55 2005
@@ -28,13 +28,14 @@
 
 package org.apache.http.io;
 
-import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 
 import org.apache.http.Header;
+import org.apache.http.mockup.HttpDataReceiverMockup;
+import org.apache.http.mockup.HttpDataTransmitterMockup;
 import org.apache.http.util.EncodingUtil;
 
 import junit.framework.Test;
@@ -68,12 +69,6 @@
         } catch (IllegalArgumentException ex) {
             // expected
         }
-        try {
-            new ChunkedInputStream((InputStream)null);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
         new MalformedChunkCodingException();
         new MalformedChunkCodingException("");
     }
@@ -87,7 +82,7 @@
     // Test for when buffer is larger than chunk size
     public void testChunkedInputStreamLargeBuffer() throws IOException {
         ChunkedInputStream in = new ChunkedInputStream(
-                new ByteArrayInputStream(
+                new HttpDataReceiverMockup(
                         EncodingUtil.getBytes(CHUNKED_INPUT, CONTENT_CHARSET)));
         byte[] buffer = new byte[300];
         ByteArrayOutputStream out = new ByteArrayOutputStream();
@@ -115,7 +110,7 @@
     //Test for when buffer is smaller than chunk size.
     public void testChunkedInputStreamSmallBuffer() throws IOException {
         ChunkedInputStream in = new ChunkedInputStream(
-                new ByteArrayInputStream(
+                new HttpDataReceiverMockup(
                             EncodingUtil.getBytes(CHUNKED_INPUT, CONTENT_CHARSET)));
 
         byte[] buffer = new byte[7];
@@ -143,7 +138,7 @@
     public void testChunkedInputStreamOneByteRead() throws IOException {
         String s = "5\r\n01234\r\n5\r\n56789\r\n0\r\n";
         ChunkedInputStream in = new ChunkedInputStream(
-                new ByteArrayInputStream(
+                new HttpDataReceiverMockup(
                         EncodingUtil.getBytes(s, CONTENT_CHARSET)));
         int ch;
         int i = '0';
@@ -160,7 +155,7 @@
     public void testChunkedInputStreamClose() throws IOException {
         String s = "5\r\n01234\r\n5\r\n56789\r\n0\r\n";
         ChunkedInputStream in = new ChunkedInputStream(
-                new ByteArrayInputStream(
+                new HttpDataReceiverMockup(
                         EncodingUtil.getBytes(s, CONTENT_CHARSET)));
         in.close();
         in.close();
@@ -189,7 +184,7 @@
     public void testCorruptChunkedInputStreamMissingCRLF() throws IOException {
         String s = "5\r\n012345\r\n56789\r\n0\r\n";
         InputStream in = new ChunkedInputStream(
-                new ByteArrayInputStream(
+                new HttpDataReceiverMockup(
                         EncodingUtil.getBytes(s, CONTENT_CHARSET)));
         byte[] buffer = new byte[300];
         ByteArrayOutputStream out = new ByteArrayOutputStream();
@@ -208,7 +203,7 @@
     public void testCorruptChunkedInputStreamMissingLF() throws IOException {
         String s = "5\r01234\r\n5\r\n56789\r\n0\r\n";
         InputStream in = new ChunkedInputStream(
-                new ByteArrayInputStream(
+                new HttpDataReceiverMockup(
                         EncodingUtil.getBytes(s, CONTENT_CHARSET)));
         try {
             in.read();
@@ -221,7 +216,7 @@
     // Missing closing chunk
     public void testCorruptChunkedInputStreamNoClosingChunk() throws IOException {
         InputStream in = new ChunkedInputStream(
-                new ByteArrayInputStream(new byte[] {}));
+                new HttpDataReceiverMockup(new byte[] {}));
         try {
             in.read();
             fail("MalformedChunkCodingException should have been thrown");
@@ -234,7 +229,7 @@
     public void testCorruptChunkedInputStreamInvalidSize() throws IOException {
         String s = "whatever\r\n01234\r\n5\r\n56789\r\n0\r\n";
         InputStream in = new ChunkedInputStream(
-                new ByteArrayInputStream(
+                new HttpDataReceiverMockup(
                         EncodingUtil.getBytes(s, CONTENT_CHARSET)));
         try {
             in.read();
@@ -248,7 +243,7 @@
     public void testCorruptChunkedInputStreamInvalidFooter() throws IOException {
         String s = "1\r\n0\r\n0\r\nstuff\r\n";
         InputStream in = new ChunkedInputStream(
-                new ByteArrayInputStream(
+                new HttpDataReceiverMockup(
                         EncodingUtil.getBytes(s, CONTENT_CHARSET)));
         try {
             in.read();
@@ -262,7 +257,7 @@
     public void testEmptyChunkedInputStream() throws IOException {
         String input = "0\r\n";
         InputStream in = new ChunkedInputStream(
-                new ByteArrayInputStream(
+                new HttpDataReceiverMockup(
                         EncodingUtil.getBytes(input, CONTENT_CHARSET)));
         byte[] buffer = new byte[300];
         ByteArrayOutputStream out = new ByteArrayOutputStream();
@@ -283,7 +278,7 @@
         out.close();
         buffer.close();
         InputStream in = new ChunkedInputStream(
-                new ByteArrayInputStream(
+                new HttpDataReceiverMockup(
                         buffer.toByteArray()));
 
         byte[] d = new byte[10];
@@ -298,9 +293,8 @@
     }
 
     public void testChunkedOutputStream() throws IOException {
-        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
-        ChunkedOutputStream out = new ChunkedOutputStream(
-        		new OutputStreamHttpDataTransmitter(buffer), 2);
+    	HttpDataTransmitterMockup buffer = new HttpDataTransmitterMockup();
+        ChunkedOutputStream out = new ChunkedOutputStream(buffer, 2);
         out.write('1');  
         out.write('2');  
         out.write('3');  
@@ -308,7 +302,7 @@
         out.finish();
         out.close();
         
-        byte [] rawdata =  buffer.toByteArray();
+        byte [] rawdata =  buffer.getData();
         
         assertEquals(19, rawdata.length);
         assertEquals('2', rawdata[0]);
@@ -333,16 +327,15 @@
     }
 
     public void testChunkedOutputStreamLargeChunk() throws IOException {
-        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
-        ChunkedOutputStream out = new ChunkedOutputStream(
-        		new OutputStreamHttpDataTransmitter(buffer), 2);
+    	HttpDataTransmitterMockup buffer = new HttpDataTransmitterMockup();
+        ChunkedOutputStream out = new ChunkedOutputStream(buffer, 2);
         out.write(new byte[] {'1', '2', '3', '4'});
         out.finish();
         out.close();
         
-        byte [] rawdata =  buffer.toByteArray();
+        byte [] rawdata =  buffer.getData();
         
-//        assertEquals(14, rawdata.length);
+        assertEquals(14, rawdata.length);
         assertEquals('4', rawdata[0]);
         assertEquals('\r', rawdata[1]);
         assertEquals('\n', rawdata[2]);

Modified: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestContentLengthInputStream.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestContentLengthInputStream.java?rev=234276&r1=234275&r2=234276&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestContentLengthInputStream.java
(original)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestContentLengthInputStream.java
Sun Aug 21 10:16:55 2005
@@ -28,11 +28,11 @@
 
 package org.apache.http.io;
 
-import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 
+import org.apache.http.mockup.HttpDataReceiverMockup;
 import org.apache.http.util.EncodingUtil;
 
 import junit.framework.Test;
@@ -60,7 +60,7 @@
     private static final String CONTENT_CHARSET = "ISO-8859-1";
         
     public void testConstructors() throws Exception {
-        new ContentLengthInputStream(new ByteArrayInputStream(new byte[] {}), 10);
+        new ContentLengthInputStream(new HttpDataReceiverMockup(new byte[] {}), 10);
         try {
             new ContentLengthInputStream(null, 10);
             fail("IllegalArgumentException should have been thrown");
@@ -68,7 +68,7 @@
             // expected
         }
         try {
-            new ContentLengthInputStream(new ByteArrayInputStream(new byte[] {}), -10);
+            new ContentLengthInputStream(new HttpDataReceiverMockup(new byte[] {}), -10);
             fail("IllegalArgumentException should have been thrown");
         } catch (IllegalArgumentException ex) {
             // expected
@@ -77,7 +77,7 @@
 
     public void testBasics() throws IOException {
         String correct = "1234567890123456";
-        InputStream in = new ContentLengthInputStream(new ByteArrayInputStream(
+        InputStream in = new ContentLengthInputStream(new HttpDataReceiverMockup(
             EncodingUtil.getBytes(correct, CONTENT_CHARSET)), 10L);
         ByteArrayOutputStream out = new ByteArrayOutputStream();
 
@@ -92,16 +92,16 @@
     }
 
     public void testSkip() throws IOException {
-        InputStream in = new ContentLengthInputStream(new ByteArrayInputStream(new byte[20]),
10L);
+        InputStream in = new ContentLengthInputStream(new HttpDataReceiverMockup(new byte[20]),
10L);
         assertEquals(10, in.skip(10));
         assertTrue(in.read() == -1);
 
-        in = new ContentLengthInputStream(new ByteArrayInputStream(new byte[20]), 10L);
+        in = new ContentLengthInputStream(new HttpDataReceiverMockup(new byte[20]), 10L);
         in.read();
         assertEquals(9, in.skip(10));
         assertTrue(in.read() == -1);
 
-        in = new ContentLengthInputStream(new ByteArrayInputStream(new byte[20]), 2L);
+        in = new ContentLengthInputStream(new HttpDataReceiverMockup(new byte[20]), 2L);
         in.read();
         in.read();
         assertTrue(in.skip(10) <= 0);
@@ -110,7 +110,7 @@
 
     public void testClose() throws IOException {
         String correct = "1234567890123456";
-        InputStream in = new ContentLengthInputStream(new ByteArrayInputStream(
+        InputStream in = new ContentLengthInputStream(new HttpDataReceiverMockup(
             EncodingUtil.getBytes(correct, CONTENT_CHARSET)), 10L);
         in.close();
         in.close();



Mime
View raw message