Index: httpcore/module-main/src/test/java/org/apache/http/util/TestByteArrayBuffer.java =================================================================== --- httpcore/module-main/src/test/java/org/apache/http/util/TestByteArrayBuffer.java (revision 729253) +++ httpcore/module-main/src/test/java/org/apache/http/util/TestByteArrayBuffer.java (working copy) @@ -195,7 +195,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"; Index: httpcore/module-main/src/main/java/org/apache/http/util/ByteArrayBuffer.java =================================================================== --- httpcore/module-main/src/main/java/org/apache/http/util/ByteArrayBuffer.java (revision 729253) +++ httpcore/module-main/src/main/java/org/apache/http/util/ByteArrayBuffer.java (working copy) @@ -224,6 +224,24 @@ } /** + * 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 required + * argument is non-positive, this method takes no action. + * + * @param required the minimum required capacity. + */ + 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. @@ -269,4 +287,56 @@ 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 + * beginIndex and finishing at endIndex. + * If no such byte occurs in this buffer within the specified bounds, + * -1 is returned. + *

+ * There is no restriction on the value of beginIndex and + * endIndex. If beginIndex is negative, + * it has the same effect as if it were zero. If endIndex is + * greater than {@link #length()}, it has the same effect as if it were + * {@link #length()}. If the beginIndex is greater than + * the endIndex, -1 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 -1 if the byte does + * not occur. + */ + 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 0 and finishing + * at {@link #length()}. If no such byte occurs in this buffer within + * those bounds, -1 is returned. + * + * @param b the byte to search for. + * @return the index of the first occurrence of the byte in the + * buffer, or -1 if the byte does not occur. + */ + public int indexOf(byte b) { + return indexOf(b, 0, this.len); + } }