KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
20  * A memory-resident {@link IndexInput} implementation.
21  *
22  * @version $Id: RAMInputStream.java 150537 2004-09-28 20:45:26Z cutting $
23  */

24
25 class RAMInputStream extends BufferedIndexInput implements Cloneable JavaDoc {
26   private RAMFile file;
27   private int pointer = 0;
28   private long length;
29
30   public RAMInputStream(RAMFile f) {
31     file = f;
32     length = file.length;
33   }
34
35   public void readInternal(byte[] dest, int destOffset, int len) {
36     int remainder = len;
37     int start = pointer;
38     while (remainder != 0) {
39       int bufferNumber = start/BUFFER_SIZE;
40       int bufferOffset = start%BUFFER_SIZE;
41       int bytesInBuffer = BUFFER_SIZE - bufferOffset;
42       int bytesToCopy = bytesInBuffer >= remainder ? remainder : bytesInBuffer;
43       byte[] buffer = (byte[])file.buffers.elementAt(bufferNumber);
44       System.arraycopy(buffer, bufferOffset, dest, destOffset, bytesToCopy);
45       destOffset += bytesToCopy;
46       start += bytesToCopy;
47       remainder -= bytesToCopy;
48     }
49     pointer += len;
50   }
51
52   public void close() {
53   }
54
55   public void seekInternal(long pos) {
56     pointer = (int)pos;
57   }
58
59   public long length() {
60     return length;
61   }
62
63 }
64
Popular Tags