1 2 23 24 25 public class CharCollector implements CharStream { 26 27 int bufsize; 28 int available; 29 int tokenBegin; 30 public int bufpos = -1; 31 private char[] buffer; 32 private int maxNextCharInd = 0; 33 34 private final void ExpandBuff(boolean wrapAround) 35 { 36 char[] newbuffer = new char[bufsize + 2048]; 37 38 try 39 { 40 if (wrapAround) 41 { 42 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 43 System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos); 44 buffer = newbuffer; 45 maxNextCharInd = (bufpos += (bufsize - tokenBegin)); 46 } 47 else 48 { 49 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 50 buffer = newbuffer; 51 maxNextCharInd = (bufpos -= tokenBegin); 52 } 53 } 54 catch (Throwable t) 55 { 56 System.out.println("Error : " + t.getClass().getName()); 57 throw new Error (); 58 } 59 60 bufsize += 2048; 61 available = bufsize; 62 tokenBegin = 0; 63 } 64 65 private final void FillBuff() 66 { 67 if (maxNextCharInd == available) 68 { 69 if (available == bufsize) 70 { 71 if (tokenBegin > 2048) 72 { 73 bufpos = maxNextCharInd = 0; 74 available = tokenBegin; 75 } 76 else if (tokenBegin < 0) 77 bufpos = maxNextCharInd = 0; 78 else 79 ExpandBuff(false); 80 } 81 else if (available > tokenBegin) 82 available = bufsize; 83 else if ((tokenBegin - available) < 2048) 84 ExpandBuff(true); 85 else 86 available = tokenBegin; 87 } 88 89 try { 90 wait(); 91 } catch (InterruptedException willNotHappen) { 92 throw new Error (); 93 } 94 } 95 96 99 synchronized public final void put(char c) 100 { 101 buffer[maxNextCharInd++] = c; 102 notify(); 103 } 104 105 public char BeginToken() throws java.io.IOException 106 { 107 tokenBegin = -1; 108 char c = readChar(); 109 tokenBegin = bufpos; 110 111 return c; 112 } 113 114 private int inBuf = 0; 115 synchronized public final char readChar() throws java.io.IOException 116 { 117 if (inBuf > 0) 118 { 119 --inBuf; 120 return (char)((char)0xff & buffer[(bufpos == bufsize - 1) ? (bufpos = 0) : ++bufpos]); 121 } 122 123 if (++bufpos >= maxNextCharInd) 124 FillBuff(); 125 126 return buffer[bufpos]; 127 } 128 129 133 134 public final int getColumn() { 135 return 0; 136 } 137 138 142 143 public final int getLine() { 144 return 0; 145 } 146 147 public final int getEndColumn() { 148 return 0; 149 } 150 151 public final int getEndLine() { 152 return 0; 153 } 154 155 public final int getBeginColumn() { 156 return 0; 157 } 158 159 public final int getBeginLine() { 160 return 0; 161 } 162 163 public final void backup(int amount) { 164 165 inBuf += amount; 166 if ((bufpos -= amount) < 0) 167 bufpos += bufsize; 168 } 169 170 public CharCollector(int buffersize) 171 { 172 available = bufsize = buffersize; 173 buffer = new char[buffersize]; 174 } 175 176 public CharCollector() 177 { 178 available = bufsize = 4096; 179 buffer = new char[4096]; 180 } 181 182 public void Clear() 183 { 184 bufpos = -1; 185 maxNextCharInd = 0; 186 inBuf = 0; 187 } 188 189 public final String GetImage() 190 { 191 if (bufpos >= tokenBegin) 192 return new String (buffer, tokenBegin, bufpos - tokenBegin + 1); 193 else 194 return new String (buffer, tokenBegin, bufsize - tokenBegin) + 195 new String (buffer, 0, bufpos + 1); 196 } 197 198 public final char[] GetSuffix(int len) 199 { 200 char[] ret = new char[len]; 201 202 if (bufpos + 1 >= len) 203 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len); 204 else 205 { 206 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, 207 len - bufpos - 1); 208 System.arraycopy(buffer, 0, ret, len - bufpos, bufpos + 1); 209 } 210 211 return ret; 212 } 213 214 public void Done() 215 { 216 buffer = null; 217 } 218 } 219 | Popular Tags |