2 * Copyright 2004 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.
19 namespace Lucene
.Net
.Store
22 /// <summary> A memory-resident {@link IndexOutput} implementation.
25 /// <version> $Id: RAMOutputStream.cs,v 1.5 2006/10/02 17:11:11 joeshaw Exp $
28 public class RAMOutputStream
: BufferedIndexOutput
31 private int pointer
= 0;
33 /// <summary>Construct an empty output buffer. </summary>
34 public RAMOutputStream() : this(new RAMFile())
38 internal RAMOutputStream(RAMFile f
)
43 /// <summary>Copy the current contents of this buffer to the named output. </summary>
44 public virtual void WriteTo(IndexOutput out_Renamed
)
47 long end
= file
.length
;
52 int length
= BUFFER_SIZE
;
53 long nextPos
= pos
+ length
;
57 length
= (int) (end
- pos
);
59 out_Renamed
.WriteBytes((byte[]) file
.buffers
[buffer
++], length
);
64 /// <summary>Resets this to an empty buffer. </summary>
65 public virtual void Reset()
71 catch (System
.IO
.IOException e
)
73 // should never happen
74 throw new System
.SystemException(e
.ToString());
80 public override void FlushBuffer(byte[] src
, int len
)
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
);
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()
118 public override void Seek(long pos
)
123 public override long Length()