FastCharStream.cs
Upload User: wdq9901
Upload Date: 2008-12-31
Package Size: 1824k
Code Size: 5k
Category:

.net

Development Platform:

Java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  * 
  9.  * http://www.apache.org/licenses/LICENSE-2.0
  10.  * 
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. using System;
  18. namespace Lucene.Net.Analysis.Standard
  19. {
  20.     /// <summary>An efficient implementation of JavaCC's CharStream interface.  <p>Note that
  21.     /// this does not do line-number counting, but instead keeps track of the
  22.     /// character position of the token in the input, as required by Lucene's {@link
  23.     /// Lucene.Net.analysis.Token} API. 
  24.     /// </summary>
  25.     public sealed class FastCharStream : CharStream
  26.     {
  27.         internal char[] buffer = null;
  28.         internal int bufferLength = 0; // end of valid chars
  29.         internal int bufferPosition = 0; // next char to read
  30.         internal int tokenStart = 0; // offset in buffer
  31.         internal int bufferStart = 0; // position in file of buffer
  32.         internal System.IO.TextReader input; // source of chars
  33.         /// <summary>Constructs from a Reader. </summary>
  34.         public FastCharStream(System.IO.TextReader r)
  35.         {
  36.             input = r;
  37.         }
  38.         public char ReadChar()
  39.         {
  40.             if (bufferPosition >= bufferLength)
  41.                 Refill();
  42.             return buffer[bufferPosition++];
  43.         }
  44.         private void  Refill()
  45.         {
  46.             int newPosition = bufferLength - tokenStart;
  47.             if (tokenStart == 0)
  48.             {
  49.                 // token won't fit in buffer
  50.                 if (buffer == null)
  51.                 {
  52.                     // first time: alloc buffer
  53.                     buffer = new char[2048];
  54.                 }
  55.                 else if (bufferLength == buffer.Length)
  56.                 {
  57.                     // grow buffer
  58.                     char[] newBuffer = new char[buffer.Length * 2];
  59.                     Array.Copy(buffer, 0, newBuffer, 0, bufferLength);
  60.                     buffer = newBuffer;
  61.                 }
  62.             }
  63.             else
  64.             {
  65.                 // shift token to front
  66.                 Array.Copy(buffer, tokenStart, buffer, 0, newPosition);
  67.             }
  68.             bufferLength = newPosition; // update state
  69.             bufferPosition = newPosition;
  70.             bufferStart += tokenStart;
  71.             tokenStart = 0;
  72.             int charsRead = input.Read(buffer, newPosition, buffer.Length - newPosition);
  73.             if (charsRead <= 0)
  74.                 throw new System.IO.IOException("read past eof");
  75.             else
  76.                 bufferLength += charsRead;
  77.         }
  78.         public char BeginToken()
  79.         {
  80.             tokenStart = bufferPosition;
  81.             return ReadChar();
  82.         }
  83.         public void  Backup(int amount)
  84.         {
  85.             bufferPosition -= amount;
  86.         }
  87.         public System.String GetImage()
  88.         {
  89.             return new System.String(buffer, tokenStart, bufferPosition - tokenStart);
  90.         }
  91.         public char[] GetSuffix(int len)
  92.         {
  93.             char[] value_Renamed = new char[len];
  94.             Array.Copy(buffer, bufferPosition - len, value_Renamed, 0, len);
  95.             return value_Renamed;
  96.         }
  97.         public void  Done()
  98.         {
  99.             try
  100.             {
  101.                 input.Close();
  102.             }
  103.             catch (System.IO.IOException e)
  104.             {
  105.                 System.Console.Error.WriteLine("Caught: " + e + "; ignoring.");
  106.             }
  107.         }
  108.         public int GetColumn()
  109.         {
  110.             return bufferStart + bufferPosition;
  111.         }
  112.         public int GetLine()
  113.         {
  114.             return 1;
  115.         }
  116.         public int GetEndColumn()
  117.         {
  118.             return bufferStart + bufferPosition;
  119.         }
  120.         public int GetEndLine()
  121.         {
  122.             return 1;
  123.         }
  124.         public int GetBeginColumn()
  125.         {
  126.             return bufferStart + tokenStart;
  127.         }
  128.         public int GetBeginLine()
  129.         {
  130.             return 1;
  131.         }
  132.     }
  133. }