Removed unused methods from CharBuffer

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1300491 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Emmanuel Bourg 2012-03-14 10:01:55 +00:00
parent 9141cb39e6
commit 637426c50d
2 changed files with 27 additions and 82 deletions

View File

@ -46,7 +46,7 @@ class CharBuffer {
* Creates a new CharBuffer with an initial capacity
* of <code>length</code> characters.
*/
CharBuffer(final int length) {
CharBuffer(int length) {
if (length == 0) {
throw new IllegalArgumentException("Can't create an empty CharBuffer");
}
@ -84,14 +84,13 @@ class CharBuffer {
*
* @param cb the CharBuffer to append or null
*/
void append(final CharBuffer cb) {
if (cb == null) {
return;
}
provideCapacity(length + cb.length);
void append(CharBuffer cb) {
if (cb != null) {
ensureCapacity(length + cb.length);
System.arraycopy(cb.c, 0, c, length, cb.length);
length += cb.length;
}
}
/**
* Appends <code>s</code> to the end of this CharBuffer.
@ -99,12 +98,11 @@ 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());
}
}
/**
* Appends <code>data</code> to the end of this CharBuffer.
@ -112,14 +110,13 @@ class CharBuffer {
*
* @param data the char[] to append or null
*/
void append(final char[] data) {
if (data == null) {
return;
}
provideCapacity(length + data.length);
void append(char[] data) {
if (data != null) {
ensureCapacity(length + data.length);
System.arraycopy(data, 0, c, length, data.length);
length += data.length;
}
}
/**
* Appends a single character to the end of this CharBuffer.
@ -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;
}
}
}

View File

@ -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);
}
}