1 11 package org.eclipse.jdt.internal.core.util; 12 13 import org.eclipse.jdt.internal.compiler.util.Util; 14 15 31 public class CharArrayBuffer { 32 36 protected char[][] fBuffer; 37 38 41 public static final int DEFAULT_BUFFER_SIZE = 10; 42 43 46 protected int fEnd; 47 48 51 protected int fSize; 52 53 58 protected int[][] fRanges; 59 62 public CharArrayBuffer() { 63 this(null, DEFAULT_BUFFER_SIZE); 64 } 65 71 public CharArrayBuffer(char[] first) { 72 this(first, DEFAULT_BUFFER_SIZE); 73 } 74 81 public CharArrayBuffer(char[] first, int size) { 82 fSize = (size > 0) ? size : DEFAULT_BUFFER_SIZE; 83 fBuffer = new char[fSize][]; 84 fRanges = new int[fSize][]; 85 fEnd = 0; 86 if (first != null) 87 append(first, 0, first.length); 88 } 89 94 public CharArrayBuffer(int size) { 95 this(null, size); 96 } 97 102 public CharArrayBuffer append(char[] src) { 103 if (src != null) 104 append(src, 0, src.length); 105 return this; 106 } 107 116 public CharArrayBuffer append(char[] src, int start, int length) { 117 if (start < 0) throw new ArrayIndexOutOfBoundsException (); 118 if (length < 0) throw new ArrayIndexOutOfBoundsException (); 119 if (src != null) { 120 int srcLength = src.length; 121 if (start > srcLength) throw new ArrayIndexOutOfBoundsException (); 122 if (length + start > srcLength) throw new ArrayIndexOutOfBoundsException (); 123 124 if (length > 0) { 125 if (fEnd == fSize) { 126 int size2 = fSize * 2; 127 System.arraycopy(fBuffer, 0, (fBuffer = new char[size2][]), 0, fSize); 128 System.arraycopy(fRanges, 0, (fRanges = new int[size2][]), 0, fSize); 129 fSize *= 2; 130 } 131 fBuffer[fEnd] = src; 132 fRanges[fEnd] = new int[] {start, length}; 133 fEnd++; 134 } 135 } 136 return this; 137 } 138 143 public CharArrayBuffer append(char c) { 144 append(new char[] {c}, 0, 1); 145 return this; 146 } 147 153 public CharArrayBuffer append(String src) { 154 if (src != null) 155 append(src.toCharArray(), 0, src.length()); 156 return this; 157 } 158 162 public char[] getContents() { 163 if (fEnd == 0) 164 return null; 165 166 int size = 0; 168 for (int i = 0; i < fEnd; i++) 169 size += fRanges[i][1]; 170 171 if (size > 0) { 172 char[] result = new char[size]; 173 int current = 0; 174 for(int i = 0; i < fEnd; i++) { 176 int[] range = fRanges[i]; 177 int length = range[1]; 178 System.arraycopy(fBuffer[i], range[0], result, current, length); 179 current += length; 180 } 181 return result; 182 } 183 return null; 184 } 185 189 public String toString() { 190 char[] contents = getContents(); 191 return (contents != null) ? new String (contents) : Util.EMPTY_STRING; 192 } 193 } 194 | Popular Tags |