hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r267282 - in /jakarta/httpclient/trunk/http-common/src: java/org/apache/http/impl/entity/ java/org/apache/http/impl/io/ java/org/apache/http/io/ java/org/apache/http/util/ test/org/apache/http/impl/ test/org/apache/http/io/ test/org/apache/...
Date Fri, 02 Sep 2005 19:37:45 GMT
Author: olegk
Date: Fri Sep  2 12:37:23 2005
New Revision: 267282

URL: http://svn.apache.org/viewcvs?rev=267282&view=rev
Log:
Massive refactoring of classic IO HTTP data receiver and transmitter classes resulting in 4 fold performance improvement on byte and line read operations

Added:
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/ByteArrayBuffer.java   (with props)
    jakarta/httpclient/trunk/http-common/src/test/org/apache/http/mockup/InputStreamHttpDataReceiverMockup.java   (with props)
    jakarta/httpclient/trunk/http-common/src/test/org/apache/http/mockup/OutputStreamHttpDataTransmitterMockup.java   (with props)
Removed:
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/HttpLineParser.java
    jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestHttpLineParser.java
Modified:
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/entity/DefaultEntityGenerator.java
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/NIOHttpDataReceiver.java
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/NIOHttpDataTransmitter.java
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/NIOSocketHttpDataReceiver.java
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/NIOSocketHttpDataTransmitter.java
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/OldIOSocketHttpDataReceiver.java
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/OldIOSocketHttpDataTransmitter.java
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/InputStreamHttpDataReceiver.java
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/OutputStreamHttpDataTransmitter.java
    jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestDefaultEntityGenerator.java
    jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestNIOHttpTransmitterAndReceiver.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/TestOldIOHttpDataReceiverAndTransmitter.java
    jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestAllUtil.java

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=267282&r1=267281&r2=267282&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 Fri Sep  2 12:37:23 2005
@@ -30,7 +30,6 @@
 package org.apache.http.impl.entity;
 
 import java.io.IOException;
-import java.io.InputStream;
 
 import org.apache.http.Header;
 import org.apache.http.HeaderElement;
@@ -42,7 +41,6 @@
 import org.apache.http.io.ContentLengthInputStream;
 import org.apache.http.io.HttpDataInputStream;
 import org.apache.http.io.HttpDataReceiver;
-import org.apache.http.io.InputStreamHttpDataReceiver;
 import org.apache.http.params.HttpParams;
 import org.apache.http.params.HttpProtocolParams;
 
@@ -197,17 +195,6 @@
         super();
     }
 
