try to make mp3decoder compile
[IRC.git] / Robust / src / ClassLibrary / SSJava / StringBuffer.java
1 public class StringBuffer {
2   char value[];
3   int count;
4   //    private static final int DEFAULTSIZE=16;
5
6   public StringBuffer(String str) {
7     value=new char[str.count+16];    //16 is DEFAULTSIZE
8     count=str.count;
9     for(int i=0; i<count; i++)
10       value[i]=str.value[i+str.offset];
11   }
12
13   public StringBuffer() {
14     value=new char[16];    //16 is DEFAULTSIZE
15     count=0;
16   }
17
18   public StringBuffer(int i) {
19     value=new char[i];
20     count=0;
21   }
22
23   public int length() {
24     return count;
25   }
26
27   public int capacity() {
28     return value.length;
29   }
30
31   public char charAt(int x) {
32     return value[x];
33   }
34
35   public StringBuffer append(char c) {
36     return append(String.valueOf(c));
37   }
38
39   public StringBuffer append(String s) {
40     if ((s.count+count)>value.length) {
41       // Need to allocate
42       char newvalue[]=new char[s.count+count+16];       //16 is DEFAULTSIZE
43       for(int i=0; i<count; i++)
44         newvalue[i]=value[i];
45       for(int i=0; i<s.count; i++)
46         newvalue[i+count]=s.value[i+s.offset];
47       value=newvalue;
48       count+=s.count;
49     } else {
50       for(int i=0; i<s.count; i++) {
51         value[i+count]=s.value[i+s.offset];
52       }
53       count+=s.count;
54     }
55     return this;
56   }
57
58   public void ensureCapacity(int i) {
59     int size=2*count;
60     if (i>size)
61       size=i;
62     if (i>value.length) {
63       char newvalue[]=new char[i];
64       for(int ii=0; ii<count; ii++)
65         newvalue[ii]=value[ii];
66       value=newvalue;
67     }
68   }
69
70   public StringBuffer append(StringBuffer s) {
71     if ((s.count+count)>value.length) {
72       // Need to allocate
73       char newvalue[]=new char[s.count+count+16];       //16 is DEFAULTSIZE
74       for(int i=0; i<count; i++)
75         newvalue[i]=value[i];
76       for(int i=0; i<s.count; i++)
77         newvalue[i+count]=s.value[i];
78       value=newvalue;
79       count+=s.count;
80     } else {
81       for(int i=0; i<s.count; i++) {
82         value[i+count]=s.value[i];
83       }
84       count+=s.count;
85     }
86     return this;
87   }
88
89   public int indexOf(String str) {
90     return indexOf(str, 0);
91   }
92
93   public synchronized int indexOf(String str, int fromIndex) {
94     String vstr = new String(value, 0, count);
95     return vstr.indexOf(str, fromIndex);
96   }
97
98   public String toString() {
99     return new String(this);
100   }
101
102   public synchronized StringBuffer replace(int start, int end, String str) {
103     if (start < 0) {
104       // FIXME
105       System.printString("StringIndexOutOfBoundsException: "+start+"\n");
106     }
107     if (start > count) {
108       // FIXME
109       System.printString("StringIndexOutOfBoundsException: start > length()\n");
110     }
111     if (start > end) {
112       // FIXME
113       System.printString("StringIndexOutOfBoundsException: start > end\n");
114     }
115     if (end > count)
116       end = count;
117
118     if (end > count)
119       end = count;
120     int len = str.length();
121     int newCount = count + len - (end - start);
122     if (newCount > value.length)
123       expandCapacity(newCount);
124
125     System.arraycopy(value, end, value, start + len, count - end);
126     str.getChars(value, start);
127     count = newCount;
128     return this;
129   }
130
131   void expandCapacity(int minimumCapacity) {
132     int newCapacity = (value.length + 1) * 2;
133     if (newCapacity < 0) {
134       newCapacity = 0x7fffffff /*Integer.MAX_VALUE*/;
135     } else if (minimumCapacity > newCapacity) {
136       newCapacity = minimumCapacity;
137     }
138     char newValue[] = new char[newCapacity];
139     System.arraycopy(value, 0, newValue, 0, count);
140     value = newValue;
141   }
142 }