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 2009/07/12 16:03:10 UTC

svn commit: r793351 - in /httpcomponents/httpcore/trunk: httpcore-osgi/pom.xml httpcore/src/main/java/org/apache/http/util/ByteArrayBuffer.java httpcore/src/test/java/org/apache/http/util/TestByteArrayBuffer.java

Author: olegk
Date: Sun Jul 12 14:03:09 2009
New Revision: 793351

URL: http://svn.apache.org/viewvc?rev=793351&view=rev
Log:
HTTPCORE-176: Add #ensureCapacity and #indexOf methods to ByteArrayBuffer

Modified:
    httpcomponents/httpcore/trunk/httpcore-osgi/pom.xml
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/ByteArrayBuffer.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestByteArrayBuffer.java

Modified: httpcomponents/httpcore/trunk/httpcore-osgi/pom.xml
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-osgi/pom.xml?rev=793351&r1=793350&r2=793351&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-osgi/pom.xml (original)
+++ httpcomponents/httpcore/trunk/httpcore-osgi/pom.xml Sun Jul 12 14:03:09 2009
@@ -78,7 +78,7 @@
       <plugin>
         <groupId>org.apache.felix</groupId>
         <artifactId>maven-bundle-plugin</artifactId>
-        <version>1.4.3</version>
+        <version>2.0.0</version>
         <extensions>true</extensions>
         <configuration>
           <instructions>

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/ByteArrayBuffer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/ByteArrayBuffer.java?rev=793351&r1=793350&r2=793351&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/ByteArrayBuffer.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/ByteArrayBuffer.java Sun Jul 12 14:03:09 2009
@@ -220,6 +220,26 @@
     }
 
     /**
+     * Ensures that the capacity is at least equal to the specified minimum.
+     * If the current capacity is less than the argument, then a new internal
+     * array is allocated with greater capacity. If the <code>required</code> 
+     * argument is non-positive, this method takes no action.
+     *
+     * @param   required   the minimum required capacity.
+     * 
+     * @since 4.1
+     */
+    public void ensureCapacity(int required) {
+        if (required <= 0) {
+            return;
+        }
+        int available = this.buffer.length - this.len;
+        if (required > available) {
+            expand(this.len + required);
+        }
+    }
+    
+    /**
      * Returns reference to the underlying byte array.
      * 
      * @return the byte array.
@@ -265,4 +285,60 @@
         return this.len == this.buffer.length; 
     }
     
+    /**
+     * Returns the index within this buffer of the first occurrence of the
+     * specified byte, starting the search at the specified 
+     * <code>beginIndex</code> and finishing at <code>endIndex</code>.
+     * If no such byte occurs in this buffer within the specified bounds, 
+     * <code>-1</code> is returned.
+     * <p>
+     * There is no restriction on the value of <code>beginIndex</code> and 
+     * <code>endIndex</code>. If <code>beginIndex</code> is negative, 
+     * it has the same effect as if it were zero. If <code>endIndex</code> is 
+     * greater than {@link #length()}, it has the same effect as if it were
+     * {@link #length()}. If the <code>beginIndex</code> is greater than 
+     * the <code>endIndex</code>, <code>-1</code> is returned.
+     *
+     * @param   b            the byte to search for.
+     * @param   beginIndex   the index to start the search from.
+     * @param   endIndex     the index to finish the search at.
+     * @return  the index of the first occurrence of the byte in the buffer
+     *   within the given bounds, or <code>-1</code> if the byte does 
+     *   not occur.
+     *   
+     * @since 4.1
+     */
+    public int indexOf(byte b, int beginIndex, int endIndex) {
+        if (beginIndex < 0) {
+            beginIndex = 0;
+        }
+        if (endIndex > this.len) {
+            endIndex = this.len;
+        }
+        if (beginIndex > endIndex) {
+            return -1;
+        }
+        for (int i = beginIndex; i < endIndex; i++) {
+            if (this.buffer[i] == b) {
+                return i;
+            }
+        }
+        return -1;
+    }
+    
+    /**
+     * Returns the index within this buffer of the first occurrence of the
+     * specified byte, starting the search at <code>0</code> and finishing 
+     * at {@link #length()}. If no such byte occurs in this buffer within 
+     * those bounds, <code>-1</code> is returned.
+     *
+     * @param   b   the byte to search for.
+     * @return  the index of the first occurrence of the byte in the 
+     *   buffer, or <code>-1</code> if the byte does not occur.
+     *   
+     * @since 4.1
+     */
+    public int indexOf(byte b) {
+        return indexOf(b, 0, this.len);
+    }
 }

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestByteArrayBuffer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestByteArrayBuffer.java?rev=793351&r1=793350&r2=793351&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestByteArrayBuffer.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestByteArrayBuffer.java Sun Jul 12 14:03:09 2009
@@ -194,7 +194,33 @@
             // expected
         }
     }
-    
+
+    public void testEnsureCapacity() throws Exception {
+        ByteArrayBuffer buffer = new ByteArrayBuffer(4);
+        buffer.ensureCapacity(2);
+        assertEquals(4, buffer.capacity());
+        buffer.ensureCapacity(8);
+        assertEquals(8, buffer.capacity());
+    }
+
+    public void testIndexOf() throws Exception {
+        final byte COLON = (byte) ':';
+        final byte COMMA = (byte) ',';
+        byte[] bytes = "name1: value1; name2: value2".getBytes("US-ASCII");
+        int index1 = 5;
+        int index2 = 20;
+       
+        ByteArrayBuffer buffer = new ByteArrayBuffer(16);
+        buffer.append(bytes, 0, bytes.length);
+
+        assertEquals(index1, buffer.indexOf(COLON));
+        assertEquals(-1, buffer.indexOf(COMMA));
+        assertEquals(index1, buffer.indexOf(COLON, -1, 11));
+        assertEquals(index1, buffer.indexOf(COLON, 0, 1000));
+        assertEquals(-1, buffer.indexOf(COLON, 2, 1));
+        assertEquals(index2, buffer.indexOf(COLON, index1 + 1, buffer.length()));
+    }
+
     public void testAppendCharArrayAsAscii() throws Exception {
         String s1 = "stuff";
         String s2 = " and more stuff";