diff --git a/src/main/java/org/apache/commons/csv/CharBuffer.java b/src/main/java/org/apache/commons/csv/CharBuffer.java index 4bb24d37..1c5ad8d6 100644 --- a/src/main/java/org/apache/commons/csv/CharBuffer.java +++ b/src/main/java/org/apache/commons/csv/CharBuffer.java @@ -46,7 +46,7 @@ class CharBuffer { * Creates a new CharBuffer with an initial capacity * of length characters. */ - CharBuffer(final int length) { + CharBuffer(int length) { if (length == 0) { throw new IllegalArgumentException("Can't create an empty CharBuffer"); } @@ -84,13 +84,12 @@ class CharBuffer { * * @param cb the CharBuffer to append or null */ - void append(final CharBuffer cb) { - if (cb == null) { - return; + void append(CharBuffer cb) { + if (cb != null) { + ensureCapacity(length + cb.length); + System.arraycopy(cb.c, 0, c, length, cb.length); + length += cb.length; } - provideCapacity(length + cb.length); - System.arraycopy(cb.c, 0, c, length, cb.length); - length += cb.length; } /** @@ -99,11 +98,10 @@ class CharBuffer { * * @param s the String to append or null */ - void append(final String s) { - if (s == null) { - return; + void append(String s) { + if (s != null) { + append(s.toCharArray()); } - append(s.toCharArray()); } /** @@ -112,13 +110,12 @@ class CharBuffer { * * @param data the char[] to append or null */ - void append(final char[] data) { - if (data == null) { - return; + void append(char[] data) { + if (data != null) { + ensureCapacity(length + data.length); + System.arraycopy(data, 0, c, length, data.length); + length += data.length; } - provideCapacity(length + data.length); - System.arraycopy(data, 0, c, length, data.length); - length += data.length; } /** @@ -127,25 +124,12 @@ class CharBuffer { * * @param data the char to append */ - void append(final char data) { - provideCapacity(length + 1); + void append(char data) { + ensureCapacity(length + 1); c[length] = data; length++; } - /** - * Shrinks the capacity of the buffer to the current length if necessary. - * This method involves copying the data once! - */ - void shrink() { - if (c.length == length) { - return; - } - char[] newc = new char[length]; - System.arraycopy(c, 0, newc, 0, length); - c = newc; - } - /** * Removes trailing whitespace. */ @@ -155,31 +139,6 @@ class CharBuffer { } } - /** - * Returns the contents of the buffer as a char[]. The returned array may - * be the internal array of the buffer, so the caller must take care when - * modifying it. - * This method allows to avoid copying if the caller knows the exact capacity - * before. - * - * @return - */ - char[] getCharacters() { - if (c.length == length) { - return c; - } - char[] chars = new char[length]; - System.arraycopy(c, 0, chars, 0, length); - return chars; - } - - /** - * Returns the character at the specified position. - */ - char charAt(int pos) { - return c[pos]; - } - /** * Converts the contents of the buffer into a StringBuffer. * This method involves copying the new data once! @@ -196,13 +155,12 @@ class CharBuffer { * * @param capacity */ - void provideCapacity(final int capacity) { - if (c.length >= capacity) { - return; + void ensureCapacity(int capacity) { + if (c.length < capacity) { + int newcapacity = ((capacity * 3) >> 1) + 1; + char[] newc = new char[newcapacity]; + System.arraycopy(c, 0, newc, 0, length); + c = newc; } - int newcapacity = ((capacity * 3) >> 1) + 1; - char[] newc = new char[newcapacity]; - System.arraycopy(c, 0, newc, 0, length); - c = newc; } } diff --git a/src/test/java/org/apache/commons/csv/CharBufferTest.java b/src/test/java/org/apache/commons/csv/CharBufferTest.java index b9e4b5d3..4c575d7a 100644 --- a/src/test/java/org/apache/commons/csv/CharBufferTest.java +++ b/src/test/java/org/apache/commons/csv/CharBufferTest.java @@ -16,6 +16,7 @@ * specific language governing permissions and limitations * under the License. */ + package org.apache.commons.csv; import junit.framework.TestCase; @@ -86,20 +87,6 @@ public class CharBufferTest extends TestCase { } } - public void testShrink() { - String data = "123456789012345678901234567890"; - - CharBuffer cb = new CharBuffer(data.length() + 100); - assertEquals(data.length() + 100, cb.capacity()); - cb.append(data); - assertEquals(data.length() + 100, cb.capacity()); - assertEquals(data.length(), cb.length()); - cb.shrink(); - assertEquals(data.length(), cb.capacity()); - assertEquals(data.length(), cb.length()); - assertEquals(data, cb.toString()); - } - //-- the following test cases have been adapted from the HttpComponents project //-- written by Oleg Kalnichevski @@ -107,7 +94,7 @@ public class CharBufferTest extends TestCase { CharBuffer buffer = new CharBuffer(16); assertEquals(16, buffer.capacity()); assertEquals(0, buffer.length()); - char[] b1 = buffer.getCharacters(); + char[] b1 = buffer.toString().toCharArray(); assertNotNull(b1); assertEquals(0, b1.length); assertEquals(0, buffer.length()); @@ -117,7 +104,7 @@ public class CharBufferTest extends TestCase { assertEquals(16, buffer.capacity()); assertEquals(4, buffer.length()); - char[] b2 = buffer.getCharacters(); + char[] b2 = buffer.toString().toCharArray(); assertNotNull(b2); assertEquals(4, b2.length); for (int i = 0; i < tmp.length; i++) { @@ -172,9 +159,9 @@ public class CharBufferTest extends TestCase { public void testProvideCapacity() throws Exception { CharBuffer buffer = new CharBuffer(4); - buffer.provideCapacity(2); + buffer.ensureCapacity(2); assertEquals(4, buffer.capacity()); - buffer.provideCapacity(8); + buffer.ensureCapacity(8); assertTrue(buffer.capacity() >= 8); } }