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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 namespace Mono.Lucene.Net.Store
23 /// <summary>Base implementation class for buffered {@link IndexOutput}. </summary>
24 public abstract class BufferedIndexOutput:IndexOutput
26 internal const int BUFFER_SIZE = 16384;
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
32 /// <summary>Writes a single byte.</summary>
33 /// <seealso cref="IndexInput.ReadByte()">
35 public override void WriteByte(byte b)
37 if (bufferPosition >= BUFFER_SIZE)
39 buffer[bufferPosition++] = b;
42 /// <summary>Writes an array of bytes.</summary>
43 /// <param name="b">the bytes to write
45 /// <param name="length">the number of bytes to write
47 /// <seealso cref="IndexInput.ReadBytes(byte[],int,int)">
49 public override void WriteBytes(byte[] b, int offset, int length)
51 int bytesLeft = BUFFER_SIZE - bufferPosition;
52 // is there enough space in the buffer?
53 if (bytesLeft >= length)
55 // we add the data to the end of the buffer
56 Array.Copy(b, offset, buffer, bufferPosition, length);
57 bufferPosition += length;
58 // if the buffer is full, flush it
59 if (BUFFER_SIZE - bufferPosition == 0)
64 // is data larger then buffer?
65 if (length > BUFFER_SIZE)
67 // we flush the buffer
68 if (bufferPosition > 0)
70 // and write data at once
71 FlushBuffer(b, offset, length);
72 bufferStart += length;
76 // we fill/flush the buffer (until the input is written)
77 int pos = 0; // position in the input data
81 pieceLength = (length - pos < bytesLeft)?length - pos:bytesLeft;
82 Array.Copy(b, pos + offset, buffer, bufferPosition, pieceLength);
84 bufferPosition += pieceLength;
85 // if the buffer is full, flush it
86 bytesLeft = BUFFER_SIZE - bufferPosition;
90 bytesLeft = BUFFER_SIZE;
97 /// <summary>Forces any buffered output to be written. </summary>
98 public override void Flush()
100 FlushBuffer(buffer, bufferPosition);
101 bufferStart += bufferPosition;
105 /// <summary>Expert: implements buffer write. Writes bytes at the current position in
108 /// <param name="b">the bytes to write
110 /// <param name="len">the number of bytes to write
112 private void FlushBuffer(byte[] b, int len)
114 FlushBuffer(b, 0, len);
117 /// <summary>Expert: implements buffer write. Writes bytes at the current position in
120 /// <param name="b">the bytes to write
122 /// <param name="offset">the offset in the byte array
124 /// <param name="len">the number of bytes to write
126 public abstract void FlushBuffer(byte[] b, int offset, int len);
128 /// <summary>Closes this stream to further operations. </summary>
129 public override void Close()
134 /// <summary>Returns the current position in this file, where the next write will
137 /// <seealso cref="Seek(long)">
139 public override long GetFilePointer()
141 return bufferStart + bufferPosition;
144 /// <summary>Sets current position in this file, where the next write will occur.</summary>
145 /// <seealso cref="GetFilePointer()">
147 public override void Seek(long pos)
153 /// <summary>The number of bytes in the file. </summary>
154 public abstract override long Length();