You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2005/12/09 23:34:30 UTC

svn commit: r355619 - in /jakarta/httpcomponents/trunk/http-core/src: java/org/apache/http/impl/io/ java/org/apache/http/io/ test/org/apache/http/io/

Author: olegk
Date: Fri Dec  9 14:34:15 2005
New Revision: 355619

URL: http://svn.apache.org/viewcvs?rev=355619&view=rev
Log:
* CharArrayBuffer and ByteArrayBuffer changed to support simple byte to char and char to byte conversion only.
* AbstractHttpDataTransmitter#writeLine optimized to reduce the amount of intermediate garbage

Modified:
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/io/AbstractHttpDataReceiver.java
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/io/AbstractHttpDataTransmitter.java
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/ByteArrayBuffer.java
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/CharArrayBuffer.java
    jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/io/TestByteArrayBuffer.java
    jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/io/TestCharArrayBuffer.java

Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/io/AbstractHttpDataReceiver.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/io/AbstractHttpDataReceiver.java?rev=355619&r1=355618&r2=355619&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/io/AbstractHttpDataReceiver.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/io/AbstractHttpDataReceiver.java Fri Dec  9 14:34:15 2005
@@ -191,7 +191,8 @@
         return lineFromLineBuffer(charbuffer);
     }
     
-    private int lineFromLineBuffer(final CharArrayBuffer charbuffer) {
+    private int lineFromLineBuffer(final CharArrayBuffer charbuffer) 
+            throws IOException {
         // discard LF if found
         int l = this.linebuffer.length(); 
         if (l > 0) {
@@ -211,12 +212,16 @@
         if (this.ascii) {
             charbuffer.append(this.linebuffer, 0, l);
         } else {
-            charbuffer.append(this.linebuffer, 0, l, this.charset);
+            // This is VERY memory inefficient, BUT since non-ASCII charsets are 
+            // NOT meant to be used anyway, there's no point optimizing it
+            String s = new String(this.linebuffer.buffer(), 0, l, this.charset);
+            charbuffer.append(s);
         }
         return l;
     }
     
-    private int lineFromReadBuffer(final CharArrayBuffer charbuffer, int pos) {
+    private int lineFromReadBuffer(final CharArrayBuffer charbuffer, int pos) 
+            throws IOException {
         int off = this.bufferpos;
         int len;
         this.bufferpos = pos + 1;
@@ -228,7 +233,10 @@
         if (this.ascii) {
             charbuffer.append(this.buffer, off, len);
         } else {
-            charbuffer.append(this.buffer, off, len, this.charset);
+            // This is VERY memory inefficient, BUT since non-ASCII charsets are 
+            // NOT meant to be used anyway, there's no point optimizing it
+            String s = new String(this.buffer, off, len, this.charset);
+            charbuffer.append(s);
         }
         return len;
     }

Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/io/AbstractHttpDataTransmitter.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/io/AbstractHttpDataTransmitter.java?rev=355619&r1=355618&r2=355619&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/io/AbstractHttpDataTransmitter.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/io/AbstractHttpDataTransmitter.java Fri Dec  9 14:34:15 2005
@@ -37,6 +37,7 @@
 import org.apache.http.io.HttpDataTransmitter;
 import org.apache.http.params.HttpParams;
 import org.apache.http.params.HttpProtocolParams;
+import org.apache.http.util.EncodingUtils;
 
 /**
  * <p>Old IO Compatibility wrapper</p>
@@ -50,11 +51,13 @@
     private static final int LF = 10;
     private static final byte[] CRLF = new byte[] {CR, LF};
 
+    private static final int MAX_CHUNK = 2048;
+    
     private OutputStream outstream;
     private ByteArrayBuffer buffer;
-    private int maxSize;
         
     private String charset = "US-ASCII";
+    private boolean ascii = true;
     
     protected void init(final OutputStream outstream, int buffersize) {
         if (outstream == null) {
@@ -65,7 +68,6 @@
         }
         this.outstream = outstream;
         this.buffer = new ByteArrayBuffer(buffersize);
-        this.maxSize = buffersize;
     }
     
     protected void flushBuffer() throws IOException {
@@ -84,16 +86,21 @@
         if (b == null) {
             return;
         }
-        int freecapacity = this.buffer.capacity() - this.buffer.length();
-        if (len > freecapacity || this.buffer.length() >= this.maxSize) {
+        // Do not want to buffer largish chunks
+        // if the byte array is larger then MAX_CHUNK
+        // write it directly to the output stream
+        if (len >= MAX_CHUNK || len > this.buffer.capacity()) {
             // flush the buffer
             flushBuffer();
-            freecapacity = this.buffer.capacity(); 
-        }
-        if (len > freecapacity || len > this.maxSize) {
-            // still does not fit, write directly to the out stream
+            // write directly to the out stream
             this.outstream.write(b, off, len);
         } else {
+            // Do not let the buffer grow unnecessarily
+            int freecapacity = this.buffer.capacity() - this.buffer.length();
+            if (len > freecapacity) {
+                // flush the buffer
+                flushBuffer();
+            }
             // buffer
             this.buffer.append(b, off, len);
         }
@@ -107,7 +114,7 @@
     }
     
     public void write(int b) throws IOException {
-        if (this.buffer.length() == this.buffer.capacity()) {
+        if (this.buffer.isFull()) {
             flushBuffer();
         }
         this.buffer.append(b);
@@ -121,15 +128,39 @@
         write(CRLF);
     }
     
-    public void writeLine(final CharArrayBuffer buffer) throws IOException {
+    public void writeLine(final CharArrayBuffer s) throws IOException {
         if (buffer == null) {
             return;
         }
-        writeLine(buffer.toString());
+        if (this.ascii) {
+            int off = 0;
+            int remaining = s.length();
+            while (remaining > 0) {
+                int chunk = this.buffer.capacity() - this.buffer.length();
+                chunk = Math.min(chunk, remaining);
+                if (chunk > 0) {
+                    this.buffer.append(s, off, chunk);
+                }
+                if (this.buffer.isFull()) {
+                    flushBuffer();
+                }
+                off =+ chunk;
+                remaining =- chunk;
+            }
+        } else {
+            // This is VERY memory inefficient, BUT since non-ASCII charsets are 
+            // NOT meant to be used anyway, there's no point optimizing it
+            byte[] tmp = s.toString().getBytes(this.charset);
+            write(tmp);
+        }
+        write(CRLF);
     }
     
     public void reset(final HttpParams params) {
         this.charset = HttpProtocolParams.getHttpElementCharset(params); 
+        this.ascii = 
+            this.charset.equalsIgnoreCase(EncodingUtils.ASCII_CHARSET) ||
+            this.charset.equalsIgnoreCase("ASCII");
     }
     
 }

Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/ByteArrayBuffer.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/ByteArrayBuffer.java?rev=355619&r1=355618&r2=355619&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/ByteArrayBuffer.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/ByteArrayBuffer.java Fri Dec  9 14:34:15 2005
@@ -29,8 +29,6 @@
 
 package org.apache.http.io;
 
-import org.apache.http.util.EncodingUtils;
-
 /**
  * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
  * 
@@ -107,14 +105,6 @@
         this.len = newlen;
     }
 
-    public void append(final char[] b, int off, int len, final String charset) {
-        if (b == null) {
-            return;
-        }
-        byte[] tmp = EncodingUtils.getBytes(new String(b, off, len), charset);
-        append(tmp, 0, tmp.length);
-    }
-
     public void append(final CharArrayBuffer b, int off, int len) {
         if (b == null) {
             return;
@@ -122,13 +112,6 @@
         append(b.buffer(), off, len);
     }
     
-    public void append(final CharArrayBuffer b, int off, int len, final String charset) {
-        if (b == null) {
-            return;
-        }
-        append(b.buffer(), off, len, charset);
-    }
-    
     public void clear() {
     	this.len = 0;
     }
@@ -166,6 +149,10 @@
     
     public boolean isEmpty() {
         return this.len == 0; 
+    }
+    
+    public boolean isFull() {
+        return this.len == this.buffer.length; 
     }
     
 }

Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/CharArrayBuffer.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/CharArrayBuffer.java?rev=355619&r1=355618&r2=355619&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/CharArrayBuffer.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/CharArrayBuffer.java Fri Dec  9 14:34:15 2005
@@ -29,8 +29,6 @@
 
 package org.apache.http.io;
 
-import org.apache.http.util.EncodingUtils;
-
 /**
  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
  * 
@@ -138,13 +136,6 @@
         this.len = newlen;
     }
     
-    public void append(final byte[] b, int off, int len, final String charset) {
-        if (b == null) {
-            return;
-        }
-        append(EncodingUtils.getString(b, off, len, charset));
-    }
-
     public void append(final ByteArrayBuffer b, int off, int len) {
         if (b == null) {
             return;
@@ -152,13 +143,6 @@
         append(b.buffer(), off, len);
     }
     
-    public void append(final ByteArrayBuffer b, int off, int len, final String charset) {
-        if (b == null) {
-            return;
-        }
-        append(b.buffer(), off, len, charset);
-    }
-    
     public void append(final Object obj) {
     	append(String.valueOf(obj));
     }
@@ -207,6 +191,10 @@
     
     public boolean isEmpty() {
         return this.len == 0; 
+    }
+    
+    public boolean isFull() {
+        return this.len == this.buffer.length; 
     }
     
     public int indexOf(int ch, int beginIndex, int endIndex) {

Modified: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/io/TestByteArrayBuffer.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/io/TestByteArrayBuffer.java?rev=355619&r1=355618&r2=355619&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/io/TestByteArrayBuffer.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/io/TestByteArrayBuffer.java Fri Dec  9 14:34:15 2005
@@ -190,25 +190,6 @@
     	}
     }
     
-    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 testAppendCharArrayAsAscii() throws Exception {
         String s1 = "stuff";
         String s2 = " and more stuff";
@@ -222,47 +203,21 @@
         assertEquals(s1 + s2, new String(buffer.toByteArray(), "US-ASCII"));
     }
     
-    public void testAppendCharArrayAsISO() throws Exception {
-        String s1 = constructString(SWISS_GERMAN_HELLO);
-        char[] b1 = s1.toCharArray();
-        
-        ByteArrayBuffer buffer = new ByteArrayBuffer(8);
-        buffer.append(b1, 0, b1.length);
-        
-        assertEquals(s1, new String(buffer.toByteArray(), "ISO-8859-1"));
-    }
-    
-    public void testAppendCharArrayWithCharset() throws Exception {
-        String s1 = constructString(SWISS_GERMAN_HELLO);
-        String s2 = constructString(RUSSIAN_HELLO);
-        char[] b1 = s1.toCharArray();
-        char[] b2 = s2.toCharArray();
-        
-        ByteArrayBuffer buffer = new ByteArrayBuffer(8);
-        buffer.append(b1, 0, b1.length, "UTF-8");
-        buffer.append(b2, 0, b2.length, "UTF-8");
-        
-        assertEquals(s1 + s2, new String(buffer.toByteArray(), "UTF-8"));
-    }
-    
     public void testAppendNullCharArray() throws Exception {
         ByteArrayBuffer buffer = new ByteArrayBuffer(8);
         buffer.append((char[])null, 0, 0);
-        buffer.append((char[])null, 0, 0, "US-ASCII");
         assertEquals(0, buffer.length());
     }
 
     public void testAppendEmptyCharArray() throws Exception {
         ByteArrayBuffer buffer = new ByteArrayBuffer(8);
         buffer.append(new char[] {}, 0, 0);
-        buffer.append(new char[] {}, 0, 0, "US-ASCII");
         assertEquals(0, buffer.length());
     }
 
     public void testAppendNullCharArrayBuffer() throws Exception {
         ByteArrayBuffer buffer = new ByteArrayBuffer(8);
         buffer.append((CharArrayBuffer)null, 0, 0);
-        buffer.append((CharArrayBuffer)null, 0, 0, "US-ASCII");
         assertEquals(0, buffer.length());
     }
 

Modified: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/io/TestCharArrayBuffer.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/io/TestCharArrayBuffer.java?rev=355619&r1=355618&r2=355619&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/io/TestCharArrayBuffer.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/io/TestCharArrayBuffer.java Fri Dec  9 14:34:15 2005
@@ -288,25 +288,6 @@
         }
     }    
     
-    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 testAppendAsciiByteArray() throws Exception {
         String s1 = "stuff";
         String s2 = " and more stuff";
@@ -320,40 +301,15 @@
         assertEquals("stuff and more stuff", buffer.toString());
     }
     
-    public void testAppendISOByteArray() throws Exception {
-        String s1 = constructString(SWISS_GERMAN_HELLO);
-        byte[] b1 = s1.getBytes("ISO-8859-1");
-        
-        CharArrayBuffer buffer = new CharArrayBuffer(8);
-        buffer.append(b1, 0, b1.length);
-        
-        assertEquals(s1, buffer.toString());
-    }
-    
-    public void testAppendByteArrayWithCharset() throws Exception {
-        String s1 = constructString(SWISS_GERMAN_HELLO);
-        String s2 = constructString(RUSSIAN_HELLO);
-        byte[] b1 = s1.getBytes("UTF-8");
-        byte[] b2 = s2.getBytes("UTF-8");
-        
-        CharArrayBuffer buffer = new CharArrayBuffer(8);
-        buffer.append(b1, 0, b1.length, "UTF-8");
-        buffer.append(b2, 0, b2.length, "UTF-8");
-        
-        assertEquals(s1 + s2, buffer.toString());
-    }
-    
     public void testAppendNullByteArray() throws Exception {
         CharArrayBuffer buffer = new CharArrayBuffer(8);
         buffer.append((byte[])null, 0, 0);
-        buffer.append((byte[])null, 0, 0, "US-ASCII");
         assertEquals("", buffer.toString());
     }
 
     public void testAppendNullByteArrayBuffer() throws Exception {
         CharArrayBuffer buffer = new CharArrayBuffer(8);
         buffer.append((ByteArrayBuffer)null, 0, 0);
-        buffer.append((ByteArrayBuffer)null, 0, 0, "US-ASCII");
         assertEquals("", buffer.toString());
     }