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