1 package org.apache.lucene.analysis.standard; 3 4 19 20 import java.io.*; 21 22 26 public final class FastCharStream implements CharStream { 27 char[] buffer = null; 28 29 int bufferLength = 0; int bufferPosition = 0; 32 int tokenStart = 0; int bufferStart = 0; 35 Reader input; 37 38 public FastCharStream(Reader r) { 39 input = r; 40 } 41 42 public final char readChar() throws IOException { 43 if (bufferPosition >= bufferLength) 44 refill(); 45 return buffer[bufferPosition++]; 46 } 47 48 private final void refill() throws IOException { 49 int newPosition = bufferLength - tokenStart; 50 51 if (tokenStart == 0) { if (buffer == null) { buffer = new char[2048]; 54 } else if (bufferLength == buffer.length) { char[] newBuffer = new char[buffer.length*2]; 56 System.arraycopy(buffer, 0, newBuffer, 0, bufferLength); 57 buffer = newBuffer; 58 } 59 } else { System.arraycopy(buffer, tokenStart, buffer, 0, newPosition); 61 } 62 63 bufferLength = newPosition; bufferPosition = newPosition; 65 bufferStart += tokenStart; 66 tokenStart = 0; 67 68 int charsRead = input.read(buffer, newPosition, buffer.length-newPosition); 70 if (charsRead == -1) 71 throw new IOException("read past eof"); 72 else 73 bufferLength += charsRead; 74 } 75 76 public final char BeginToken() throws IOException { 77 tokenStart = bufferPosition; 78 return readChar(); 79 } 80 81 public final void backup(int amount) { 82 bufferPosition -= amount; 83 } 84 85 public final String GetImage() { 86 return new String (buffer, tokenStart, bufferPosition - tokenStart); 87 } 88 89 public final char[] GetSuffix(int len) { 90 char[] value = new char[len]; 91 System.arraycopy(buffer, bufferPosition - len, value, 0, len); 92 return value; 93 } 94 95 public final void Done() { 96 try { 97 input.close(); 98 } catch (IOException e) { 99 System.err.println("Caught: " + e + "; ignoring."); 100 } 101 } 102 103 public final int getColumn() { 104 return bufferStart + bufferPosition; 105 } 106 public final int getLine() { 107 return 1; 108 } 109 public final int getEndColumn() { 110 return bufferStart + bufferPosition; 111 } 112 public final int getEndLine() { 113 return 1; 114 } 115 public final int getBeginColumn() { 116 return bufferStart + tokenStart; 117 } 118 public final int getBeginLine() { 119 return 1; 120 } 121 } 122 | Popular Tags |