2 * Copyright 2005 The Apache Software Foundation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 namespace Lucene
.Net
.Store
20 /// <summary>Base implementation class for buffered {@link IndexOutput}. </summary>
21 public abstract class BufferedIndexOutput
:IndexOutput
23 internal const int BUFFER_SIZE
= 1024;
25 private byte[] buffer
= new byte[BUFFER_SIZE
];
26 private long bufferStart
= 0; // position in file of buffer
27 private int bufferPosition
= 0; // position in buffer
29 /// <summary>Writes a single byte.</summary>
30 /// <seealso cref="IndexInput#ReadByte()">
32 public override void WriteByte(byte b
)
34 if (bufferPosition
>= BUFFER_SIZE
)
36 buffer
[bufferPosition
++] = b
;
39 /// <summary>Writes an array of bytes.</summary>
40 /// <param name="b">the bytes to write
42 /// <param name="length">the number of bytes to write
44 /// <seealso cref="IndexInput#ReadBytes(byte[],int,int)">
46 public override void WriteBytes(byte[] b
, int length
)
48 for (int i
= 0; i
< length
; i
++)
52 /// <summary>Forces any buffered output to be written. </summary>
53 public override void Flush()
55 FlushBuffer(buffer
, bufferPosition
);
56 bufferStart
+= bufferPosition
;
60 /// <summary>Expert: implements buffer write. Writes bytes at the current position in
63 /// <param name="b">the bytes to write
65 /// <param name="len">the number of bytes to write
67 public abstract void FlushBuffer(byte[] b
, int len
);
69 /// <summary>Closes this stream to further operations. </summary>
70 public override void Close()
75 /// <summary>Returns the current position in this file, where the next write will
78 /// <seealso cref="#Seek(long)">
80 public override long GetFilePointer()
82 return bufferStart
+ bufferPosition
;
85 /// <summary>Sets current position in this file, where the next write will occur.</summary>
86 /// <seealso cref="#GetFilePointer()">
88 public override void Seek(long pos
)
94 /// <summary>The number of bytes in the file. </summary>
95 public abstract override long Length();