1 2 12 package com.versant.core.util; 13 14 19 public final class CharBuf { 20 21 private char[] buf; 22 private int size; 23 24 public CharBuf() { 25 this(64); 26 } 27 28 public CharBuf(int capacity) { 29 buf = new char[capacity]; 30 } 31 32 public CharBuf(String s) { 33 this(s.length()); 34 append(s); 35 } 36 37 public CharBuf(CharBuf s) { 38 this(s.size); 39 size = s.size; 40 System.arraycopy(s.buf, 0, buf, 0, size); 41 } 42 43 public int size() { 44 return size; 45 } 46 47 public void clear() { 48 size = 0; 49 } 50 51 private void ensureCapacity(int len) { 52 if (size + len > buf.length) { 53 int n = buf.length * 3 / 2 + 1; 54 if (size + len > n) { 55 n = size + len; 56 } 57 char[] a = new char[n]; 58 System.arraycopy(buf, 0, a, 0, size); 59 buf = a; 60 } 61 } 62 63 66 public int append(char ch) { 67 ensureCapacity(size + 1); 68 int start = size; 69 buf[size++] = ch; 70 return start; 71 } 72 73 76 public int append(int i) { 77 return append(Integer.toString(i)); 78 } 79 80 83 public int append(String s) { 84 return append(s.toCharArray()); 85 } 86 87 90 public int append(char[] a) { 91 int n = a.length; 92 ensureCapacity(size + n); 93 int start = size; 94 System.arraycopy(a, 0, buf, size, n); 95 size += n; 96 return start; 97 } 98 99 104 public void replace(int i, char[] text) { 105 System.arraycopy(text, 0, buf, i, text.length); 106 } 107 108 112 public void replace(int first, int last, char c) { 113 for (int i = first; i < last; i++) { 114 buf[i] = c; 115 } 116 } 117 118 public String toString() { 119 return new String (buf, 0, size); 120 } 121 122 126 public char[] toArray(int offset, int length) { 127 char[] a = new char[length]; 128 System.arraycopy(buf, offset, a, 0, length); 129 return a; 130 } 131 132 public void setSize(int sz) { 133 size = sz; 134 } 135 136 140 public String toString(int offset, int length) { 141 return new String (buf, offset, length); 142 } 143 144 147 public void insert(int offset, char[] text) { 148 adjust(offset, offset, text.length); 149 System.arraycopy(text, 0, buf, offset, text.length); 150 } 151 152 155 public void insert(int offset, String text) { 156 adjust(offset, offset, text.length()); 157 text.getChars(0, text.length(), buf, offset); 158 } 159 160 164 public void replace(int from, int to, String text) { 165 adjust(from, to, text.length()); 166 text.getChars(0, text.length(), buf, from); 167 } 168 169 173 public void replace(int from, int to, char[] text) { 174 adjust(from, to, text.length); 175 System.arraycopy(text, 0, buf, from, text.length); 176 } 177 178 188 protected void adjust(int from, int to, int length) { 189 if (from >= 0 && to < size && from <= to) { 190 int change = from + length - to; 191 if (change > 0) { 192 ensureCapacity(size + change); 193 } 194 if (to < size){ 195 System.arraycopy(buf, to, buf, to + change, size - to); 196 size += change; 197 } 198 } else { 199 throw new ArrayIndexOutOfBoundsException ("Invalid remove range"); 200 } 201 } 202 203 206 public void set(int index, char value) { 207 buf[index] = value; 208 } 209 210 217 public void remove(int from, int to) { 218 if (from >= 0 && to <= size && from <= to) { 219 if (to < size){ 220 int change = from - to; 221 System.arraycopy(buf, to, buf, from, size - to); 222 size += change; 223 } 224 } else { 225 throw new ArrayIndexOutOfBoundsException ("Invalid remove range"); 226 } 227 } 228 229 } 230 | Popular Tags |