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.
17 namespace Lucene
.Net
.Store
20 /// <summary> A memory-resident {@link OutputStream} implementation.
23 /// <version> $Id: RAMOutputStream.cs,v 1.2 2005/06/24 18:52:03 joeshaw Exp $
26 public class RAMOutputStream
:OutputStream
29 private int pointer
= 0;
31 /// <summary>Construct an empty output buffer. </summary>
32 public RAMOutputStream():this(new RAMFile())
36 internal RAMOutputStream(RAMFile f
)
41 /// <summary>Copy the current contents of this buffer to the named output. </summary>
42 public virtual void WriteTo(OutputStream out_Renamed
)
45 long end
= file
.length
;
50 int length
= BUFFER_SIZE
;
51 long nextPos
= pos
+ length
;
55 length
= (int) (end
- pos
);
57 out_Renamed
.WriteBytes((byte[]) file
.buffers
[buffer
++], length
);
62 /// <summary>Resets this to an empty buffer. </summary>
63 public virtual void Leset()
69 catch (System
.IO
.IOException e
)
71 // should never happen
72 throw new System
.SystemException(e
.ToString());
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
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
);
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()
115 public override void Seek(long pos
)
120 public override long Length()