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/12/01 21:38:40 UTC
svn commit: r351465 - in /jakarta/httpcomponents/trunk/http-core/src:
java/org/apache/http/impl/io/ java/org/apache/http/io/
java/org/apache/http/util/ test/org/apache/http/io/
Author: olegk
Date: Thu Dec 1 12:38:25 2005
New Revision: 351465
URL: http://svn.apache.org/viewcvs?rev=351465&view=rev
Log:
ByteArrayBuffer and CharArrayBuffer classes no longer publicly expose underlying internal buffers
Modified:
jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/io/AbstractHttpDataReceiver.java
jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/ByteArrayBuffer.java
jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/CharArrayBuffer.java
jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/HeaderParser.java
jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/io/TestByteArrayBuffer.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/impl/io/AbstractHttpDataReceiver.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/io/AbstractHttpDataReceiver.java?rev=351465&r1=351464&r2=351465&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/io/AbstractHttpDataReceiver.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/io/AbstractHttpDataReceiver.java Thu Dec 1 12:38:25 2005
@@ -207,10 +207,13 @@
}
}
}
- copyToCharBuffer(
- this.linebuffer.internBuffer(), 0, this.linebuffer.length(),
- charbuffer);
- return this.linebuffer.length();
+ l = this.linebuffer.length();
+ if (this.ascii) {
+ charbuffer.append(this.linebuffer, 0, l);
+ } else {
+ charbuffer.append(this.linebuffer, 0, l, this.charset);
+ }
+ return l;
}
private int lineFromReadBuffer(final CharArrayBuffer charbuffer, int pos) {
@@ -222,33 +225,14 @@
pos--;
}
len = pos - off;
- copyToCharBuffer(this.buffer, off, len, charbuffer);
- return len;
- }
-
- private void copyToCharBuffer(final byte[] b, int off, int len,
- final CharArrayBuffer charbuffer) {
if (this.ascii) {
- // this is an uuuuugly performance hack
- charbuffer.ensureCapacity(len);
- int oldlen = charbuffer.length();
- int newlen = oldlen + len;
- charbuffer.setLength(newlen);
- char[] tmp = charbuffer.internBuffer();
- for (int i = oldlen; i < newlen; i++) {
- int ch = b[off + i];
- if (ch < 0) {
- ch = 256 + ch;
- }
- tmp[i] = (char) ch;
- }
+ charbuffer.append(this.buffer, off, len);
} else {
- String s = EncodingUtils.getString(b, off, len, this.charset);
- charbuffer.ensureCapacity(s.length());
- charbuffer.append(s);
+ charbuffer.append(this.buffer, off, len, this.charset);
}
+ return len;
}
-
+
public String readLine() throws IOException {
CharArrayBuffer charbuffer = new CharArrayBuffer(64);
int l = readLine(charbuffer);
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=351465&r1=351464&r2=351465&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 1 12:38:25 2005
@@ -36,10 +36,10 @@
*
* @since 4.0
*/
-public class ByteArrayBuffer {
+public final class ByteArrayBuffer {
- private byte[] buffer;
- private int len;
+ protected byte[] buffer;
+ protected int len;
public ByteArrayBuffer(int capacity) {
super();
@@ -76,10 +76,6 @@
public void clear() {
this.len = 0;
- }
-
- public byte[] internBuffer() {
- return this.buffer;
}
public byte[] toByteArray() {
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=351465&r1=351464&r2=351465&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 Thu Dec 1 12:38:25 2005
@@ -29,6 +29,8 @@
package org.apache.http.io;
+import org.apache.http.util.EncodingUtils;
+
/**
* @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
*
@@ -36,10 +38,10 @@
*
* @since 4.0
*/
-public class CharArrayBuffer {
+public final class CharArrayBuffer {
- private char[] buffer;
- private int len;
+ protected char[] buffer;
+ protected int len;
public CharArrayBuffer(int capacity) {
super();
@@ -87,6 +89,20 @@
this.len = newlen;
}
+ 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) {
+ if (b == null) {
+ return;
+ }
+ append(b.buffer,0, b.len);
+ }
+
public void append(char ch) {
int newlen = this.len + 1;
if (newlen > this.buffer.length) {
@@ -96,16 +112,59 @@
this.len = newlen;
}
+ public void append(final byte[] 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++) {
+ int ch = b[i1];
+ if (ch < 0) {
+ ch = 256 + ch;
+ }
+ this.buffer[i2] = (char) ch;
+ }
+ this.len = newlen;
+ }
+
+ public void append(final byte[] b, int off, int len, final String charset) {
+ if (b == null) {
+ return;
+ }
+ append(EncodingUtils.getString(b, off, len, charset));
+ }
+
+ public void append(final ByteArrayBuffer b, int off, int len) {
+ if (b == null) {
+ return;
+ }
+ append(b.buffer, off, len);
+ }
+
+ public void append(final ByteArrayBuffer b, int off, int len, final String charset) {
+ if (b == null) {
+ return;
+ }
+ append(b.buffer, off, len, charset);
+ }
+
public void append(final Object obj) {
append(String.valueOf(obj));
}
public void clear() {
this.len = 0;
- }
-
- public char[] internBuffer() {
- return this.buffer;
}
public char[] toCharArray() {
Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/HeaderParser.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/HeaderParser.java?rev=351465&r1=351464&r2=351465&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/HeaderParser.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/HeaderParser.java Thu Dec 1 12:38:25 2005
@@ -85,7 +85,7 @@
i++;
}
previous.append(' ');
- previous.append(current.internBuffer(), i, current.length() - i);
+ previous.append(current, i, current.length() - i);
} else {
headerLines.add(current);
previous = current;
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=351465&r1=351464&r2=351465&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 1 12:38:25 2005
@@ -59,7 +59,6 @@
ByteArrayBuffer buffer = new ByteArrayBuffer(16);
assertEquals(16, buffer.capacity());
assertEquals(0, buffer.length());
- assertEquals(16, buffer.internBuffer().length);
try {
new ByteArrayBuffer(-1);
fail("IllegalArgumentException should have been thrown");
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=351465&r1=351464&r2=351465&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 Thu Dec 1 12:38:25 2005
@@ -59,7 +59,6 @@
CharArrayBuffer buffer = new CharArrayBuffer(16);
assertEquals(16, buffer.capacity());
assertEquals(0, buffer.length());
- assertEquals(16, buffer.internBuffer().length);
try {
new CharArrayBuffer(-1);
fail("IllegalArgumentException should have been thrown");
@@ -127,10 +126,26 @@
public void testAppendNullString() throws Exception {
CharArrayBuffer buffer = new CharArrayBuffer(8);
- buffer.append(null);
+ buffer.append((String)null);
assertEquals("null", buffer.toString());
}
+ public void testAppendCharArrayBuffer() throws Exception {
+ CharArrayBuffer buffer1 = new CharArrayBuffer(8);
+ buffer1.append(" and more stuff");
+ CharArrayBuffer buffer2 = new CharArrayBuffer(8);
+ buffer2.append("stuff");
+ buffer2.append(buffer1);
+ assertEquals("stuff and more stuff", buffer2.toString());
+ }
+
+ public void testAppendNullCharArrayBuffer() throws Exception {
+ CharArrayBuffer buffer = new CharArrayBuffer(8);
+ buffer.append((CharArrayBuffer)null);
+ buffer.append((CharArrayBuffer)null, 0, 0);
+ assertEquals("", buffer.toString());
+ }
+
public void testAppendSingleChar() throws Exception {
CharArrayBuffer buffer = new CharArrayBuffer(4);
buffer.append('1');
@@ -142,9 +157,9 @@
assertEquals("123456", buffer.toString());
}
- public void testInvalidAppend() throws Exception {
+ public void testInvalidCharArrayAppend() throws Exception {
CharArrayBuffer buffer = new CharArrayBuffer(4);
- buffer.append(null, 0, 0);
+ buffer.append((char[])null, 0, 0);
char[] tmp = new char[] { '1', '2', '3', '4'};
try {
@@ -269,5 +284,112 @@
} 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 testAppendAsciiByteArray() throws Exception {
+ String s1 = "stuff";
+ String s2 = " and more stuff";
+ byte[] b1 = s1.getBytes("US-ASCII");
+ byte[] b2 = s2.getBytes("US-ASCII");
+
+ CharArrayBuffer buffer = new CharArrayBuffer(8);
+ buffer.append(b1, 0, b1.length);
+ buffer.append(b2, 0, b2.length);
+
+ assertEquals("stuff and more stuff", buffer.toString());
+ }
+
+ public void testAppendISOByteArray() throws Exception {
+ String s1 = constructString(SWISS_GERMAN_HELLO);
+ byte[] b1 = s1.getBytes("ISO-8859-1");
+
+ CharArrayBuffer buffer = new CharArrayBuffer(8);
+ buffer.append(b1, 0, b1.length);
+
+ assertEquals(s1, buffer.toString());
+ }
+
+ public void testAppendByteArrayWithCharset() throws Exception {
+ String s1 = constructString(SWISS_GERMAN_HELLO);
+ String s2 = constructString(RUSSIAN_HELLO);
+ byte[] b1 = s1.getBytes("UTF-8");
+ byte[] b2 = s2.getBytes("UTF-8");
+
+ CharArrayBuffer buffer = new CharArrayBuffer(8);
+ buffer.append(b1, 0, b1.length, "UTF-8");
+ buffer.append(b2, 0, b2.length, "UTF-8");
+
+ assertEquals(s1 + s2, buffer.toString());
+ }
+
+ public void testAppendNullByteArray() throws Exception {
+ CharArrayBuffer buffer = new CharArrayBuffer(8);
+ buffer.append((byte[])null, 0, 0);
+ buffer.append((byte[])null, 0, 0, "US-ASCII");
+ assertEquals("", buffer.toString());
}
+
+ public void testAppendNullByteArrayBuffer() throws Exception {
+ CharArrayBuffer buffer = new CharArrayBuffer(8);
+ buffer.append((ByteArrayBuffer)null, 0, 0);
+ buffer.append((ByteArrayBuffer)null, 0, 0, "US-ASCII");
+ assertEquals("", buffer.toString());
+ }
+
+ public void testInvalidAppendAsciiByteArray() throws Exception {
+ CharArrayBuffer buffer = new CharArrayBuffer(4);
+ buffer.append((byte[])null, 0, 0);
+
+ byte[] tmp = new byte[] { '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
+ }
+ }
+
}