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/11/20 22:43:53 UTC

svn commit: r345782 - in /jakarta/httpcomponents/trunk/http-core/src: java/org/apache/http/io/CharArrayBuffer.java java/org/apache/http/io/ChunkedInputStream.java test/org/apache/http/io/TestCharArrayBuffer.java

Author: olegk
Date: Sun Nov 20 13:43:42 2005
New Revision: 345782

URL: http://svn.apache.org/viewcvs?rev=345782&view=rev
Log:
Added more methods to help manipulate the content of the char array buffer.

Modified:
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/CharArrayBuffer.java
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/ChunkedInputStream.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/io/CharArrayBuffer.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/CharArrayBuffer.java?rev=345782&r1=345781&r2=345782&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 Sun Nov 20 13:43:42 2005
@@ -146,6 +146,57 @@
         return this.len == 0; 
     }
     
+    public int indexOf(int ch, int fromIndex) {
+        if (fromIndex < 0) {
+            fromIndex = 0;
+        }
+        if (fromIndex > this.len) {
+            return -1;
+        }
+        for (int i = fromIndex; i < this.len; i++) {
+            if (this.buffer[i] == ch) {
+                return i;
+            }
+        }
+        return -1;
+    }
+    
+    public int indexOf(int ch) {
+        return indexOf(ch, 0);
+    }
+
+    public String substring(int beginIndex, int endIndex) {
+        if (beginIndex < 0) {
+            throw new IndexOutOfBoundsException();
+        }
+        if (endIndex > this.len) {
+            throw new IndexOutOfBoundsException();
+        }
+        if (beginIndex > endIndex) {
+            throw new IndexOutOfBoundsException();
+        }
+        return new String(this.buffer, beginIndex, endIndex - beginIndex);
+    }
+    
+    public String substringTrimmed(int beginIndex, int endIndex) {
+        if (beginIndex < 0) {
+            throw new IndexOutOfBoundsException();
+        }
+        if (endIndex > this.len) {
+            throw new IndexOutOfBoundsException();
+        }
+        if (beginIndex > endIndex) {
+            throw new IndexOutOfBoundsException();
+        }
+        while (beginIndex < endIndex && Character.isWhitespace(this.buffer[beginIndex])) {
+            beginIndex++;
+        }
+        while (endIndex > beginIndex && Character.isWhitespace(this.buffer[endIndex - 1])) {
+            endIndex--;
+        }
+        return new String(this.buffer, beginIndex, endIndex - beginIndex);
+    }
+    
     public String toString() {
     	return new String(this.buffer, 0, this.len);
     }

Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/ChunkedInputStream.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/ChunkedInputStream.java?rev=345782&r1=345781&r2=345782&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/ChunkedInputStream.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/ChunkedInputStream.java Sun Nov 20 13:43:42 2005
@@ -123,6 +123,8 @@
     /** The data receiver that we're wrapping */
     private HttpDataReceiver in;
 
+    private final CharArrayBuffer buffer;
+    
     /** The chunk size */
     private int chunkSize;
 
@@ -147,6 +149,7 @@
     	}
         this.in = in;
         this.pos = 0;
+        this.buffer = new CharArrayBuffer(16);
     }
 
     /**
@@ -262,23 +265,22 @@
             }
         }
         //parse data
-        String dataString = this.in.readLine();
-        if (dataString == null) {
+        this.buffer.clear();
+        int i = this.in.readLine(this.buffer);
+        if (i == -1) {
             throw new MalformedChunkCodingException(
             		"Chunked stream ended unexpectedly");
         }
-        int separator = dataString.indexOf(';');
-        dataString = (separator > 0)
-            ? dataString.substring(0, separator).trim()
-            : dataString.trim();
-
-        int result;
+        int separator = this.buffer.indexOf(';');
+        if (separator < 0) {
+            separator = this.buffer.length();
+        }
+        String s = this.buffer.substringTrimmed(0, separator);
         try {
-            result = Integer.parseInt(dataString, 16);
+            return Integer.parseInt(s, 16);
         } catch (NumberFormatException e) {
-            throw new MalformedChunkCodingException("Bad chunk size: " + dataString);
+            throw new MalformedChunkCodingException("Bad chunk header");
         }
-        return result;
     }
 
     /**

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=345782&r1=345781&r2=345782&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 Sun Nov 20 13:43:42 2005
@@ -208,5 +208,65 @@
         buffer.ensureCapacity(8);
         assertEquals(8, buffer.capacity());
     }
-        
+
+    public void testIndexOf() {
+        CharArrayBuffer buffer = new CharArrayBuffer(16);
+        buffer.append("name: value");
+        assertEquals(4, buffer.indexOf(':'));
+        assertEquals(-1, buffer.indexOf(','));
+        assertEquals(4, buffer.indexOf(':', -1));
+        assertEquals(-1, buffer.indexOf(',', 1000));
+    }
+    
+    public void testSubstring() {
+        CharArrayBuffer buffer = new CharArrayBuffer(16);
+        buffer.append(" name:  value    ");
+        assertEquals(5, buffer.indexOf(':'));
+        assertEquals(" name", buffer.substring(0, 5));
+        assertEquals("  value    ", buffer.substring(6, buffer.length()));
+        assertEquals("name", buffer.substringTrimmed(0, 5));
+        assertEquals("value", buffer.substringTrimmed(6, buffer.length()));
+        assertEquals("", buffer.substringTrimmed(13, buffer.length()));
+    }
+    
+    public void testSubstringIndexOfOutBound() {
+        CharArrayBuffer buffer = new CharArrayBuffer(16);
+        buffer.append("stuff");
+        try {
+            buffer.substring(-2, 10);
+            fail("IndexOutOfBoundsException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            buffer.substringTrimmed(-2, 10);
+            fail("IndexOutOfBoundsException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            buffer.substring(12, 10);
+            fail("IndexOutOfBoundsException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            buffer.substringTrimmed(12, 10);
+            fail("IndexOutOfBoundsException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            buffer.substring(2, 1);
+            fail("IndexOutOfBoundsException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            buffer.substringTrimmed(2, 1);
+            fail("IndexOutOfBoundsException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+    }
 }