[monkeydoc] Merge/add monkeydoc to master.
[mono.git] / mcs / tools / monkeydoc / Lucene.Net / Lucene.Net / Store / BufferedIndexOutput.cs
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 Mono.Lucene.Net.Store
21 {
22         
23         /// <summary>Base implementation class for buffered {@link 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                 /// <summary>Writes a single byte.</summary>
33                 /// <seealso cref="IndexInput.ReadByte()">
34                 /// </seealso>
35                 public override void  WriteByte(byte b)
36                 {
37                         if (bufferPosition >= BUFFER_SIZE)
38                                 Flush();
39                         buffer[bufferPosition++] = b;
40                 }
41                 
42                 /// <summary>Writes an array of bytes.</summary>
43                 /// <param name="b">the bytes to write
44                 /// </param>
45                 /// <param name="length">the number of bytes to write
46                 /// </param>
47                 /// <seealso cref="IndexInput.ReadBytes(byte[],int,int)">
48                 /// </seealso>
49                 public override void  WriteBytes(byte[] b, int offset, int length)
50                 {
51                         int bytesLeft = BUFFER_SIZE - bufferPosition;
52                         // is there enough space in the buffer?
53                         if (bytesLeft >= length)
54                         {
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)
60                                         Flush();
61                         }
62                         else
63                         {
64                                 // is data larger then buffer?
65                                 if (length > BUFFER_SIZE)
66                                 {
67                                         // we flush the buffer
68                                         if (bufferPosition > 0)
69                                                 Flush();
70                                         // and write data at once
71                                         FlushBuffer(b, offset, length);
72                                         bufferStart += length;
73                                 }
74                                 else
75                                 {
76                                         // we fill/flush the buffer (until the input is written)
77                                         int pos = 0; // position in the input data
78                                         int pieceLength;
79                                         while (pos < length)
80                                         {
81                                                 pieceLength = (length - pos < bytesLeft)?length - pos:bytesLeft;
82                                                 Array.Copy(b, pos + offset, buffer, bufferPosition, pieceLength);
83                                                 pos += pieceLength;
84                                                 bufferPosition += pieceLength;
85                                                 // if the buffer is full, flush it
86                                                 bytesLeft = BUFFER_SIZE - bufferPosition;
87                                                 if (bytesLeft == 0)
88                                                 {
89                                                         Flush();
90                                                         bytesLeft = BUFFER_SIZE;
91                                                 }
92                                         }
93                                 }
94                         }
95                 }
96                 
97                 /// <summary>Forces any buffered output to be written. </summary>
98                 public override void  Flush()
99                 {
100                         FlushBuffer(buffer, bufferPosition);
101                         bufferStart += bufferPosition;
102                         bufferPosition = 0;
103                 }
104                 
105                 /// <summary>Expert: implements buffer write.  Writes bytes at the current position in
106                 /// the output.
107                 /// </summary>
108                 /// <param name="b">the bytes to write
109                 /// </param>
110                 /// <param name="len">the number of bytes to write
111                 /// </param>
112                 private void  FlushBuffer(byte[] b, int len)
113                 {
114                         FlushBuffer(b, 0, len);
115                 }
116                 
117                 /// <summary>Expert: implements buffer write.  Writes bytes at the current position in
118                 /// the output.
119                 /// </summary>
120                 /// <param name="b">the bytes to write
121                 /// </param>
122                 /// <param name="offset">the offset in the byte array
123                 /// </param>
124                 /// <param name="len">the number of bytes to write
125                 /// </param>
126                 public abstract void  FlushBuffer(byte[] b, int offset, int len);
127                 
128                 /// <summary>Closes this stream to further operations. </summary>
129                 public override void  Close()
130                 {
131                         Flush();
132                 }
133                 
134                 /// <summary>Returns the current position in this file, where the next write will
135                 /// occur.
136                 /// </summary>
137                 /// <seealso cref="Seek(long)">
138                 /// </seealso>
139                 public override long GetFilePointer()
140                 {
141                         return bufferStart + bufferPosition;
142                 }
143                 
144                 /// <summary>Sets current position in this file, where the next write will occur.</summary>
145                 /// <seealso cref="GetFilePointer()">
146                 /// </seealso>
147                 public override void  Seek(long pos)
148                 {
149                         Flush();
150                         bufferStart = pos;
151                 }
152                 
153                 /// <summary>The number of bytes in the file. </summary>
154                 public abstract override long Length();
155         }
156 }