1 public class StringBuffer {
4 // private static final int DEFAULTSIZE=16;
6 public StringBuffer(String str) {
7 value=new char[str.count+16]; //16 is DEFAULTSIZE
9 for(int i=0; i<count; i++)
10 value[i]=str.value[i+str.offset];
13 public StringBuffer() {
14 value=new char[16]; //16 is DEFAULTSIZE
18 public StringBuffer(int i) {
27 public int capacity() {
31 public char charAt(int x) {
35 public StringBuffer append(char c) {
36 return append(String.valueOf(c));
39 public StringBuffer append(String s) {
40 if ((s.count+count)>value.length) {
42 char newvalue[]=new char[s.count+count+16]; //16 is DEFAULTSIZE
43 for(int i=0; i<count; i++)
45 for(int i=0; i<s.count; i++)
46 newvalue[i+count]=s.value[i+s.offset];
50 for(int i=0; i<s.count; i++) {
51 value[i+count]=s.value[i+s.offset];
58 public void ensureCapacity(int i) {
63 char newvalue[]=new char[i];
64 for(int ii=0; ii<count; ii++)
65 newvalue[ii]=value[ii];
70 public StringBuffer append(StringBuffer s) {
71 if ((s.count+count)>value.length) {
73 char newvalue[]=new char[s.count+count+16]; //16 is DEFAULTSIZE
74 for(int i=0; i<count; i++)
76 for(int i=0; i<s.count; i++)
77 newvalue[i+count]=s.value[i];
81 for(int i=0; i<s.count; i++) {
82 value[i+count]=s.value[i];
89 public int indexOf(String str) {
90 return indexOf(str, 0);
93 public synchronized int indexOf(String str, int fromIndex) {
94 String vstr = new String(value, 0, count);
95 return vstr.indexOf(str, fromIndex);
98 public String toString() {
99 return new String(this);
102 public synchronized StringBuffer replace(int start, int end, String str) {
105 System.printString("StringIndexOutOfBoundsException: "+start+"\n");
109 System.printString("StringIndexOutOfBoundsException: start > length()\n");
113 System.printString("StringIndexOutOfBoundsException: start > end\n");
120 int len = str.length();
121 int newCount = count + len - (end - start);
122 if (newCount > value.length)
123 expandCapacity(newCount);
125 System.arraycopy(value, end, value, start + len, count - end);
126 str.getChars(value, start);
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;
138 char newValue[] = new char[newCapacity];
139 System.arraycopy(value, 0, newValue, 0, count);