Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
BufferedIndexOutput.cs
Go to the documentation of this file.
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 
18 using System;
19 
20 namespace Lucene.Net.Store
21 {
22 
23  /// <summary>Base implementation class for buffered <see cref="IndexOutput" />. </summary>
24  public abstract class BufferedIndexOutput:IndexOutput
25  {
26  internal const int BUFFER_SIZE = 16384;
27 
28  private byte[] buffer = new byte[BUFFER_SIZE];
29  private long bufferStart = 0; // position in file of buffer
30  private int bufferPosition = 0; // position in buffer
31 
32  private bool isDisposed;
33 
34  /// <summary>Writes a single byte.</summary>
35  /// <seealso cref="IndexInput.ReadByte()">
36  /// </seealso>
37  public override void WriteByte(byte b)
38  {
39  if (bufferPosition >= BUFFER_SIZE)
40  Flush();
41  buffer[bufferPosition++] = b;
42  }
43 
44  /// <summary>Writes an array of bytes.</summary>
45  /// <param name="b">the bytes to write
46  /// </param>
47  /// <param name="length">the number of bytes to write
48  /// </param>
49  /// <seealso cref="IndexInput.ReadBytes(byte[],int,int)">
50  /// </seealso>
51  public override void WriteBytes(byte[] b, int offset, int length)
52  {
53  int bytesLeft = BUFFER_SIZE - bufferPosition;
54  // is there enough space in the buffer?
55  if (bytesLeft >= length)
56  {
57  // we add the data to the end of the buffer
58  Array.Copy(b, offset, buffer, bufferPosition, length);
59  bufferPosition += length;
60  // if the buffer is full, flush it
61  if (BUFFER_SIZE - bufferPosition == 0)
62  Flush();
63  }
64  else
65  {
66  // is data larger then buffer?
67  if (length > BUFFER_SIZE)
68  {
69  // we flush the buffer
70  if (bufferPosition > 0)
71  Flush();
72  // and write data at once
73  FlushBuffer(b, offset, length);
74  bufferStart += length;
75  }
76  else
77  {
78  // we fill/flush the buffer (until the input is written)
79  int pos = 0; // position in the input data
80  int pieceLength;
81  while (pos < length)
82  {
83  pieceLength = (length - pos < bytesLeft)?length - pos:bytesLeft;
84  Array.Copy(b, pos + offset, buffer, bufferPosition, pieceLength);
85  pos += pieceLength;
86  bufferPosition += pieceLength;
87  // if the buffer is full, flush it
88  bytesLeft = BUFFER_SIZE - bufferPosition;
89  if (bytesLeft == 0)
90  {
91  Flush();
92  bytesLeft = BUFFER_SIZE;
93  }
94  }
95  }
96  }
97  }
98 
99  /// <summary>Forces any buffered output to be written. </summary>
100  public override void Flush()
101  {
102  FlushBuffer(buffer, bufferPosition);
103  bufferStart += bufferPosition;
104  bufferPosition = 0;
105  }
106 
107  /// <summary>Expert: implements buffer write. Writes bytes at the current position in
108  /// the output.
109  /// </summary>
110  /// <param name="b">the bytes to write
111  /// </param>
112  /// <param name="len">the number of bytes to write
113  /// </param>
114  private void FlushBuffer(byte[] b, int len)
115  {
116  FlushBuffer(b, 0, len);
117  }
118 
119  /// <summary>Expert: implements buffer write. Writes bytes at the current position in
120  /// the output.
121  /// </summary>
122  /// <param name="b">the bytes to write
123  /// </param>
124  /// <param name="offset">the offset in the byte array
125  /// </param>
126  /// <param name="len">the number of bytes to write
127  /// </param>
128  public abstract void FlushBuffer(byte[] b, int offset, int len);
129 
130  /// <summary>Closes this stream to further operations. </summary>
131  protected override void Dispose(bool disposing)
132  {
133  if (isDisposed) return;
134 
135  if (disposing)
136  {
137  Flush();
138  }
139 
140  isDisposed = true;
141  }
142 
143  /// <summary>Returns the current position in this file, where the next write will
144  /// occur.
145  /// </summary>
146  /// <seealso cref="Seek(long)">
147  /// </seealso>
148  public override long FilePointer
149  {
150  get { return bufferStart + bufferPosition; }
151  }
152 
153  /// <summary>Sets current position in this file, where the next write will occur.</summary>
154  /// <seealso cref="FilePointer">
155  /// </seealso>
156  public override void Seek(long pos)
157  {
158  Flush();
159  bufferStart = pos;
160  }
161 
162  /// <summary>The number of bytes in the file. </summary>
163  public abstract override long Length { get; }
164  }
165 }