8000 Removed unused methods from CharBuffer · pkdevbox/commons-csv@637426c · GitHub
Skip to content

Commit 637426c

Browse files
committed
Removed unused methods from CharBuffer
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1300491 13f79535-47bb-0310-9956-ffa450edef68
1 parent 9141cb3 commit 637426c

2 files changed

Lines changed: 27 additions & 82 deletions

File tree

src/main/java/org/apache/commons/csv/CharBuffer.java

Lines changed: 22 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class CharBuffer {
4646
* Creates a new CharBuffer with an initial capacity
4747
* of <code>length</code> characters.
4848
*/
49-
CharBuffer(final int length) {
49+
CharBuffer(int length) {
5050
if (length == 0) {
5151
throw new IllegalArgumentException("Can't create an empty CharBuffer");
5252
}
@@ -84,13 +84,12 @@ int capacity() {
8484
*
8585
* @param cb the CharBuffer to append or null
8686
*/
87-
void append(final CharBuffer cb) {
88-
if (cb == null) {
89-
return;
87+
void append(CharBuffer cb) {
88+
if (cb != null) {
89+
ensureCapacity(length + cb.length);
90+
System.arraycopy(cb.c, 0, c, length, cb.length);
91+
length += cb.length;
9092
}
91-
provideCapacity(length + cb.length);
92-
System.arraycopy(cb.c, 0, c, length, cb.length);
93-
length += cb.length;
9493
}
9594

9695
/**
@@ -99,11 +98,10 @@ void append(final CharBuffer cb) {
9998
*
10099
* @param s the String to append or null
101100
*/
102-
void append(final String s) {
103-
if (s == null) {
104-
return;
101+
void append(String s) {
102+
if (s != null) {
103+
append(s.toCharArray());
105104
}
106-
append(s.toCharArray());
107105
}
108106

109107
/**
@@ -112,13 +110,12 @@ void append(final String s) {
112110
*
113111
* @param data the char[] to append or null
114112
*/
115-
void append(final char[] data) {
116-
if (data == null) {
117-
return;
113+
void append(char[] data) {
114+
if (data != null) {
115+
ensureCapacity(length + data.length);
116+
System.arraycopy(data, 0, c, length, data.length);
117+
length += data.length;
118118
}
119-
provideCapacity(length + data.length);
120-
System.arraycopy(data, 0, c, length, data.length);
121-
length += data.length;
122119
}
123120

124121
/**
@@ -127,25 +124,12 @@ void append(final char[] data) {
127124
*
128125
* @param data the char to append
129126
*/
130-
void append(final char data) {
131-
provideCapacity(length + 1);
127+
void append(char data) {
128+
ensureCapacity(length + 1);
132129
c[length] = data;
133130
length++;
134131
}
135132

136-
/**
137-
* Shrinks the capacity of the buffer to the current length if necessary.
138-
* This method involves copying the data once!
139-
*/
140-
void shrink() {
141-
if (c.length == length) {
142-
return;
143-
}
144-
char[] newc = new char[length];
145-
System.arraycopy(c, 0, newc, 0, length);
146-
c = newc;
147-
}
148-
149133
/**
150134
* Removes trailing whitespace.
151135
*/
@@ -155,31 +139,6 @@ void trimTrailingWhitespace() {
155139
}
156140
}
157141

158-
/**
159-
* Returns the contents of the buffer as a char[]. The returned array may
160-
* be the internal array of the buffer, so the caller must take care when
161-
* modifying it.
162-
* This method allows to avoid copying if the caller knows the exact capacity
163-
* before.
164-
*
165-
* @return
166-
*/
167-
char[] getCharacters() {
168-
if (c.length == length) {
169-
return c;
170-
}
171-
char[] chars = new char[length];
172-
System.arraycopy(c, 0, chars, 0, length);
173-
return chars;
174-
}
175-
176-
/**
177-
* Returns the character at the specified position.
178-
*/
179-
char charAt(int pos) {
180-
return c[pos];
181-
}
182-
183142
/**
184143
* Converts the contents of the buffer into a StringBuffer.
185144
* This method involves copying the new data once!
@@ -196,13 +155,12 @@ public String toString() {
196155
*
197156
* @param capacity
198157
*/
199-
void provideCapacity(final int capacity) {
200-
if (c.length >= capacity) {
201-
return;
158+
void ensureCapacity(int capacity) {
159+
if (c.length < capacity) {
160+
int newcapacity = ((capacity * 3) >> 1) + 1;
161+
char[] newc = new char[newcapacity];
162+
System.arraycopy(c, 0, newc, 0, length);
163+
c = newc;
202164
}
203-
int newcapacity = ((capacity * 3) >> 1) + 1;
204-
char[] newc = new char[newcapacity];
205-
System.arraycopy(c, 0, newc, 0, length);
206-
c = newc;
207165
}
208166
}

src/test/java/org/apache/commons/csv/CharBufferTest.java

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19+
1920
package org.apache.commons.csv;
2021

2122
import junit.framework.TestCase;
@@ -86,28 +87,14 @@ public void testAppendCharBuffer() {
8687
}
8788
}
8889

89-
public void testShrink() {
90-
String data = "123456789012345678901234567890";
91-
92-
CharBuffer cb = new CharBuffer(data.length() + 100);
93-
assertEquals(data.length() + 100, cb.capacity());
94-
cb.append(data);
95-
assertEquals(data.length() + 100, cb.capacity());
96-
assertEquals(data.length(), cb.length());
97-
cb.shrink();
98-
assertEquals(data.length(), cb.capacity());
99-
assertEquals(data.length(), cb.length());
100-
assertEquals(data, cb.toString());
101-
}
102-
10390
//-- the following test cases have been adapted from the HttpComponents project
10491
//-- written by Oleg Kalnichevski
10592

10693
public void testSimpleAppend() throws Exception {
10794
CharBuffer buffer = new CharBuffer(16);
10895
assertEquals(16, buffer.capacity());
10996
assertEquals(0, buffer.length());
110-
char[] b1 = buffer.getCharacters();
97+
char[] b1 = buffer.toString().toCharArray();
11198
assertNotNull(b1);
11299
assertEquals(0, b1.length);
113100
assertEquals(0, buffer.length());
@@ -117,7 +104,7 @@ public void testSimpleAppend() throws Exception {
117104
assertEquals(16, buffer.capacity());
118105
assertEquals(4, buffer.length());
119106

120-
char[] b2 = buffer.getCharacters();
107+
char[] b2 = buffer.toString().toCharArray();
121108
assertNotNull(b2);
122109
assertEquals(4, b2.length);
123110
for (int i = 0; i < tmp.length; i++) {
@@ -172,9 +159,9 @@ public void testAppendSingleChar() throws Exception {
172159

173160
public void testProvideCapacity() throws Exception {
174161
CharBuffer buffer = new CharBuffer(4);
175-
buffer.provideCapacity(2);
162+
buffer.ensureCapacity(2);
176163
assertEquals(4, buffer.capacity());
177-
buffer.provideCapacity(8);
164+
buffer.ensureCapacity(8);
178165
assertTrue(buffer.capacity() >= 8);
179166
}
180167
}

0 commit comments

Comments
 (0)