KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > search > analyzer > FastCharStream


1 // FastCharStream.java
2
package org.jahia.services.search.analyzer;
3
4 /* ====================================================================
5  * The Apache Software License, Version 1.1
6  *
7  * Copyright (c) 2001 The Apache Software Foundation. All rights
8  * reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright
18  * notice, this list of conditions and the following disclaimer in
19  * the documentation and/or other materials provided with the
20  * distribution.
21  *
22  * 3. The end-user documentation included with the redistribution,
23  * if any, must include the following acknowledgment:
24  * "This product includes software developed by the
25  * Apache Software Foundation (http://www.apache.org/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Apache" and "Apache Software Foundation" and
30  * "Apache Lucene" must not be used to endorse or promote products
31  * derived from this software without prior written permission. For
32  * written permission, please contact apache@apache.org.
33  *
34  * 5. Products derived from this software may not be called "Apache",
35  * "Apache Lucene", nor may "Apache" appear in their name, without
36  * prior written permission of the Apache Software Foundation.
37  *
38  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
39  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49  * SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This software consists of voluntary contributions made by many
53  * individuals on behalf of the Apache Software Foundation. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 import java.io.*;
59
60 /** An efficient implementation of JavaCC's CharStream interface. <p>Note that
61  * this does not do line-number counting, but instead keeps track of the
62  * character position of the token in the input, as required by Lucene's {@link
63  * org.apache.lucene.analysis.Token} API. */

64 public final class FastCharStream implements CharStream {
65   char[] buffer = null;
66
67   int bufferLength = 0; // end of valid chars
68
int bufferPosition = 0; // next char to read
69

70   int tokenStart = 0; // offset in buffer
71
int bufferStart = 0; // position in file of buffer
72

73   Reader input; // source of chars
74

75   /** Constructs from a Reader. */
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) { // token won't fit in buffer
90
if (buffer == null) { // first time: alloc buffer
91
buffer = new char[2048];
92       } else if (bufferLength == buffer.length) { // grow buffer
93
char[] newBuffer = new char[buffer.length*2];
94     System.arraycopy(buffer, 0, newBuffer, 0, bufferLength);
95     buffer = newBuffer;
96       }
97     } else { // shift token to front
98
System.arraycopy(buffer, tokenStart, buffer, 0, newPosition);
99     }
100
101     bufferLength = newPosition; // update state
102
bufferPosition = newPosition;
103     bufferStart += tokenStart;
104     tokenStart = 0;
105
106     int charsRead = // fill space in buffer
107
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 JavaDoc GetImage() {
124     return new String JavaDoc(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