cvsimport
[beagle.git] / beagled / Lucene.Net / Store / RAMOutputStream.cs
blob3fc930b5ba26d6a47ddc65d1ee98e3ae5c9603f0
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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 using System;
19 namespace Lucene.Net.Store
22 /// <summary> A memory-resident {@link IndexOutput} implementation.
23 ///
24 /// </summary>
25 /// <version> $Id: RAMOutputStream.cs,v 1.5 2006/10/02 17:11:11 joeshaw Exp $
26 /// </version>
28 public class RAMOutputStream : BufferedIndexOutput
30 private RAMFile file;
31 private int pointer = 0;
33 /// <summary>Construct an empty output buffer. </summary>
34 public RAMOutputStream() : this(new RAMFile())
38 internal RAMOutputStream(RAMFile f)
40 file = f;
43 /// <summary>Copy the current contents of this buffer to the named output. </summary>
44 public virtual void WriteTo(IndexOutput out_Renamed)
46 Flush();
47 long end = file.length;
48 long pos = 0;
49 int buffer = 0;
50 while (pos < end)
52 int length = BUFFER_SIZE;
53 long nextPos = pos + length;
54 if (nextPos > end)
56 // at the last buffer
57 length = (int) (end - pos);
59 out_Renamed.WriteBytes((byte[]) file.buffers[buffer++], length);
60 pos = nextPos;
64 /// <summary>Resets this to an empty buffer. </summary>
65 public virtual void Reset()
67 try
69 Seek(0);
71 catch (System.IO.IOException e)
73 // should never happen
74 throw new System.SystemException(e.ToString());
77 file.length = 0;
80 public override void FlushBuffer(byte[] src, int len)
82 byte[] buffer;
83 int bufferPos = 0;
84 while (bufferPos != len)
86 int bufferNumber = pointer / BUFFER_SIZE;
87 int bufferOffset = pointer % BUFFER_SIZE;
88 int bytesInBuffer = BUFFER_SIZE - bufferOffset;
89 int remainInSrcBuffer = len - bufferPos;
90 int bytesToCopy = bytesInBuffer >= remainInSrcBuffer ? remainInSrcBuffer : bytesInBuffer;
92 if (bufferNumber == file.buffers.Count)
94 buffer = new byte[BUFFER_SIZE];
95 file.buffers.Add(buffer);
97 else
99 buffer = (byte[]) file.buffers[bufferNumber];
102 Array.Copy(src, bufferPos, buffer, bufferOffset, bytesToCopy);
103 bufferPos += bytesToCopy;
104 pointer += bytesToCopy;
107 if (pointer > file.length)
108 file.length = pointer;
110 file.lastModified = System.DateTime.UtcNow.Ticks;
113 public override void Close()
115 base.Close();
118 public override void Seek(long pos)
120 base.Seek(pos);
121 pointer = (int) pos;
123 public override long Length()
125 return file.length;