hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r355255 - in /jakarta/httpcomponents/trunk/http-core/src: java/org/apache/http/io/ByteArrayBuffer.java test/org/apache/http/io/TestByteArrayBuffer.java
Date Thu, 08 Dec 2005 22:36:52 GMT
Author: olegk
Date: Thu Dec  8 14:36:44 2005
New Revision: 355255

URL: http://svn.apache.org/viewcvs?rev=355255&view=rev
Log:
Added various ByteArrayBuffer#append() methods to append array of chars

Modified:
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/ByteArrayBuffer.java
    jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/io/TestByteArrayBuffer.java

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=355255&r1=355254&r2=355255&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
Thu Dec  8 14:36:44 2005
@@ -29,6 +29,8 @@
 
 package org.apache.http.io;
 
+import org.apache.http.util.EncodingUtils;
+
 /**
  * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
  * 
@@ -83,6 +85,50 @@
         this.len = newlen;
     }
 
+    public void append(final char[] b, int off, int len) {
+        if (b == null) {
+            return;
+        }
+        if ((off < 0) || (off > b.length) || (len < 0) ||
+                ((off + len) < 0) || ((off + len) > b.length)) {
+            throw new IndexOutOfBoundsException();
+        }
+        if (len == 0) {
+            return;
+        }
+        int oldlen = this.len;
+        int newlen = oldlen + len;
+        if (newlen > this.buffer.length) {
+            expand(newlen);
+        }
+        for (int i1 = off, i2 = oldlen; i2 < newlen; i1++, i2++) {
+            this.buffer[i2] = (byte) b[i1];
+        }
+        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;
+        }
+        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;
     }

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=355255&r1=355254&r2=355255&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
Thu Dec  8 14:36:44 2005
@@ -117,7 +117,7 @@
     
     public void testInvalidAppend() throws Exception {
     	ByteArrayBuffer buffer = new ByteArrayBuffer(4);
-    	buffer.append(null, 0, 0);
+    	buffer.append((byte[])null, 0, 0);
 
     	byte[] tmp = new byte[] { 1, 2, 3, 4};
     	try {
@@ -188,6 +188,119 @@
     	} catch (IndexOutOfBoundsException ex) {
     		// expected
     	}
+    }
+    
+    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";
+        char[] b1 = s1.toCharArray();
+        char[] b2 = s2.toCharArray();
+        
+        ByteArrayBuffer buffer = new ByteArrayBuffer(8);
+        buffer.append(b1, 0, b1.length);
+        buffer.append(b2, 0, b2.length);
+         
+        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());
+    }
+
+    public void testInvalidAppendCharArrayAsAscii() throws Exception {
+        ByteArrayBuffer buffer = new ByteArrayBuffer(4);
+        buffer.append((char[])null, 0, 0);
+
+        char[] tmp = new char[] { '1', '2', '3', '4'};
+        try {
+            buffer.append(tmp, -1, 0);
+            fail("IndexOutOfBoundsException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            buffer.append(tmp, 0, -1);
+            fail("IndexOutOfBoundsException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            buffer.append(tmp, 0, 8);
+            fail("IndexOutOfBoundsException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            buffer.append(tmp, 10, Integer.MAX_VALUE);
+            fail("IndexOutOfBoundsException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            buffer.append(tmp, 2, 4);
+            fail("IndexOutOfBoundsException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
     }
     
 }



Mime
View raw message