1 package org.jahia.services.search.analyzer; 3 4 57 58 import java.io.*; 59 60 64 public final class FastCharStream implements CharStream { 65 char[] buffer = null; 66 67 int bufferLength = 0; int bufferPosition = 0; 70 int tokenStart = 0; int bufferStart = 0; 73 Reader input; 75 76 public FastCharStream(Reader r) { 77 input = r; 78 } 79 80 public final char readChar() throws IOException { 81 if (bufferPosition >= bufferLength) 82 refill(); 83 return buffer[bufferPosition++]; 84 } 85 86 private final void refill() throws IOException { 87 int newPosition = bufferLength - tokenStart; 88 89 if (tokenStart == 0) { if (buffer == null) { buffer = new char[2048]; 92 } else if (bufferLength == buffer.length) { char[] newBuffer = new char[buffer.length*2]; 94 System.arraycopy(buffer, 0, newBuffer, 0, bufferLength); 95 buffer = newBuffer; 96 } 97 } else { System.arraycopy(buffer, tokenStart, buffer, 0, newPosition); 99 } 100 101 bufferLength = newPosition; bufferPosition = newPosition; 103 bufferStart += tokenStart; 104 tokenStart = 0; 105 106 int charsRead = input.read(buffer, newPosition, buffer.length-newPosition); 108 if (charsRead == -1) 109 throw new IOException("read past eof"); 110 else 111 bufferLength += charsRead; 112 } 113 114 public final char BeginToken() throws IOException { 115 tokenStart = bufferPosition; 116 return readChar(); 117 } 118 119 public final void backup(int amount) { 120 bufferPosition -= amount; 121 } 122 123 public final String GetImage() { 124 return new String (buffer, tokenStart, bufferPosition - tokenStart); 125 } 126 127 public final char[] GetSuffix(int len) { 128 char[] value = new char[len]; 129 System.arraycopy(buffer, bufferPosition - len, value, 0, len); 130 return value; 131 } 132 133 public final void Done() { 134 try { 135 input.close(); 136 } catch (IOException e) { 137 System.err.println("Caught: " + e + "; ignoring."); 138 } 139 } 140 141 public final int getColumn() { 142 return bufferStart + bufferPosition; 143 } 144 public final int getLine() { 145 return 1; 146 } 147 public final int getEndColumn() { 148 return bufferStart + bufferPosition; 149 } 150 public final int getEndLine() { 151 return 1; 152 } 153 public final int getBeginColumn() { 154 return bufferStart + tokenStart; 155 } 156 public final int getBeginLine() { 157 return 1; 158 } 159 } 160 | Popular Tags |