Outgoing socket I/O
[IRC.git] / Robust / src / ClassLibrary / StringBuffer.java
1 public class StringBuffer {
2     char value[];
3     int count;
4     int offset;
5     //    private static final int DEFAULTSIZE=16;
6
7     public StringBuffer(String str) {
8         value=new char[str.count+16];//16 is DEFAULTSIZE
9         count=str.count;
10         offset=0;
11         for(int i=0;i<count;i++)
12             value[i]=str.value[i+str.offset];
13     }
14
15     public StringBuffer() {
16         value=new char[16];//16 is DEFAULTSIZE
17         count=0;
18         offset=0;
19     }
20
21     public int length() {
22         return count;
23     }
24
25     public int capacity() {
26         return value.length;
27     }
28
29     public char charAt(int x) {
30         return value[x+offset];
31     }
32
33     public void append(String s) {
34         if ((s.count+count+offset)>value.length) {
35             // Need to allocate
36             char newvalue[]=new char[s.count+count+16]; //16 is DEFAULTSIZE
37             for(int i=0;i<count;i++)
38                 newvalue[i]=value[i+offset];
39             for(int i=0;i<s.count;i++)
40                 newvalue[i+count]=s.value[i+s.offset];
41             value=newvalue;
42             count+=s.count;
43             offset=0;
44         } else {
45             for(int i=0;i<s.count;i++) {
46                 value[i+count+offset]=s.value[i+s.offset];
47             }
48             count+=s.count;
49         }
50     }
51
52     public void append(StringBuffer s) {
53         if ((s.count+count+offset)>value.length) {
54             // Need to allocate
55             char newvalue[]=new char[s.count+count+16]; //16 is DEFAULTSIZE
56             for(int i=0;i<count;i++)
57                 newvalue[i]=value[i+offset];
58             for(int i=0;i<s.count;i++)
59                 newvalue[i+count]=s.value[i+s.offset];
60             value=newvalue;
61             count+=s.count;
62             offset=0;
63         } else {
64             for(int i=0;i<s.count;i++) {
65                 value[i+count+offset]=s.value[i+s.offset];
66             }
67             count+=s.count;
68         }
69     }
70
71     public String toString() {
72         return new String(this);
73     }
74 }