hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1599241 - in /httpcomponents/httpcore/trunk/httpcore/src: main/java/org/apache/http/util/CharArrayBuffer.java test/java/org/apache/http/util/TestCharArrayBuffer.java
Date Mon, 02 Jun 2014 15:33:58 GMT
Author: olegk
Date: Mon Jun  2 15:33:57 2014
New Revision: 1599241

URL: http://svn.apache.org/r1599241
Log:
CharArrayBuffer to implement CharSequence

Modified:
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/CharArrayBuffer.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestCharArrayBuffer.java

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/CharArrayBuffer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/CharArrayBuffer.java?rev=1599241&r1=1599240&r2=1599241&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/CharArrayBuffer.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/CharArrayBuffer.java
Mon Jun  2 15:33:57 2014
@@ -28,6 +28,7 @@
 package org.apache.http.util;
 
 import java.io.Serializable;
+import java.nio.CharBuffer;
 
 import org.apache.http.annotation.NotThreadSafe;
 import org.apache.http.protocol.HTTP;
@@ -38,7 +39,7 @@ import org.apache.http.protocol.HTTP;
  * @since 4.0
  */
 @NotThreadSafe
-public final class CharArrayBuffer implements Serializable {
+public final class CharArrayBuffer implements CharSequence, Serializable {
 
     private static final long serialVersionUID = -6208952725094867135L;
 
@@ -416,6 +417,15 @@ public final class CharArrayBuffer imple
      *             <code>endIndex</code>.
      */
     public String substring(final int beginIndex, final int endIndex) {
+        if (beginIndex < 0) {
+            throw new IndexOutOfBoundsException("Negative beginIndex: " + beginIndex);
+        }
+        if (endIndex > this.len) {
+            throw new IndexOutOfBoundsException("endIndex: " + endIndex + " > length:
" + this.len);
+        }
+        if (beginIndex > endIndex) {
+            throw new IndexOutOfBoundsException("beginIndex: " + beginIndex + " > endIndex:
" + endIndex);
+        }
         return new String(this.buffer, beginIndex, endIndex - beginIndex);
     }
 
@@ -426,8 +436,8 @@ public final class CharArrayBuffer imple
      * non-whitespace character with the index lesser than
      * <code>endIndex</code>.
      *
-     * @param      from   the beginning index, inclusive.
-     * @param      to     the ending index, exclusive.
+     * @param      beginIndex   the beginning index, inclusive.
+     * @param      endIndex     the ending index, exclusive.
      * @return     the specified substring.
      * @exception  IndexOutOfBoundsException  if the
      *             <code>beginIndex</code> is negative, or
@@ -435,25 +445,43 @@ public final class CharArrayBuffer imple
      *             buffer, or <code>beginIndex</code> is larger than
      *             <code>endIndex</code>.
      */
-    public String substringTrimmed(final int from, final int to) {
-        int beginIndex = from;
-        int endIndex = to;
+    public String substringTrimmed(final int beginIndex, final int endIndex) {
         if (beginIndex < 0) {
-            throw new IndexOutOfBoundsException("Negative beginIndex: "+beginIndex);
+            throw new IndexOutOfBoundsException("Negative beginIndex: " + beginIndex);
         }
         if (endIndex > this.len) {
-            throw new IndexOutOfBoundsException("endIndex: "+endIndex+" > length: "+this.len);
+            throw new IndexOutOfBoundsException("endIndex: " + endIndex + " > length:
" + this.len);
         }
         if (beginIndex > endIndex) {
-            throw new IndexOutOfBoundsException("beginIndex: "+beginIndex+" > endIndex:
"+endIndex);
+            throw new IndexOutOfBoundsException("beginIndex: " + beginIndex + " > endIndex:
" + endIndex);
         }
-        while (beginIndex < endIndex && HTTP.isWhitespace(this.buffer[beginIndex]))
{
-            beginIndex++;
+        int beginIndex0 = beginIndex;
+        int endIndex0 = endIndex;
+        while (beginIndex0 < endIndex && HTTP.isWhitespace(this.buffer[beginIndex0]))
{
+            beginIndex0++;
         }
-        while (endIndex > beginIndex && HTTP.isWhitespace(this.buffer[endIndex
- 1])) {
-            endIndex--;
+        while (endIndex0 > beginIndex0 && HTTP.isWhitespace(this.buffer[endIndex0
- 1])) {
+            endIndex0--;
         }
-        return new String(this.buffer, beginIndex, endIndex - beginIndex);
+        return new String(this.buffer, beginIndex0, endIndex0 - beginIndex0);
+    }
+
+    /**
+     * {@inheritDoc}
+     * @since 4.4
+     */
+    @Override
+    public CharSequence subSequence(final int beginIndex, final int endIndex) {
+        if (beginIndex < 0) {
+            throw new IndexOutOfBoundsException("Negative beginIndex: " + beginIndex);
+        }
+        if (endIndex > this.len) {
+            throw new IndexOutOfBoundsException("endIndex: " + endIndex + " > length:
" + this.len);
+        }
+        if (beginIndex > endIndex) {
+            throw new IndexOutOfBoundsException("beginIndex: " + beginIndex + " > endIndex:
" + endIndex);
+        }
+        return CharBuffer.wrap(this.buffer, beginIndex, endIndex);
     }
 
     @Override

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestCharArrayBuffer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestCharArrayBuffer.java?rev=1599241&r1=1599240&r2=1599241&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestCharArrayBuffer.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestCharArrayBuffer.java
Mon Jun  2 15:33:57 2014
@@ -399,4 +399,28 @@ public class TestCharArrayBuffer {
         Assert.assertEquals('c', data[2]);
     }
 
+    @Test
+    public void testSubSequenceIndexOfOutBound() {
+        final CharArrayBuffer buffer = new CharArrayBuffer(16);
+        buffer.append("stuff");
+        try {
+            buffer.subSequence(-2, 10);
+            Assert.fail("IndexOutOfBoundsException should have been thrown");
+        } catch (final IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            buffer.subSequence(12, 10);
+            Assert.fail("IndexOutOfBoundsException should have been thrown");
+        } catch (final IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            buffer.subSequence(2, 1);
+            Assert.fail("IndexOutOfBoundsException should have been thrown");
+        } catch (final IndexOutOfBoundsException ex) {
+            // expected
+        }
+    }
+
 }



Mime
View raw message