KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > util > SegmentBuffer


1 /*
2  * SegmentBuffer.java - A Segment you can append stuff to
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2001 Slava Pestov
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.util;
24
25 import javax.swing.text.Segment JavaDoc;
26
27 /**
28  * An extended segment that you can append text to.
29  */

30 public class SegmentBuffer extends Segment JavaDoc
31 {
32     //{{{ SegmentBuffer constructor
33
public SegmentBuffer(int capacity)
34     {
35         ensureCapacity(capacity);
36     } //}}}
37

38     //{{{ append() method
39
public void append(char ch)
40     {
41         ensureCapacity(count + 1);
42         array[offset + count] = ch;
43         count++;
44     } //}}}
45

46     //{{{ append() method
47
public void append(char[] text, int off, int len)
48     {
49         ensureCapacity(count + len);
50         System.arraycopy(text,off,array,count,len);
51         count += len;
52     } //}}}
53

54     //{{{ Private members
55

56     //{{{ ensureCapacity() method
57
private void ensureCapacity(int capacity)
58     {
59         if(array == null)
60             array = new char[capacity];
61         else if(capacity >= array.length)
62         {
63             char[] arrayN = new char[capacity * 2];
64             System.arraycopy(array,0,arrayN,0,count);
65             array = arrayN;
66         }
67     } //}}}
68

69     //}}}
70
}
71
Popular Tags