@@ -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}
0 commit comments