1 16 17 package org.apache.xerces.util; 18 19 import org.apache.xerces.xni.XMLString; 20 21 42 public class XMLStringBuffer 43 extends XMLString { 44 45 49 50 public static final int DEFAULT_SIZE = 32; 51 52 56 59 public XMLStringBuffer() { 60 this(DEFAULT_SIZE); 61 } 63 68 public XMLStringBuffer(int size) { 69 ch = new char[size]; 70 } 72 73 public XMLStringBuffer(char c) { 74 this(1); 75 append(c); 76 } 78 79 public XMLStringBuffer(String s) { 80 this(s.length()); 81 append(s); 82 } 84 85 public XMLStringBuffer(char[] ch, int offset, int length) { 86 this(length); 87 append(ch, offset, length); 88 } 90 91 public XMLStringBuffer(XMLString s) { 92 this(s.length); 93 append(s); 94 } 96 100 101 public void clear() { 102 offset = 0; 103 length = 0; 104 } 105 106 111 public void append(char c) { 112 if (this.length + 1 > this.ch.length) { 113 int newLength = this.ch.length*2; 114 if (newLength < this.ch.length + DEFAULT_SIZE) 115 newLength = this.ch.length + DEFAULT_SIZE; 116 char[] newch = new char[newLength]; 117 System.arraycopy(this.ch, 0, newch, 0, this.length); 118 this.ch = newch; 119 } 120 this.ch[this.length] = c; 121 this.length++; 122 } 124 129 public void append(String s) { 130 int length = s.length(); 131 if (this.length + length > this.ch.length) { 132 int newLength = this.ch.length*2; 133 if (newLength < this.length + length + DEFAULT_SIZE) 134 newLength = this.ch.length + length + DEFAULT_SIZE; 135 char[] newch = new char[newLength]; 136 System.arraycopy(this.ch, 0, newch, 0, this.length); 137 this.ch = newch; 138 } 139 s.getChars(0, length, this.ch, this.length); 140 this.length += length; 141 } 143 150 public void append(char[] ch, int offset, int length) { 151 if (this.length + length > this.ch.length) { 152 char[] newch = new char[this.ch.length + length + DEFAULT_SIZE]; 153 System.arraycopy(this.ch, 0, newch, 0, this.length); 154 this.ch = newch; 155 } 156 System.arraycopy(ch, offset, this.ch, this.length, length); 157 this.length += length; 158 } 160 165 public void append(XMLString s) { 166 append(s.ch, s.offset, s.length); 167 } 169 } | Popular Tags |