1 package org.apache.lucene.analysis;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.io.IOException;
21 import java.io.Reader;
23 import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
24 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
25 import org.apache.lucene.util.AttributeSource;
28 * Emits the entire input as a single token.
30 public final class KeywordTokenizer extends Tokenizer {
32 private static final int DEFAULT_BUFFER_SIZE = 256;
34 private boolean done = false;
35 private int finalOffset;
36 private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
37 private OffsetAttribute offsetAtt = addAttribute(OffsetAttribute.class);
39 public KeywordTokenizer(Reader input) {
40 this(input, DEFAULT_BUFFER_SIZE);
43 public KeywordTokenizer(Reader input, int bufferSize) {
45 termAtt.resizeBuffer(bufferSize);
48 public KeywordTokenizer(AttributeSource source, Reader input, int bufferSize) {
50 termAtt.resizeBuffer(bufferSize);
53 public KeywordTokenizer(AttributeFactory factory, Reader input, int bufferSize) {
54 super(factory, input);
55 termAtt.resizeBuffer(bufferSize);
59 public final boolean incrementToken() throws IOException {
64 char[] buffer = termAtt.buffer();
66 final int length = input.read(buffer, upto, buffer.length-upto);
67 if (length == -1) break;
69 if (upto == buffer.length)
70 buffer = termAtt.resizeBuffer(1+buffer.length);
72 termAtt.setLength(upto);
73 finalOffset = correctOffset(upto);
74 offsetAtt.setOffset(correctOffset(0), finalOffset);
81 public final void end() {
83 offsetAtt.setOffset(finalOffset, finalOffset);
87 public void reset(Reader input) throws IOException {