01: /*
02: * SegmentBuffer.java - A Segment you can append stuff to
03: * :tabSize=8:indentSize=8:noTabs=false:
04: * :folding=explicit:collapseFolds=1:
05: *
06: * Copyright (C) 2001 Slava Pestov
07: *
08: * This program is free software; you can redistribute it and/or
09: * 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;
26:
27: /**
28: * An extended segment that you can append text to.
29: */
30: public class SegmentBuffer extends Segment {
31: //{{{ SegmentBuffer constructor
32: public SegmentBuffer(int capacity) {
33: ensureCapacity(capacity);
34: } //}}}
35:
36: //{{{ append() method
37: public void append(char ch) {
38: ensureCapacity(count + 1);
39: array[offset + count] = ch;
40: count++;
41: } //}}}
42:
43: //{{{ append() method
44: public void append(char[] text, int off, int len) {
45: ensureCapacity(count + len);
46: System.arraycopy(text, off, array, count, len);
47: count += len;
48: } //}}}
49:
50: //{{{ Private members
51:
52: //{{{ ensureCapacity() method
53: private void ensureCapacity(int capacity) {
54: if (array == null)
55: array = new char[capacity];
56: else if (capacity >= array.length) {
57: char[] arrayN = new char[capacity * 2];
58: System.arraycopy(array, 0, arrayN, 0, count);
59: array = arrayN;
60: }
61: } //}}}
62:
63: //}}}
64: }
|