Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Lucene.Net / Store / RAMOutputStream.cs
blob20486d4a47151b2566d84d0d32d3442099ea739d
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.
16 using System;
17 namespace Lucene.Net.Store
20 /// <summary> A memory-resident {@link OutputStream} implementation.
21 ///
22 /// </summary>
23 /// <version> $Id: RAMOutputStream.cs,v 1.2 2005/06/24 18:52:03 joeshaw Exp $
24 /// </version>
26 public class RAMOutputStream:OutputStream
28 private RAMFile file;
29 private int pointer = 0;
31 /// <summary>Construct an empty output buffer. </summary>
32 public RAMOutputStream():this(new RAMFile())
36 internal RAMOutputStream(RAMFile f)
38 file = f;
41 /// <summary>Copy the current contents of this buffer to the named output. </summary>
42 public virtual void WriteTo(OutputStream out_Renamed)
44 Flush();
45 long end = file.length;
46 long pos = 0;
47 int buffer = 0;
48 while (pos < end)
50 int length = BUFFER_SIZE;
51 long nextPos = pos + length;
52 if (nextPos > end)
54 // at the last buffer
55 length = (int) (end - pos);
57 out_Renamed.WriteBytes((byte[]) file.buffers[buffer++], length);
58 pos = nextPos;
62 /// <summary>Resets this to an empty buffer. </summary>
63 public virtual void Leset()
65 try
67 Seek(0);
69 catch (System.IO.IOException e)
71 // should never happen
72 throw new System.SystemException(e.ToString());
75 file.length = 0;
78 public override void FlushBuffer(byte[] src, int len)
80 int bufferNumber = pointer / BUFFER_SIZE;
81 int bufferOffset = pointer % BUFFER_SIZE;
82 int bytesInBuffer = BUFFER_SIZE - bufferOffset;
83 int bytesToCopy = bytesInBuffer >= len?len:bytesInBuffer;
85 if (bufferNumber == file.buffers.Count)
86 file.buffers.Add(new byte[BUFFER_SIZE]);
88 byte[] buffer = (byte[]) file.buffers[bufferNumber];
89 Array.Copy(src, 0, buffer, bufferOffset, bytesToCopy);
91 if (bytesToCopy < len)
93 // not all in one buffer
94 int srcOffset = bytesToCopy;
95 bytesToCopy = len - bytesToCopy; // remaining bytes
96 bufferNumber++;
97 if (bufferNumber == file.buffers.Count)
98 file.buffers.Add(new byte[BUFFER_SIZE]);
99 buffer = (byte[]) file.buffers[bufferNumber];
100 Array.Copy(src, srcOffset, buffer, 0, bytesToCopy);
102 pointer += len;
103 if (pointer > file.length)
104 file.length = pointer;
106 // FIXED joeshaw@novell.com 24 Jun 2005 - Use UTC
107 file.lastModified = (System.DateTime.UtcNow.Ticks - 621355968000000000) / 10000;
110 public override void Close()
112 base.Close();
115 public override void Seek(long pos)
117 base.Seek(pos);
118 pointer = (int) pos;
120 public override long Length()
122 return file.length;