Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
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 
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 
37  public override void WriteByte(byte b)
38  {
39  if (bufferPosition >= BUFFER_SIZE)
40  Flush();
41  buffer[bufferPosition++] = b;
42  }
43 
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 
100  public override void Flush()
101  {
102  FlushBuffer(buffer, bufferPosition);
103  bufferStart += bufferPosition;
104  bufferPosition = 0;
105  }
106 
114  private void FlushBuffer(byte[] b, int len)
115  {
116  FlushBuffer(b, 0, len);
117  }
118 
128  public abstract void FlushBuffer(byte[] b, int offset, int len);
129 
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 
148  public override long FilePointer
149  {
150  get { return bufferStart + bufferPosition; }
151  }
152 
156  public override void Seek(long pos)
157  {
158  Flush();
159  bufferStart = pos;
160  }
161 
163  public abstract override long Length { get; }
164  }
165 }