KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > lists > CharBuffer


1 // Copyright (c) 2001, 2003, 2005 Per M.A. Bothner and Brainfood Inc.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.lists;
5
6 /** Editable character sequence using a a buffer-gap implementstion and
7  * self-adjusting position.
8  * Can implement (the text part of) an Emacs buffer, or a
9  * javax.swing.text.AbstractDocument.Content
10  */

11
12 public class CharBuffer extends StableVector
13   implements CharSeq, java.io.Serializable JavaDoc
14 {
15   // Same as super.base but pre-cast to FString.
16
private FString string;
17
18   public CharBuffer(FString str)
19   {
20     super(str);
21     string = str;
22   }
23
24   public CharBuffer(int initialSize)
25   {
26     this(new FString(initialSize));
27   }
28
29   protected CharBuffer ()
30   {
31   }
32
33   public int length() { return size(); }
34
35   public char charAt(int index)
36   {
37     // If index is out of bounds, the base.get will catch that.
38
if (index >= gapStart)
39       index += gapEnd - gapStart;
40     return string.charAt(index);
41   }
42
43   /** Copy characters into a destination buffer.
44    * Same interface as java.lang.String's getChars. */

45   public void getChars (int srcBegin, int srcEnd, char[] dst, int dstBegin)
46   {
47     char[] array = string.data;
48     int count;
49     if (srcBegin < gapStart)
50       {
51     count = (srcEnd < gapStart ? srcEnd : gapStart) - srcBegin;
52     if (count > 0)
53       {
54         System.arraycopy(array, srcBegin, dst, dstBegin, count);
55         srcBegin += count;
56         dstBegin += count;
57       }
58       }
59     int gapSize = gapEnd - gapStart;
60     srcBegin += gapSize;
61     srcEnd += gapSize;
62     count = srcEnd - srcBegin;
63     if (count > 0)
64       System.arraycopy(array, srcBegin, dst, dstBegin, count);
65   }
66
67   public void setCharAt(int index, char value)
68   {
69     // If index is out of bounds, the base.get will catch that.
70
if (index >= gapStart)
71       index += gapEnd - gapStart;
72     string.setCharAt(index, value);
73   }
74
75   /* #ifdef use:java.lang.CharSequence */
76   public CharSequence JavaDoc subSequence(int start, int end)
77   {
78     int sz = size();
79     if (start < 0 || end < start || end > sz)
80       throw new IndexOutOfBoundsException JavaDoc();
81     return new SubCharSeq(this,
82                           base.createPos(start, false),
83                           base.createPos(end, true));
84   }
85   /* #endif */
86
87   public void fill(int fromIndex, int toIndex, char value)
88   {
89     char[] array = string.data;
90     int i = fromIndex;
91     int limit = gapStart < toIndex ? gapStart : toIndex;
92     for (; i < limit; i++)
93       array[i] = value;
94     int gapSize = gapEnd - gapStart;
95     i = limit + gapSize;
96     limit += toIndex;
97     for (; i < limit; i++)
98       array[i] = value;
99   }
100
101   /** Set all the elements to a given character. */
102   public final void fill (char value)
103   {
104     char[] array = string.data;
105     for (int i = array.length; --i >= gapEnd; )
106       array[i] = value;
107     for (int i = gapStart; --i >= 0; )
108       array[i] = value;
109   }
110
111   public char[] getArray() { return (char[]) base.getBuffer(); }
112
113   public void delete(int where, int count)
114   {
115     int ipos = createPos(where, false);
116     removePos(ipos, count);
117     releasePos(ipos);
118   }
119
120   public void insert(int where, String JavaDoc str, boolean beforeMarkers/*ignored*/)
121   {
122     int len = str.length();
123     gapReserve(where, len);
124     str.getChars(0, len, string.data, where);
125     gapStart += len;
126   }
127
128   public void consume(int start, int count, Consumer dest)
129   {
130     char[] array = string.data;
131     if (start < gapStart)
132       {
133     int count0 = gapStart - start;
134     if (count0 > count)
135       count0 = count;
136     dest.write(array, start, count0);
137     count -= count0;
138     start += count;
139       }
140     if (count > 0)
141       {
142     start += gapEnd - gapStart;
143     dest.write(array, start, count);
144       }
145   }
146
147   public String JavaDoc toString()
148   {
149     int len = size();
150     int start = getSegment(0, len);
151     return new String JavaDoc(getArray(), start, len);
152   }
153
154   /* #ifdef JAVA5 */
155   // public void writeTo(int start, int count, Appendable dest)
156
// throws java.io.IOException
157
// {
158
// if (dest instanceof java.io.Writer)
159
// writeTo(start, count, (java.io.Writer) dest);
160
// else
161
// dest.append(this, start, start+count);
162
// }
163

164   // public void writeTo(Appendable dest)
165
// throws java.io.IOException
166
// {
167
// writeTo(0, size(), dest);
168
// }
169
/* #endif */
170
171   public void writeTo(int start, int count, java.io.Writer JavaDoc dest)
172     throws java.io.IOException JavaDoc
173   {
174     char[] array = string.data;
175     if (start < gapStart)
176       {
177     int count0 = gapStart - start;
178     if (count0 > count)
179       count0 = count;
180     dest.write(array, start, count0);
181     count -= count0;
182     start += count;
183       }
184     if (count > 0)
185       {
186     start += gapEnd - gapStart;
187     dest.write(array, start, count);
188       }
189   }
190
191   public void writeTo(java.io.Writer JavaDoc dest) throws java.io.IOException JavaDoc
192   {
193     char[] array = string.data;
194     dest.write(array, 0, gapStart);
195     dest.write(array, gapEnd, array.length - gapEnd);
196   }
197
198   public void dump()
199   {
200     System.err.println("Buffer Content dump. size:"+size()+" buffer:"+getArray().length);
201     System.err.print("before gap: \"");
202     System.err.print(new String JavaDoc(getArray(), 0, gapStart));
203     System.err.println("\" (gapStart:"+gapStart+" gapEnd:"+gapEnd+')');
204     System.err.print("after gap: \"");
205     System.err.print(new String JavaDoc(getArray(), gapEnd, getArray().length-gapEnd));
206     System.err.println("\"");
207     int poslen = positions == null ? 0 : positions.length;
208     System.err.println("Positions (size: "+poslen+" free:"+free+"):");
209     boolean[] isFree = null;
210     if (free != -2)
211       {
212         isFree = new boolean[positions.length];
213         for (int i = free; i >= 0; i = positions[i])
214           isFree[i] = true;
215       }
216     for (int i = 0; i < poslen; i++)
217       {
218     int pos = positions[i];
219     if (free == -2 ? pos != FREE_POSITION : ! isFree[i])
220       System.err.println("position#"+i+": "+(pos>>1)+" isAfter:"+(pos&1));
221       }
222   }
223 }
224
Popular Tags