-    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
-            // Get the original input stream
-            return  ((InputStreamHttpDataReceiver)datareceiver).getInputStream();
-        } else {
-            return new HttpDataInputStream(datareceiver);
-        }
-    }
-    
     public HttpMutableEntity generate(
             final HttpDataReceiver datareceiver,
             final HttpMessage message) throws HttpException, IOException {
@@ -247,7 +234,7 @@
             if (IDENTITY_ENCODING.equalsIgnoreCase(transferEncodingHeader.getValue())) {
                 entity.setChunked(false);
                 entity.setContentLength(-1);
-                entity.setContent(getIdentityInputStream(datareceiver));                            
+                entity.setContent(new HttpDataInputStream(datareceiver));                            
             } else if ((len > 0) && (CHUNKED_ENCODING.equalsIgnoreCase(encodings[len - 1].getName()))) { 
                 entity.setChunked(true);
                 entity.setContentLength(-1);
@@ -258,7 +245,7 @@
                 }
                 entity.setChunked(false);
                 entity.setContentLength(-1);
-                entity.setContent(getIdentityInputStream(datareceiver));                            
+                entity.setContent(new HttpDataInputStream(datareceiver));                            
             }
         } else if (contentLengthHeader != null) {
             long contentlen = -1;
@@ -283,12 +270,12 @@
             if (contentlen >= 0) {
             	entity.setContent(new ContentLengthInputStream(datareceiver, contentlen));
             } else {
-            	entity.setContent(getIdentityInputStream(datareceiver));
+            	entity.setContent(new HttpDataInputStream(datareceiver));
             }
         } else {
             entity.setChunked(false);
             entity.setContentLength(-1);
-            entity.setContent(getIdentityInputStream(datareceiver));                            
+            entity.setContent(new HttpDataInputStream(datareceiver));                            
         }
         if (contentTypeHeader != null) {
             entity.setContentType(contentTypeHeader.getValue());    

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/NIOHttpDataReceiver.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/NIOHttpDataReceiver.java?rev=267282&r1=267281&r2=267282&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/NIOHttpDataReceiver.java (original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/NIOHttpDataReceiver.java Fri Sep  2 12:37:23 2005
@@ -62,9 +62,6 @@
     private CharBuffer chbuffer = null;
     
     protected void initBuffer(int buffersize) {
-        if (buffersize < 2048) {
-            buffersize = 2048;
-        }
         this.buffer = ByteBuffer.allocateDirect(buffersize);
         this.buffer.flip();
         this.charset = Charset.forName("US-ASCII");

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/NIOHttpDataTransmitter.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/NIOHttpDataTransmitter.java?rev=267282&r1=267281&r2=267282&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/NIOHttpDataTransmitter.java (original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/NIOHttpDataTransmitter.java Fri Sep  2 12:37:23 2005
@@ -63,9 +63,6 @@
     private CharBuffer chbuffer = null;
 
     protected void initBuffer(int buffersize) {
-        if (buffersize < 2048) {
-            buffersize = 2048;
-        }
         this.buffer = ByteBuffer.allocateDirect(buffersize);
         this.charset = Charset.forName("US-ASCII");
         this.charencoder = createCharEncoder();

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/NIOSocketHttpDataReceiver.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/NIOSocketHttpDataReceiver.java?rev=267282&r1=267281&r2=267282&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/NIOSocketHttpDataReceiver.java (original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/NIOSocketHttpDataReceiver.java Fri Sep  2 12:37:23 2005
@@ -59,7 +59,11 @@
             throw new IllegalArgumentException("Socket does not implement NIO channel");
         }
         this.channel = socket.getChannel();
-        initBuffer(socket.getReceiveBufferSize());
+        int buffersize = socket.getReceiveBufferSize();
+        if (buffersize < 2048) {
+            buffersize = 2048;
+        }
+        initBuffer(buffersize);
     }
     
     public void reset(final HttpParams params) {

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/NIOSocketHttpDataTransmitter.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/NIOSocketHttpDataTransmitter.java?rev=267282&r1=267281&r2=267282&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/NIOSocketHttpDataTransmitter.java (original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/NIOSocketHttpDataTransmitter.java Fri Sep  2 12:37:23 2005
@@ -57,7 +57,11 @@
             throw new IllegalArgumentException("Socket does not implement NIO channel");
         }
         this.channel = socket.getChannel();
-        initBuffer(socket.getSendBufferSize());
+        int buffersize = socket.getSendBufferSize();
+        if (buffersize < 2048) {
+            buffersize = 2048;
+        }
+        initBuffer(buffersize);
     }
 
     protected void writeToChannel(final ByteBuffer src) throws IOException {

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/OldIOSocketHttpDataReceiver.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/OldIOSocketHttpDataReceiver.java?rev=267282&r1=267281&r2=267282&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/OldIOSocketHttpDataReceiver.java (original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/OldIOSocketHttpDataReceiver.java Fri Sep  2 12:37:23 2005
@@ -29,9 +29,7 @@
 
 package org.apache.http.impl.io;
 
-import java.io.BufferedInputStream;
 import java.io.IOException;
-import java.io.InputStream;
 import java.io.InterruptedIOException;
 import java.net.Socket;
 
@@ -74,35 +72,24 @@
     
     private final Socket socket;
     
-    private static InputStream createInputStream(final Socket socket) throws IOException {
-        if (socket == null) {
-            throw new IllegalArgumentException("Socket may not be null");
-        }
+    public OldIOSocketHttpDataReceiver(final Socket socket) throws IOException {
+        super(socket.getInputStream());
+        this.socket = socket;
         int buffersize = socket.getReceiveBufferSize();
         if (buffersize < 2048) {
             buffersize = 2048;
         }
-        return new BufferedInputStream(socket.getInputStream(), buffersize);
-    }
-    
-    public OldIOSocketHttpDataReceiver(final Socket socket) throws IOException {
-        super(createInputStream(socket));
-        this.socket = socket;
+        initBuffer(buffersize);
     }
     
     public boolean isDataAvailable(int timeout) throws IOException {
-        InputStream instream = getInputStream();
-        boolean result = instream.available() > 0;
+        boolean result = hasBufferedData();
         if (!result) {
             int oldtimeout = this.socket.getSoTimeout();
             try {
                 this.socket.setSoTimeout(timeout);
-                instream.mark(1);
-                int byteRead = instream.read();
-                if (byteRead != -1) {
-                    instream.reset();
-                    result = true;
-                }
+                fillBuffer();
+                result = hasBufferedData();
             } catch (InterruptedIOException e) {
                 if (!isSocketTimeoutException(e)) {
                     throw e;

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/OldIOSocketHttpDataTransmitter.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/OldIOSocketHttpDataTransmitter.java?rev=267282&r1=267281&r2=267282&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/OldIOSocketHttpDataTransmitter.java (original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/OldIOSocketHttpDataTransmitter.java Fri Sep  2 12:37:23 2005
@@ -29,9 +29,7 @@
 
 package org.apache.http.impl.io;
 
-import java.io.BufferedOutputStream;
 import java.io.IOException;
-import java.io.OutputStream;
 import java.net.Socket;
 
 import org.apache.http.io.OutputStreamHttpDataTransmitter;
@@ -47,19 +45,13 @@
  */
 public class OldIOSocketHttpDataTransmitter extends OutputStreamHttpDataTransmitter {
 
-    private static OutputStream createOutputStream(final Socket socket) throws IOException {
-        if (socket == null) {
-            throw new IllegalArgumentException("Socket may not be null");
-        }
+    public OldIOSocketHttpDataTransmitter(final Socket socket) throws IOException {
+        super(socket.getOutputStream());
         int buffersize = socket.getSendBufferSize();
         if (buffersize < 2048) {
             buffersize = 2048;
         }
-        return new BufferedOutputStream(socket.getOutputStream(), buffersize);
-    }
-    
-    public OldIOSocketHttpDataTransmitter(final Socket socket) throws IOException {
-        super(createOutputStream(socket));
+        initBuffer(buffersize);
     }
     
 }

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/InputStreamHttpDataReceiver.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/InputStreamHttpDataReceiver.java?rev=267282&r1=267281&r2=267282&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/InputStreamHttpDataReceiver.java (original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/InputStreamHttpDataReceiver.java Fri Sep  2 12:37:23 2005
@@ -34,18 +34,28 @@
 
 import org.apache.http.params.HttpParams;
 import org.apache.http.params.HttpProtocolParams;
-import org.apache.http.util.HttpLineParser;
+import org.apache.http.util.ByteArrayBuffer;
+import org.apache.http.util.EncodingUtil;
 
 /**
- * <p>Old IO Compatibility wrapper</p>
+ * <p>Classic IO Compatibility wrapper</p>
  *
  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
  *
  */
 public class InputStreamHttpDataReceiver implements HttpDataReceiver {
 
+    private static final int CR = 13;
+    private static final int LF = 10;
+    
     private final InputStream instream;
     
+    private byte[] buffer;
+    private int bufferpos;
+    private int bufferlen;
+    
+    private ByteArrayBuffer linebuffer = null;
+    
     private String charset = "US-ASCII";
     
     public InputStreamHttpDataReceiver(final InputStream instream) {
@@ -55,33 +65,154 @@
         }
         this.instream = instream;
     }
-    
-    public InputStream getInputStream() {
-        return this.instream;
+
+    protected void initBuffer(int buffersize) {
+        this.buffer = new byte[buffersize];
+        this.bufferpos = 0;
+        this.bufferlen = 0;
+        this.linebuffer = new ByteArrayBuffer(buffersize);
+    }
+    
+    protected int fillBuffer() throws IOException {
+    	// compact the buffer if necessary
+    	if (this.bufferpos > 0) {
+    		int len = this.bufferlen - this.bufferpos;
+            if (len > 0) {
+                System.arraycopy(this.buffer, this.bufferpos, this.buffer, 0, len);
+            }
+        	this.bufferpos = 0;
+        	this.bufferlen = len;
+    	}
+    	int l;
+    	int off = this.bufferlen;
+    	int len = this.buffer.length - this.bufferlen;
+    	while ((l = this.instream.read(this.buffer, off, len)) > 0) {
+    		off += l;
+            len -= l;
+    	}
+    	this.bufferlen = off;
+        if (l == -1 && off == 0) {
+            return -1;
+        } else {
+            return off;
+        }
+    }
+
+    protected boolean hasBufferedData() {
+        return this.bufferpos < this.bufferlen;
     }
     
     public boolean isDataAvailable(int timeout) throws IOException {
-        return this.instream.available() > 0;
+        return hasBufferedData();
     }
     
     public int read() throws IOException {
-        return this.instream.read();
+        int noRead = 0;
+    	while (!hasBufferedData()) {
+            noRead = fillBuffer();
+    		if (noRead == -1) {
+    			return -1;
+    		}
+    	}
+        int b = this.buffer[this.bufferpos++];
+        if (b < 0) {
+            b = 256 + b;
+        }
+        return b;
     }
     
     public int read(final byte[] b, int off, int len) throws IOException {
-        return this.instream.read(b, off, len);
+        if (b == null) {
+            return 0;
+        }
+        int noRead = 0;
+        while (!hasBufferedData()) {
+            noRead = fillBuffer();
+            if (noRead == -1) {
+                return -1;
+            }
+        }
+    	int chunk = this.bufferlen - this.bufferpos;
+    	if (chunk > len) {
+    		chunk = len;
+    	}
+    	System.arraycopy(this.buffer, this.bufferpos, b, off, chunk);
+    	this.bufferpos += chunk;
+    	return chunk;
     }
     
     public int read(final byte[] b) throws IOException {
-        return this.instream.read(b);
+        if (b == null) {
+            return 0;
+        }
+        return read(b, 0, b.length);
+    }
+    
+    private int locateLF() {
+        for (int i = this.bufferpos; i < this.bufferlen; i++) {
+            if (this.buffer[i] == LF) {
+                return i;
+            }
+        }
+        return -1;
     }
     
     public String readLine() throws IOException {
-        return HttpLineParser.readLine(this.instream, this.charset);
+    	this.linebuffer.clear();
+    	int noRead = 0;
+        boolean retry = true;
+        while (retry) {
+            // attempt to find end of line (LF)
+            int i = locateLF();
+            if (i != -1) {
+                // end of line found. 
+                retry = false;
+                int len = i + 1 - this.bufferpos;
+                this.linebuffer.append(this.buffer, this.bufferpos, len);
+                this.bufferpos = i + 1;
+            } else {
+                // end of line not found
+                if (hasBufferedData()) {
+                    int len = this.bufferlen - this.bufferpos;
+                    this.linebuffer.append(this.buffer, this.bufferpos, len);
+                    this.bufferpos = this.bufferlen;
+                }
+                noRead = fillBuffer();
+                if (noRead == -1) {
+                    retry = false;
+                    if (hasBufferedData()) {
+                        int len = this.bufferlen - this.bufferpos;
+                        this.linebuffer.append(this.buffer, this.bufferpos, len);
+                        this.bufferpos = this.bufferlen;
+                    }
+                }
+            }
+        }
+        if (noRead == -1 && this.linebuffer.length() == 0) {
+            // indicate the end of stream
+            return null;
+        }
+        // discard LF if found
+        int l = this.linebuffer.length(); 
+        if (l > 0) {
+            if (this.linebuffer.byteAt(l - 1) == LF) {
+                l--;
+                this.linebuffer.setLength(l);
+            }
+            // discard CR if found
+            if (l > 0) {
+                if (this.linebuffer.byteAt(l - 1) == CR) {
+                    l--;
+                    this.linebuffer.setLength(l);
+                }
+            }
+        }
+        return EncodingUtil.getString(
+        		this.linebuffer.getBytes(), 0, this.linebuffer.length(), this.charset);
     }
     
     public void reset(final HttpParams params) {
-        this.charset = HttpProtocolParams.getHttpElementCharset(params); 
+        this.charset = HttpProtocolParams.getHttpElementCharset(params);
     }
     
 }

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/OutputStreamHttpDataTransmitter.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/OutputStreamHttpDataTransmitter.java?rev=267282&r1=267281&r2=267282&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/OutputStreamHttpDataTransmitter.java (original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/OutputStreamHttpDataTransmitter.java Fri Sep  2 12:37:23 2005
@@ -49,6 +49,9 @@
 
     private final OutputStream outstream;
     
+    private byte[] buffer;
+    private int bufferlen;
+        
     private String charset = "US-ASCII";
     
     public OutputStreamHttpDataTransmitter(final OutputStream outstream) {
@@ -59,32 +62,63 @@
         this.outstream = outstream;
     }
 
-    public OutputStream getOutputStream() {
-        return this.outstream;
+    protected void initBuffer(int buffersize) {
+        this.buffer = new byte[buffersize];
+        this.bufferlen = 0;
+    }
+    
+    protected void flushBuffer() throws IOException {
+        if (this.bufferlen > 0) {
+            this.outstream.write(this.buffer, 0, this.bufferlen);
+            this.bufferlen = 0;
+        }
     }
     
     public void flush() throws IOException {
+        flushBuffer();
         this.outstream.flush();
     }
     
     public void write(final byte[] b, int off, int len) throws IOException {
-        this.outstream.write(b, off, len);
+        if (b == null) {
+            return;
+        }
+        int freecapacity = this.buffer.length - this.bufferlen;
+        if (len > freecapacity) {
+            // flush the buffer
+            flushBuffer();
+            freecapacity = this.buffer.length; 
+        }
+        if (len > freecapacity) {
+            // still does not fit, write directly to the out stream
+            this.outstream.write(b, off, len);
+        } else {
+            // buffer
+            System.arraycopy(b, off, this.buffer, this.bufferlen, len);
+            this.bufferlen += len;
+        }
     }
     
     public void write(final byte[] b) throws IOException {
-        this.outstream.write(b);
+        if (b == null) {
+            return;
+        }
+        write(b, 0, b.length);
     }
     
     public void write(int b) throws IOException {
-        this.outstream.write(b);
+        if (this.bufferlen == this.buffer.length) {
+            flushBuffer();
+        }
+        this.buffer[this.bufferlen++] = (byte)b;
     }
     
     public void writeLine(final String s) throws IOException {
         if (s == null) {
             return;
         }
-        this.outstream.write(s.getBytes(this.charset));
-        this.outstream.write(CRLF);
+        write(s.getBytes(this.charset));
+        write(CRLF);
     }
     
     public void reset(final HttpParams params) {

Added: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/ByteArrayBuffer.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/ByteArrayBuffer.java?rev=267282&view=auto
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/ByteArrayBuffer.java (added)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/ByteArrayBuffer.java Fri Sep  2 12:37:23 2005
@@ -0,0 +1,100 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ *  Copyright 1999-2004 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.util;
+
+/**
+ * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
+ * 
+ * @version $Revision$
+ * 
+ * @since 4.0
+ */
+public class ByteArrayBuffer  {
+    
+    private byte[] buffer;
+    private int len;
+
+    public ByteArrayBuffer(int capacity) {
+        super();
+        if (capacity < 0) {
+            throw new IllegalArgumentException("Buffer capacity may not be negative");
+        }
+        this.buffer = new byte[capacity]; 
+    }
+
+    public void append(final byte[] b, int off, int len) {
+        if (b == null) {
+            return;
+        }
+        if ((off < 0) || (off > b.length) || (len < 0) ||
+                ((off + len) < 0) || ((off + len) > b.length)) {
+            throw new IndexOutOfBoundsException();
+        }
+        if (len == 0) {
+            return;
+        }
+        int newlen = this.len + len;
+        if (newlen > this.buffer.length) {
+            byte newbuffer[] = new byte[Math.max(this.buffer.length << 1, newlen)];
+            System.arraycopy(this.buffer, 0, newbuffer, 0, this.len);
+            this.buffer = newbuffer;
+        }
+        System.arraycopy(b, off, this.buffer, this.len, len);
+        this.len = newlen;
+    }
+
+    public void clear() {
+    	this.len = 0;
+    }
+    
+    public byte[] getBytes() {
+        return this.buffer;
+    }
+    
+    public int byteAt(int i) {
+        return this.buffer[i];
+    }
+    
+    public int capacity() {
+        return this.buffer.length;
+    }
+    
+    public int length() {
+        return this.len;
+    }
+
+    public void setLength(int len) {
+        if (len < 0 || len > this.buffer.length) {
+            throw new IndexOutOfBoundsException();
+        }
+        this.len = len;
+    }
+    
+}

Propchange: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/ByteArrayBuffer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/ByteArrayBuffer.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/ByteArrayBuffer.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestDefaultEntityGenerator.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestDefaultEntityGenerator.java?rev=267282&r1=267281&r2=267282&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestDefaultEntityGenerator.java (original)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestDefaultEntityGenerator.java Fri Sep  2 12:37:23 2005
@@ -123,7 +123,6 @@
         assertNotNull(entity);
         assertEquals(-1, entity.getContentLength());
         assertFalse(entity.isChunked());
-        assertTrue(entity.getContent() instanceof ByteArrayInputStream);
     }
 
     public void testEntityWithUnsupportedTransferEncoding() throws Exception {
@@ -320,19 +319,6 @@
         assertFalse(entity.getContent() instanceof ContentLengthInputStream);
         assertFalse(entity.getContent() instanceof ChunkedInputStream);
         assertTrue(entity.getContent() instanceof HttpDataInputStream);
-    }
-
-    public void testOldIOWrapper() throws Exception {
-        InputStream instream = new ByteArrayInputStream(new byte[] {});
-        HttpDataReceiver datareceiver = new InputStreamHttpDataReceiver(instream);
-        HttpMutableMessage message = new BasicHttpMessage();
-        message.addHeader(new Header("Content-Type", "unknown"));
-        EntityGenerator entitygen = new DefaultEntityGenerator();
-        HttpEntity entity = entitygen.generate(datareceiver, message);
-        assertNotNull(entity);
-        assertEquals(-1, entity.getContentLength());
-        assertFalse(entity.isChunked());
-        assertTrue(entity.getContent() instanceof ByteArrayInputStream);
     }
 
 }

Modified: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestNIOHttpTransmitterAndReceiver.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestNIOHttpTransmitterAndReceiver.java?rev=267282&r1=267281&r2=267282&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestNIOHttpTransmitterAndReceiver.java (original)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestNIOHttpTransmitterAndReceiver.java Fri Sep  2 12:37:23 2005
@@ -70,7 +70,13 @@
 
     public void testInit() throws Exception {
         ByteArrayOutputStream out = new ByteArrayOutputStream();
-        new HttpDataTransmitterMockup(Channels.newChannel(out), -10); 
+        new HttpDataTransmitterMockup(Channels.newChannel(out), 10); 
+        try {
+            new HttpDataTransmitterMockup(Channels.newChannel(out), -10); 
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            //expected
+        }
         try {
             new HttpDataTransmitterMockup(null, 1024); 
             fail("IllegalArgumentException should have been thrown");
@@ -78,7 +84,13 @@
             //expected
         }
         ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
-        new HttpDataReceiverMockup(Channels.newChannel(in), -10); 
+        new HttpDataReceiverMockup(Channels.newChannel(in), 10); 
+        try {
+            new HttpDataReceiverMockup(Channels.newChannel(in), -10); 
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            //expected
+        }
         try {
             new HttpDataReceiverMockup((ReadableByteChannel)null, 1024); 
             fail("IllegalArgumentException should have been thrown");

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=267282&r1=267281&r2=267282&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 Fri Sep  2 12:37:23 2005
@@ -36,6 +36,7 @@
 import org.apache.http.Header;
 import org.apache.http.mockup.HttpDataReceiverMockup;
 import org.apache.http.mockup.HttpDataTransmitterMockup;
+import org.apache.http.mockup.OutputStreamHttpDataTransmitterMockup;
 import org.apache.http.util.EncodingUtil;
 
 import junit.framework.Test;
@@ -271,7 +272,7 @@
     public void testChunkedConsitance() throws IOException {
         String input = "76126;27823abcd;:q38a-\nkjc\rk%1ad\tkh/asdui\r\njkh+?\\suweb";
         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
-        OutputStream out = new ChunkedOutputStream(new OutputStreamHttpDataTransmitter(buffer));
+        OutputStream out = new ChunkedOutputStream(new OutputStreamHttpDataTransmitterMockup(buffer));
         out.write(EncodingUtil.getBytes(input, CONTENT_CHARSET));
         out.flush();
         out.close();
@@ -355,7 +356,7 @@
     public void testChunkedOutputStreamSmallChunk() throws IOException {
         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
         ChunkedOutputStream out = new ChunkedOutputStream(
-        		new OutputStreamHttpDataTransmitter(buffer), 2);
+        		new OutputStreamHttpDataTransmitterMockup(buffer), 2);
         out.write('1');  
         out.finish();
         out.close();

Modified: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestOldIOHttpDataReceiverAndTransmitter.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestOldIOHttpDataReceiverAndTransmitter.java?rev=267282&r1=267281&r2=267282&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestOldIOHttpDataReceiverAndTransmitter.java (original)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestOldIOHttpDataReceiverAndTransmitter.java Fri Sep  2 12:37:23 2005
@@ -30,7 +30,6 @@
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
-import java.io.IOException;
 import java.io.InputStream;
 
 import junit.framework.Test;
@@ -38,7 +37,10 @@
 import junit.framework.TestSuite;
 
 import org.apache.http.impl.DefaultHttpParams;
-import org.apache.http.util.EncodingUtil;
+import org.apache.http.mockup.InputStreamHttpDataReceiverMockup;
+import org.apache.http.mockup.OutputStreamHttpDataTransmitterMockup;
+import org.apache.http.params.HttpParams;
+import org.apache.http.params.HttpProtocolParams;
 
 public class TestOldIOHttpDataReceiverAndTransmitter extends TestCase {
 
@@ -58,59 +60,296 @@
         junit.textui.TestRunner.main(testCaseName);
     }
 
-    public void testConstructors() {
-        String s = "aaaaa";
-        InputStream in = new ByteArrayInputStream(EncodingUtil.getAsciiBytes(s));
-        new InputStreamHttpDataReceiver(in);
+    public void testInit() throws Exception {
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        new OutputStreamHttpDataTransmitterMockup(out); 
+        try {
+            new OutputStreamHttpDataTransmitterMockup(null); 
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            //expected
+        }
+        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+        new InputStreamHttpDataReceiverMockup(in, 10); 
         try {
-            new InputStreamHttpDataReceiver(null);
+            new InputStreamHttpDataReceiverMockup(in, -10); 
             fail("IllegalArgumentException should have been thrown");
         } catch (IllegalArgumentException ex) {
-            // expected
+            //expected
         }
-        new OutputStreamHttpDataTransmitter(new ByteArrayOutputStream()); 
         try {
-            new OutputStreamHttpDataTransmitter(null);
+            new InputStreamHttpDataReceiverMockup((InputStream)null, 1024); 
             fail("IllegalArgumentException should have been thrown");
         } catch (IllegalArgumentException ex) {
-            // expected
+            //expected
         }
     }
     
-    public void testInputStreamHttpDataReceiver() throws IOException {
-        String s = "aaaaa";
-        InputStream in = new ByteArrayInputStream(EncodingUtil.getAsciiBytes(s));
-        InputStreamHttpDataReceiver datareceiver = new InputStreamHttpDataReceiver(in);
-        assertNotNull(datareceiver.getInputStream());
-        assertTrue(datareceiver.isDataAvailable(1));
-        assertEquals('a', datareceiver.read());
-        byte[] tmp = new byte[2];
-        datareceiver.read(tmp);
-        assertEquals('a', tmp[0]);
-        assertEquals('a', tmp[1]);
-        datareceiver.read(tmp, 0, tmp.length);
-        assertEquals('a', tmp[0]);
-        assertEquals('a', tmp[1]);
-        assertEquals(-1, datareceiver.read());
-        datareceiver.reset(new DefaultHttpParams(null));
+    public void testBasicReadWriteLine() throws Exception {
+        
+        String[] teststrs = new String[5];
+        teststrs[0] = "Hello";
+        teststrs[1] = "This string should be much longer than the size of the output buffer " +
+                "which is only 16 bytes for this test";
+        StringBuffer buffer = new StringBuffer();
+        for (int i = 0; i < 15; i++) {
+            buffer.append("123456789 ");
+        }
+        buffer.append("and stuff like that");
+        teststrs[2] = buffer.toString();
+        teststrs[3] = "";
+        teststrs[4] = "And goodbye";
+        
+        OutputStreamHttpDataTransmitterMockup transmitter = 
+            new OutputStreamHttpDataTransmitterMockup(); 
+        for (int i = 0; i < teststrs.length; i++) {
+            transmitter.writeLine(teststrs[i]);
+        }
+        //this write operation should have no effect
+        transmitter.writeLine(null);
+        transmitter.flush();
+        
+        InputStreamHttpDataReceiverMockup receiver = 
+            new InputStreamHttpDataReceiverMockup(transmitter.getData());
+
+        for (int i = 0; i < teststrs.length; i++) {
+            assertEquals(teststrs[i], receiver.readLine());
+        }
+        assertNull(receiver.readLine());
+        assertNull(receiver.readLine());
     }
 
-    public void testOutputStreamHttpDataTransmitter() throws IOException {
-        String s = "aaaaa\r\n";
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-        OutputStreamHttpDataTransmitter datatransmitter = new OutputStreamHttpDataTransmitter(out);
-        datatransmitter.reset(new DefaultHttpParams(null));
+    public void testComplexReadWriteLine() throws Exception {
+        OutputStreamHttpDataTransmitterMockup transmitter = 
+            new OutputStreamHttpDataTransmitterMockup(); 
+        transmitter.write(new byte[] {'a', '\n'});
+        transmitter.write(new byte[] {'\r', '\n'});
+        transmitter.write(new byte[] {'\r', '\r', '\n'});
+        transmitter.write(new byte[] {'\n'});
+        //these write operations should have no effect
+        transmitter.write(null);
+        transmitter.write(null, 0, 12);
+        
+        transmitter.flush();
+
+        StringBuffer buffer = new StringBuffer();
+        for (int i = 0; i < 14; i++) {
+            buffer.append("a");
+        }
+        String s1 = buffer.toString();
+        buffer.append("\r\n");
+        transmitter.write(buffer.toString().getBytes("US-ASCII"));
+        transmitter.flush();
 
-        assertNotNull(datatransmitter.getOutputStream());
+        buffer.setLength(0);
+        for (int i = 0; i < 15; i++) {
+            buffer.append("a");
+        }
+        String s2 = buffer.toString();
+        buffer.append("\r\n");
+        transmitter.write(buffer.toString().getBytes("US-ASCII"));
+        transmitter.flush();
+
+        buffer.setLength(0);
+        for (int i = 0; i < 16; i++) {
+            buffer.append("a");
+        }
+        String s3 = buffer.toString();
+        buffer.append("\r\n");
+        transmitter.write(buffer.toString().getBytes("US-ASCII"));
+        transmitter.flush();
+
+        transmitter.write(new byte[] {'a'});
+        transmitter.flush();
+        
+        InputStreamHttpDataReceiverMockup receiver = 
+            new InputStreamHttpDataReceiverMockup(transmitter.getData());
+
+        assertEquals("a", receiver.readLine());
+        assertEquals("", receiver.readLine());
+        assertEquals("\r", receiver.readLine());
+        assertEquals("", receiver.readLine());
+        assertEquals(s1, receiver.readLine());
+        assertEquals(s2, receiver.readLine());
+        assertEquals(s3, receiver.readLine());
+        assertEquals("a", receiver.readLine());
+        assertNull(receiver.readLine());
+        assertNull(receiver.readLine());
+    }
+    
+    public void testBasicReadWriteLineLargeBuffer() throws Exception {
+        
+        String[] teststrs = new String[5];
+        teststrs[0] = "Hello";
+        teststrs[1] = "This string should be much longer than the size of the output buffer " +
+                "which is only 16 bytes for this test";
+        StringBuffer buffer = new StringBuffer();
+        for (int i = 0; i < 15; i++) {
+            buffer.append("123456789 ");
+        }
+        buffer.append("and stuff like that");
+        teststrs[2] = buffer.toString();
+        teststrs[3] = "";
+        teststrs[4] = "And goodbye";
         
-        datatransmitter.write('a');
-        datatransmitter.write(new byte[] {'a'});
-        datatransmitter.write(new byte[] {'a'}, 0, 1);
-        datatransmitter.writeLine("aa");
-        datatransmitter.writeLine(null);
-        datatransmitter.flush();
+        OutputStreamHttpDataTransmitterMockup transmitter = 
+            new OutputStreamHttpDataTransmitterMockup(); 
+        for (int i = 0; i < teststrs.length; i++) {
+            transmitter.writeLine(teststrs[i]);
+        }
+        //this write operation should have no effect
+        transmitter.writeLine(null);
+        transmitter.flush();
         
-        assertEquals(s, out.toString("US-ASCII"));
+        InputStreamHttpDataReceiverMockup receiver = 
+            new InputStreamHttpDataReceiverMockup(transmitter.getData(), 1024);
+
+        for (int i = 0; i < teststrs.length; i++) {
+            assertEquals(teststrs[i], receiver.readLine());
+        }
+        assertNull(receiver.readLine());
+        assertNull(receiver.readLine());
     }
+
+    public void testReadWriteBytes() throws Exception {
+        // make the buffer larger than that of transmitter
+        byte[] out = new byte[40];
+        for (int i = 0; i < out.length; i++) {
+            out[i] = (byte)('0' + i);
+        }
+        OutputStreamHttpDataTransmitterMockup transmitter = 
+            new OutputStreamHttpDataTransmitterMockup();
+        int off = 0;
+        int remaining = out.length;
+        while (remaining > 0) {
+            int chunk = 10;
+            if (chunk > remaining) {
+                chunk = remaining;
+            }
+            transmitter.write(out, off, chunk);
+            off += chunk;
+            remaining -= chunk;
+        }
+        transmitter.flush();
+
+        byte[] tmp = transmitter.getData();
+        assertEquals(out.length, tmp.length);
+        for (int i = 0; i < out.length; i++) {
+            assertEquals(out[i], tmp[i]);
+        }
+        
+        InputStreamHttpDataReceiverMockup receiver = 
+            new InputStreamHttpDataReceiverMockup(tmp);
+
+        // these read operations will have no effect
+        assertEquals(0, receiver.read(null, 0, 10));
+        assertEquals(0, receiver.read(null));        
+        
+        byte[] in = new byte[40];
+        off = 0;
+        remaining = in.length;
+        while (remaining > 0) {
+            int chunk = 10;
+            if (chunk > remaining) {
+                chunk = remaining;
+            }
+            int l = receiver.read(in, off, chunk);
+            if (l == -1) {
+                break;
+            }
+            off += l;
+            remaining -= l;
+        }
+        for (int i = 0; i < out.length; i++) {
+            assertEquals(out[i], in[i]);
+        }
+        assertEquals(-1, receiver.read(tmp));
+        assertEquals(-1, receiver.read(tmp));
+    }
+    
+    public void testReadWriteByte() throws Exception {
+        // make the buffer larger than that of transmitter
+        byte[] out = new byte[40];
+        for (int i = 0; i < out.length; i++) {
+            out[i] = (byte)('0' + i);
+        }
+        OutputStreamHttpDataTransmitterMockup transmitter = 
+            new OutputStreamHttpDataTransmitterMockup();
+        for (int i = 0; i < out.length; i++) {
+            transmitter.write(out[i]);
+        }
+        transmitter.flush();
+
+        byte[] tmp = transmitter.getData();
+        assertEquals(out.length, tmp.length);
+        for (int i = 0; i < out.length; i++) {
+            assertEquals(out[i], tmp[i]);
+        }
+        
+        InputStreamHttpDataReceiverMockup receiver = 
+            new InputStreamHttpDataReceiverMockup(tmp);
+
+        byte[] in = new byte[40];
+        for (int i = 0; i < in.length; i++) {
+            in[i] = (byte)receiver.read();
+        }
+        for (int i = 0; i < out.length; i++) {
+            assertEquals(out[i], in[i]);
+        }
+        assertEquals(-1, receiver.read());
+        assertEquals(-1, receiver.read());
+    }
+
+    static final int SWISS_GERMAN_HELLO [] = {
+        0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4
+    };
+        
+    static final int RUSSIAN_HELLO [] = {
+        0x412, 0x441, 0x435, 0x43C, 0x5F, 0x43F, 0x440, 0x438, 
+        0x432, 0x435, 0x442 
+    }; 
+    
+    private static String constructString(int [] unicodeChars) {
+        StringBuffer buffer = new StringBuffer();
+        if (unicodeChars != null) {
+            for (int i = 0; i < unicodeChars.length; i++) {
+                buffer.append((char)unicodeChars[i]); 
+            }
+        }
+        return buffer.toString();
+    }
+
+    public void testMultibyteCodedReadWriteLine() throws Exception {
+        String s1 = constructString(SWISS_GERMAN_HELLO);
+        String s2 = constructString(RUSSIAN_HELLO);
+        String s3 = "Like hello and stuff";
+        
+        HttpParams params = new DefaultHttpParams(null);
+        HttpProtocolParams.setHttpElementCharset(params, "UTF-8");
+        
+        OutputStreamHttpDataTransmitterMockup transmitter = 
+            new OutputStreamHttpDataTransmitterMockup();
+        transmitter.reset(params);
+
+        for (int i = 0; i < 10; i++) {
+            transmitter.writeLine(s1);
+            transmitter.writeLine(s2);
+            transmitter.writeLine(s3);
+        }
+        transmitter.flush();
+        
+        InputStreamHttpDataReceiverMockup receiver = 
+            new InputStreamHttpDataReceiverMockup(transmitter.getData());
+        receiver.reset(params);
+
+        for (int i = 0; i < 10; i++) {
+            assertEquals(s1, receiver.readLine());
+            assertEquals(s2, receiver.readLine());
+            assertEquals(s3, receiver.readLine());
+        }
+        assertNull(receiver.readLine());
+        assertNull(receiver.readLine());
+    }
+    
 }
 

Added: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/mockup/InputStreamHttpDataReceiverMockup.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/mockup/InputStreamHttpDataReceiverMockup.java?rev=267282&view=auto
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/mockup/InputStreamHttpDataReceiverMockup.java (added)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/mockup/InputStreamHttpDataReceiverMockup.java Fri Sep  2 12:37:23 2005
@@ -0,0 +1,46 @@
+package org.apache.http.mockup;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+
+import org.apache.http.io.InputStreamHttpDataReceiver;
+
+/**
+ * {@link HttpDataInputStream} mockup implementation.
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ */
+public class InputStreamHttpDataReceiverMockup extends InputStreamHttpDataReceiver {
+
+    public static int BUFFER_SIZE = 16;
+    
+    public InputStreamHttpDataReceiverMockup(final InputStream instream, int buffersize) {
+        super(instream);
+        if (buffersize <= 0) {
+            throw new IllegalArgumentException("Buffer size may not be negative or zero");
+        }
+        initBuffer(buffersize);
+    }
+
+    public InputStreamHttpDataReceiverMockup(final byte[] bytes) {
+        this(bytes, BUFFER_SIZE);
+    }
+
+    public InputStreamHttpDataReceiverMockup(final byte[] bytes, int buffersize) {
+        super(new ByteArrayInputStream(bytes));
+        initBuffer(buffersize);
+    }
+
+    public InputStreamHttpDataReceiverMockup(final String s, final String charset, int buffersize) 
+        throws UnsupportedEncodingException {
+        this(s.getBytes(charset), buffersize);
+    }
+    
+    public InputStreamHttpDataReceiverMockup(final String s, final String charset) 
+        throws UnsupportedEncodingException {
+        this(s.getBytes(charset));
+    
+    }
+    
+}

Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/mockup/InputStreamHttpDataReceiverMockup.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/mockup/InputStreamHttpDataReceiverMockup.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/mockup/InputStreamHttpDataReceiverMockup.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/mockup/OutputStreamHttpDataTransmitterMockup.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/mockup/OutputStreamHttpDataTransmitterMockup.java?rev=267282&view=auto
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/mockup/OutputStreamHttpDataTransmitterMockup.java (added)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/mockup/OutputStreamHttpDataTransmitterMockup.java Fri Sep  2 12:37:23 2005
@@ -0,0 +1,43 @@
+package org.apache.http.mockup;
+
+import java.io.ByteArrayOutputStream;
+import java.io.OutputStream;
+
+import org.apache.http.io.OutputStreamHttpDataTransmitter;
+
+/**
+ * {@link HttpDataTransmitter} mockup implementation.
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ */
+public class OutputStreamHttpDataTransmitterMockup extends OutputStreamHttpDataTransmitter {
+
+    private ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+    public static int BUFFER_SIZE = 16;
+    
+    public OutputStreamHttpDataTransmitterMockup(final OutputStream outstream, int buffersize) {
+        super(outstream);
+        if (buffersize <= 0) {
+            throw new IllegalArgumentException("Buffer size may not be negative or zero");
+        }
+        initBuffer(buffersize);
+    }
+
+    public OutputStreamHttpDataTransmitterMockup(final ByteArrayOutputStream buffer) {
+        this(buffer, BUFFER_SIZE);
+        this.buffer = buffer;
+    }
+
+    public OutputStreamHttpDataTransmitterMockup() {
+        this(new ByteArrayOutputStream());
+    }
+
+    public byte[] getData() {
+        if (this.buffer != null) {
+            return this.buffer.toByteArray();
+        } else {
+            return new byte[] {};
+        }
+    }
+    
+}

Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/mockup/OutputStreamHttpDataTransmitterMockup.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/mockup/OutputStreamHttpDataTransmitterMockup.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/mockup/OutputStreamHttpDataTransmitterMockup.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestAllUtil.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestAllUtil.java?rev=267282&r1=267281&r2=267282&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestAllUtil.java (original)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestAllUtil.java Fri Sep  2 12:37:23 2005
@@ -41,7 +41,6 @@
         suite.addTest(TestLangUtils.suite());
         suite.addTest(TestExceptionUtils.suite());
         suite.addTest(TestEncodingUtils.suite());
-        suite.addTest(TestHttpLineParser.suite());
         suite.addTest(TestParameterParser.suite());
         suite.addTest(TestParameterFormatter.suite());
         suite.addTest(TestHeadersParser.suite());



Mime
View raw message