KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > util > CharBuffer


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.util;
30
31 import java.io.InputStream JavaDoc;
32
33 /**
34  * CharBuffer is an unsynchronized version of StringBuffer.
35  */

36 public final class CharBuffer extends CharSegment {
37   private static final int MIN_CAPACITY = 64;
38
39   /**
40    * Constructs a char buffer with no characters.
41    */

42   public CharBuffer()
43   {
44     _buffer = new char[MIN_CAPACITY];
45     _length = 0;
46   }
47
48   /**
49    * Constructs a char buffer with the given initial capacity
50    *
51    * @param capacity initial capacity
52    */

53   public CharBuffer(int capacity)
54   {
55     if (capacity < 0)
56       throw new IllegalArgumentException JavaDoc();
57     if (capacity < MIN_CAPACITY)
58       capacity = MIN_CAPACITY;
59
60     _buffer = new char[capacity];
61     _length = 0;
62   }
63
64   /**
65    * Constructs a char buffer with the given initial string
66    *
67    * @param string initial string
68    */

69   public CharBuffer(String JavaDoc string)
70   {
71     int length = string.length();
72     int capacity = length + MIN_CAPACITY;
73
74     _buffer = new char[capacity];
75     _length = length;
76     string.getChars(0, length, _buffer, 0);
77   }
78
79   /**
80    * Constructs a char buffer with the given initial string
81    *
82    * @param string initial string
83    */

84   public CharBuffer(String JavaDoc string, int offset, int length)
85   {
86     int capacity = length;
87     if (capacity < MIN_CAPACITY)
88       capacity = MIN_CAPACITY;
89
90     _buffer = new char[capacity];
91     _length = length;
92     string.getChars(offset, length, _buffer, 0);
93   }
94
95   public static CharBuffer allocate()
96   {
97     return new CharBuffer();
98   }
99
100   public void free()
101   {
102   }
103
104   /**
105    * Returns the character count of the buffer's contents.
106    */

107   public int length()
108   {
109     return _length;
110   }
111
112   /**
113    * Returns the buffer length
114    */

115   public int getLength()
116   {
117     return _length;
118   }
119
120   /**
121    * Returns the capacity of the buffer, i.e. how many chars it
122    * can hold.
123    */

124   public int capacity()
125   {
126     return _buffer.length;
127   }
128   
129   public int getCapacity()
130   {
131     return _buffer.length;
132   }
133
134   /**
135    * Ensure the buffer can hold at least 'minimumCapacity' chars.
136    */

137   public final void ensureCapacity(int minimumCapacity)
138   {
139     if (minimumCapacity <= _buffer.length) {
140       return;
141     }
142
143     expandCapacity(minimumCapacity);
144   }
145
146   /**
147    * Expands the capacity to a new value.
148    */

149   private final void expandCapacity(int minimumCapacity)
150   {
151     int oldCapacity = _buffer.length;
152     int newCapacity = oldCapacity * 2;
153
154     if (newCapacity < 0)
155       newCapacity = Integer.MAX_VALUE;
156     else if (newCapacity < minimumCapacity)
157       newCapacity = minimumCapacity;
158
159     char []chars = new char[newCapacity];
160     
161     System.arraycopy(_buffer, 0, chars, 0, oldCapacity);
162
163     _buffer = chars;
164   }
165
166   /**
167    * Clears the buffer. Equivalent to setLength(0)
168    */

169   public final void clear()
170   {
171     _length = 0;
172   }
173
174   /**
175    * Set the length of the buffer.
176    */

177   public final void setLength(int newLength)
178   {
179     if (newLength < 0)
180       throw new IndexOutOfBoundsException JavaDoc("illegal argument");
181     else if (_buffer.length < newLength)
182       expandCapacity(newLength);
183
184     _length = newLength;
185   }
186
187   /**
188    * Returns the char at the specified offset.
189    */

190   public char charAt(int i)
191   {
192     if (i < 0 || _length <= i)
193       throw new IndexOutOfBoundsException JavaDoc();
194
195     return _buffer[i];
196   }
197
198   /**
199    * Returns the last character of the buffer
200    *
201    * @throws IndexOutOfBoundsException for an empty buffer
202    */

203   public char getLastChar()
204   {
205     if (_length == 0)
206       throw new IndexOutOfBoundsException JavaDoc();
207
208     return _buffer[_length - 1];
209   }
210   
211   /**
212    * Returns the buffer's char array.
213    */

214   public final char []getBuffer()
215   {
216     return _buffer;
217   }
218
219   /**
220    * Copies characters to the destination buffer.
221    */

222   public void getChars(int srcBegin, int srcEnd, char []dst, int dstBegin)
223   {
224     char []buffer = _buffer;
225     while (srcBegin < srcEnd)
226       dst[dstBegin++] = buffer[srcBegin++];
227   }
228
229   /**
230    * Sets the character at the given index.
231    */

232   public void setCharAt(int index, char ch)
233   {
234     if (index < 0 || _length <= index)
235       throw new IndexOutOfBoundsException JavaDoc();
236
237     _buffer[index] = ch;
238   }
239
240   /**
241    * Appends the string representation of the object to the buffer.
242    */

243   public CharBuffer append(Object JavaDoc obj)
244   {
245     return append(String.valueOf(obj));
246   }
247
248   /**
249    * Appends the string representation of the object to the buffer.
250    */

251   public CharBuffer append(CharBuffer cb)
252   {
253     return append(cb._buffer, 0, cb._length);
254   }
255
256   /**
257    * Appends the string.
258    */

259   public CharBuffer append(String JavaDoc string)
260   {
261     if (string == null)
262       string = "null";
263
264     int len = string.length();
265     int newLength = _length + len;
266     int length = _length;
267     if (_buffer.length <= newLength)
268       expandCapacity(newLength);
269
270     string.getChars(0, len, _buffer, length);
271
272     _length = newLength;
273
274     return this;
275   }
276
277   public CharBuffer append(String JavaDoc string, int offset, int len)
278   {
279     if (_buffer.length <= len + _length)
280       expandCapacity(len + _length);
281
282     string.getChars(offset, offset + len, _buffer, _length);
283
284     _length += len;
285
286     return this;
287   }
288   
289   /**
290    * Appends the characters to the buffer.
291    */

292   public CharBuffer append(char []buffer)
293   {
294     return append(buffer, 0, buffer.length);
295   }
296
297   /**
298    * Appends the characters to the buffer.
299    */

300   public CharBuffer append(char []buffer, int offset, int length)
301   {
302     if (_buffer.length < _length + length)
303       expandCapacity(_length + length);
304
305     System.arraycopy(buffer, offset, _buffer, _length, length);
306
307     _length += length;
308
309     return this;
310   }
311
312   /**
313    * Appends the boolean representation to the buffer
314    */

315   public final CharBuffer append(boolean b)
316   {
317     return append(String.valueOf(b));
318   }
319   
320   /**
321    * Appends the character to the buffer
322    */

323   public final CharBuffer append(char ch)
324   {
325     if (_buffer.length <= _length)
326       expandCapacity(_length + 1);
327
328     _buffer[_length++] = ch;
329
330     return this;
331   }
332   
333   /**
334    * Add an int to the buffer.
335    */

336   public CharBuffer append(int i)
337   {
338     if (i == 0x80000000) {
339       return append("-2147483648");
340     }
341     
342     int length = _length;
343     
344     if (_buffer.length <= length + 16)
345       expandCapacity(length + 16);
346
347     char []buffer = _buffer;
348
349     if (i < 0) {
350       buffer[length++] = '-';
351       i = -i;
352     }
353     else if (i == 0) {
354       buffer[_length++] = '0';
355       return this;
356     }
357
358     int start = length;
359     while (i > 0) {
360       buffer[length++] = (char) ((i % 10) + '0');
361       i /= 10;
362     }
363
364     for (int j = (length - start) / 2; j > 0; j--) {
365       char temp = buffer[length - j];
366       buffer[length - j] = buffer[start + j - 1];
367       buffer[start + j - 1] = temp;
368     }
369
370     _length = length;
371
372     return this;
373   }
374   
375   /**
376    * Add a long to the buffer.
377    */

378   public CharBuffer append(long i)
379   {
380     if (i == 0x8000000000000000L) {
381       return append("-9223372036854775808");
382     }
383     
384     int length = _length;
385     
386     if (_buffer.length < length + 32)
387       expandCapacity(length + 32);
388
389     char []buffer = _buffer;
390
391     if (i < 0) {
392       buffer[length++] = '-';
393       i = -i;
394     }
395     else if (i == 0) {
396       buffer[_length++] = '0';
397       return this;
398     }
399
400     int start = length;
401     while (i > 0) {
402       buffer[length++] = (char) ((i % 10) + '0');
403       i /= 10;
404     }
405
406     for (int j = (length - start) / 2; j > 0; j--) {
407       char temp = buffer[length - j];
408       buffer[length - j] = buffer[start + j - 1];
409       buffer[start + j - 1] = temp;
410     }
411
412     _length = length;
413
414     return this;
415   }
416
417   /**
418    * Add a float to the buffer.
419    */

420   public CharBuffer append(float f)
421   {
422     return append(String.valueOf(f));
423   }
424
425   /**
426    * Add a double to the buffer.
427    */

428   public CharBuffer append(double d)
429   {
430     return append(String.valueOf(d));
431   }
432   
433   /**
434    * Appends iso-8859-1 bytes to the buffer
435    */

436   public final CharBuffer append(byte []buf, int offset, int len)
437   {
438     int length = _length;
439     if (_buffer.length < _length + len)
440       expandCapacity(_length + len);
441
442     char []buffer = _buffer;
443     for (; len > 0; len--)
444       buffer[length++] = (char) buf[offset++];
445
446     _length = length;
447
448     return this;
449   }
450
451   /**
452    * Deletes characters from the buffer.
453    */

454   public CharBuffer delete(int start, int end)
455   {
456     if (start < 0 || end < start || _length < start)
457       throw new StringIndexOutOfBoundsException JavaDoc();
458
459     if (_length < end)
460       end = _length;
461     
462     int tail = _length - end;
463     char []buffer = _buffer;
464     
465     for (int i = 0; i < tail; i++)
466       buffer[start + i] = buffer[end + i];
467
468     _length -= end - start;
469
470     return this;
471   }
472
473   /**
474    * Deletes a character from the buffer.
475    */

476   public CharBuffer deleteCharAt(int index)
477   {
478     if (index < 0 || _length < index)
479       throw new StringIndexOutOfBoundsException JavaDoc();
480
481     if (index == _length)
482       return this;
483     
484     int tail = _length - index + 1;
485     char []buffer = _buffer;
486
487     for (int i = 0; i < tail; i++)
488       buffer[index + i] = buffer[index + i + 1];
489
490     _length--;
491
492     return this;
493   }
494
495   /**
496    * Replaces a range with a string
497    */

498   public CharBuffer replace(int start, int end, String JavaDoc string)
499   {
500     if (start < 0 || end < start || _length < start)
501       throw new StringIndexOutOfBoundsException JavaDoc();
502
503     int len = string.length();
504     int length = _length;
505
506     if (_buffer.length < len + length - (end - start))
507       expandCapacity(len + length - (end - start));
508
509     char []buffer = _buffer;
510
511     if (len < end - start) {
512       int tail = length - end;
513       for (int i = 0; i < tail; i++)
514     buffer[start + len + i] = buffer[end + i];
515     }
516     else {
517       int tail = length - end;
518       for (int i = tail - 1; i >= 0; i--)
519     buffer[end + i] = buffer[start + len + i];
520     }
521
522     string.getChars(0, len, buffer, start);
523
524     _length = length + len - (end - start);
525
526     return this;
527   }
528
529   /**
530    * Replaces a range with a character array
531    */

532   public CharBuffer replace(int start, int end,
533                 char []buffer, int offset, int len)
534   {
535     if (start < 0 || end < start || _length < start)
536       throw new StringIndexOutOfBoundsException JavaDoc();
537
538     if (_buffer.length < len + _length - (end - start))
539       expandCapacity(len + _length - (end - start));
540
541     char []thisBuffer = _buffer;
542
543     if (len < end - start) {
544       int tail = _length - end;
545       for (int i = 0; i < tail; i++)
546     thisBuffer[start + len + i] = thisBuffer[end + i];
547     }
548     else {
549       int tail = _length - end;
550       for (int i = tail - 1; i >= 0; i--)
551     thisBuffer[end + i] = thisBuffer[start + len + i];
552     }
553
554     System.arraycopy(buffer, offset, thisBuffer, start, len);
555
556     _length += len - (end - start);
557
558     return this;
559   }
560
561   /**
562    * Returns a substring
563    */

564   public String JavaDoc substring(int start)
565   {
566     if (_length < start || start < 0)
567       throw new StringIndexOutOfBoundsException JavaDoc();
568
569     return new String JavaDoc(_buffer, start, _length - start);
570   }
571
572   /**
573    * Returns a substring
574    */

575   public String JavaDoc substring(int start, int end)
576   {
577     if (_length < start || start < 0 || end < start)
578       throw new StringIndexOutOfBoundsException JavaDoc();
579
580     return new String JavaDoc(_buffer, start, end - start);
581   }
582   /**
583    * Inserts a string.
584    */

585   public CharBuffer insert(int index, String JavaDoc string)
586   {
587     if (string == null)
588       string = "null";
589
590     if (index < 0 || _length < index)
591       throw new StringIndexOutOfBoundsException JavaDoc();
592
593     int len = string.length();
594
595     if (_buffer.length < _length + len)
596       expandCapacity(len + _length);
597
598     int tail = _length - index;
599     char []buffer = _buffer;
600     
601     for (int i = tail - 1; i >= 0; i--)
602       buffer[index + len + i] = buffer[index + i];
603
604     string.getChars(0, len, buffer, index);
605     _length += len;
606
607     return this;
608   }
609
610   /**
611    * Inserts a character buffer.
612    */

613   public CharBuffer insert(int index, char []buffer, int offset, int len)
614   {
615     if (index < 0 || _length < index)
616       throw new StringIndexOutOfBoundsException JavaDoc();
617
618     if (_buffer.length < len + _length)
619       expandCapacity(len + _length);
620
621     int tail = _length - index;
622     char []thisBuffer = _buffer;
623     for (int i = tail - 1; i >= 0; i--)
624       buffer[index + len + i] = thisBuffer[index + i];
625
626     System.arraycopy(buffer, offset, thisBuffer, index, len);
627     _length += len;
628
629     return this;
630   }
631
632   /**
633    * Inserts an object at a given offset.
634    */

635   public CharBuffer insert(int offset, Object JavaDoc o)
636   {
637     return insert(offset, String.valueOf(o));
638   }
639
640   /**
641    * Inserts a character at a given offset.
642    */

643   public CharBuffer insert(int offset, char ch)
644   {
645     return insert(offset, String.valueOf(ch));
646   }
647
648   /**
649    * Inserts an integer at a given offset.
650    */

651   public CharBuffer insert(int offset, int i)
652   {
653     return insert(offset, String.valueOf(i));
654   }
655
656   /**
657    * Inserts a long at a given offset.
658    */

659   public CharBuffer insert(int offset, long l)
660   {
661     return insert(offset, String.valueOf(l));
662   }
663
664   /**
665    * Inserts a float at a given offset.
666    */

667   public CharBuffer insert(int offset, float f)
668   {
669     return insert(offset, String.valueOf(f));
670   }
671
672   /**
673    * Inserts a double at a given offset.
674    */

675   public CharBuffer insert(int offset, double d)
676   {
677     return insert(offset, String.valueOf(d));
678   }
679
680   public int indexOf(char ch)
681   {
682     return indexOf(ch, 0);
683   }
684
685   /**
686    * Clones the buffer
687    */

688   public Object JavaDoc clone()
689   {
690     CharBuffer newBuffer = new CharBuffer();
691
692     newBuffer.setLength(_length);
693
694     System.arraycopy(_buffer, 0, newBuffer._buffer, 0, _length);
695
696     return newBuffer;
697   }
698
699   /**
700    * String representation of the buffer.
701    */

702   public String JavaDoc toString()
703   {
704     return new String JavaDoc(_buffer, 0, _length);
705   }
706
707   public String JavaDoc close()
708   {
709     String JavaDoc string = new String JavaDoc(_buffer, 0, _length);
710     free();
711     return string;
712   }
713
714   class CBInputStream extends InputStream JavaDoc {
715     int _index = 0;
716
717     public int read()
718     {
719       if (_length <= _index)
720     return -1;
721
722       return _buffer[_index++];
723     }
724   }
725
726   public InputStream JavaDoc getInputStream()
727   {
728     return new CBInputStream();
729   }
730 }
731
Popular Tags