KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > store > RAMOutputStream


1 package org.apache.lucene.store;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.IOException JavaDoc;
20
21 /**
22  * A memory-resident {@link IndexOutput} implementation.
23  *
24  * @version $Id: RAMOutputStream.java 381429 2006-02-27 20:17:01Z cutting $
25  */

26
27 public class RAMOutputStream extends BufferedIndexOutput {
28   private RAMFile file;
29   private int pointer = 0;
30
31   /** Construct an empty output buffer. */
32   public RAMOutputStream() {
33     this(new RAMFile());
34   }
35
36   RAMOutputStream(RAMFile f) {
37     file = f;
38   }
39
40   /** Copy the current contents of this buffer to the named output. */
41   public void writeTo(IndexOutput out) throws IOException JavaDoc {
42     flush();
43     final long end = file.length;
44     long pos = 0;
45     int buffer = 0;
46     while (pos < end) {
47       int length = BUFFER_SIZE;
48       long nextPos = pos + length;
49       if (nextPos > end) { // at the last buffer
50
length = (int)(end - pos);
51       }
52       out.writeBytes((byte[])file.buffers.elementAt(buffer++), length);
53       pos = nextPos;
54     }
55   }
56
57   /** Resets this to an empty buffer. */
58   public void reset() {
59     try {
60       seek(0);
61     } catch (IOException JavaDoc e) { // should never happen
62
throw new RuntimeException JavaDoc(e.toString());
63     }
64
65     file.length = 0;
66   }
67
68   public void flushBuffer(byte[] src, int len) {
69     byte[] buffer;
70     int bufferPos = 0;
71     while (bufferPos != len) {
72       int bufferNumber = pointer/BUFFER_SIZE;
73       int bufferOffset = pointer%BUFFER_SIZE;
74       int bytesInBuffer = BUFFER_SIZE - bufferOffset;
75       int remainInSrcBuffer = len - bufferPos;
76       int bytesToCopy = bytesInBuffer >= remainInSrcBuffer ? remainInSrcBuffer : bytesInBuffer;
77
78       if (bufferNumber == file.buffers.size()) {
79         buffer = new byte[BUFFER_SIZE];
80         file.buffers.addElement(buffer);
81       } else {
82         buffer = (byte[]) file.buffers.elementAt(bufferNumber);
83       }
84
85       System.arraycopy(src, bufferPos, buffer, bufferOffset, bytesToCopy);
86       bufferPos += bytesToCopy;
87       pointer += bytesToCopy;
88     }
89
90     if (pointer > file.length)
91       file.length = pointer;
92
93     file.lastModified = System.currentTimeMillis();
94   }
95
96   public void close() throws IOException JavaDoc {
97     super.close();
98   }
99
100   public void seek(long pos) throws IOException JavaDoc {
101     super.seek(pos);
102     pointer = (int)pos;
103   }
104   public long length() {
105     return file.length;
106   }
107 }
108
Popular Tags