RAMInputStream.java
01 package gate.creole.annic.apache.lucene.store;
02 
03 /**
04  * Copyright 2004 The Apache Software Foundation
05  *
06  * Licensed under the Apache License, Version 2.0 (the "License");
07  * you may not use this file except in compliance with the License.
08  * You may obtain a copy of the License at
09  *
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 InputStream} implementation.
21  *
22  @version $Id: RAMInputStream.java 529 2004-10-05 11:55:26Z niraj $
23  */
24 
25 class RAMInputStream extends InputStream implements Cloneable {
26   private RAMFile file;
27   private int pointer = 0;
28 
29   public RAMInputStream(RAMFile f) {
30     file = f;
31     length = file.length;
32   }
33 
34   public void readInternal(byte[] dest, int destOffset, int len) {
35     int remainder = len;
36     int start = pointer;
37     while (remainder != 0) {
38       int bufferNumber = start/BUFFER_SIZE;
39       int bufferOffset = start%BUFFER_SIZE;
40       int bytesInBuffer = BUFFER_SIZE - bufferOffset;
41       int bytesToCopy = bytesInBuffer >= remainder ? remainder : bytesInBuffer;
42       byte[] buffer = (byte[])file.buffers.elementAt(bufferNumber);
43       System.arraycopy(buffer, bufferOffset, dest, destOffset, bytesToCopy);
44       destOffset += bytesToCopy;
45       start += bytesToCopy;
46       remainder -= bytesToCopy;
47     }
48     pointer += len;
49   }
50 
51   public void close() {
52   }
53 
54   public void seekInternal(long pos) {
55     pointer = (int)pos;
56   }
57 }