1 package org.apache.lucene.store;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.io.IOException;
21 import java.io.EOFException;
23 /** A memory-resident {@link IndexInput} implementation.
26 public class RAMInputStream extends IndexInput implements Cloneable {
27 static final int BUFFER_SIZE = RAMOutputStream.BUFFER_SIZE;
32 private byte[] currentBuffer;
33 private int currentBufferIndex;
35 private int bufferPosition;
36 private long bufferStart;
37 private int bufferLength;
39 /** Please pass String name */
41 public RAMInputStream(RAMFile f) throws IOException {
45 public RAMInputStream(String name, RAMFile f) throws IOException {
46 super("RAMInputStream(name=" + name + ")");
49 if (length/BUFFER_SIZE >= Integer.MAX_VALUE) {
50 throw new IOException("RAMInputStream too large length=" + length + ": " + name);
53 // make sure that we switch to the
54 // first needed buffer lazily
55 currentBufferIndex = -1;
65 public long length() {
70 public byte readByte() throws IOException {
71 if (bufferPosition >= bufferLength) {
73 switchCurrentBuffer(true);
75 return currentBuffer[bufferPosition++];
79 public void readBytes(byte[] b, int offset, int len) throws IOException {
81 if (bufferPosition >= bufferLength) {
83 switchCurrentBuffer(true);
86 int remainInBuffer = bufferLength - bufferPosition;
87 int bytesToCopy = len < remainInBuffer ? len : remainInBuffer;
88 System.arraycopy(currentBuffer, bufferPosition, b, offset, bytesToCopy);
89 offset += bytesToCopy;
91 bufferPosition += bytesToCopy;
95 private final void switchCurrentBuffer(boolean enforceEOF) throws IOException {
96 bufferStart = (long) BUFFER_SIZE * (long) currentBufferIndex;
97 if (currentBufferIndex >= file.numBuffers()) {
98 // end of file reached, no more buffers left
100 throw new EOFException("Read past EOF (resource: " + this + ")");
102 // Force EOF if a read takes place at this position
103 currentBufferIndex--;
104 bufferPosition = BUFFER_SIZE;
107 currentBuffer = file.getBuffer(currentBufferIndex);
109 long buflen = length - bufferStart;
110 bufferLength = buflen > BUFFER_SIZE ? BUFFER_SIZE : (int) buflen;
115 public void copyBytes(IndexOutput out, long numBytes) throws IOException {
116 assert numBytes >= 0: "numBytes=" + numBytes;
118 long left = numBytes;
120 if (bufferPosition == bufferLength) {
121 ++currentBufferIndex;
122 switchCurrentBuffer(true);
125 final int bytesInBuffer = bufferLength - bufferPosition;
126 final int toCopy = (int) (bytesInBuffer < left ? bytesInBuffer : left);
127 out.writeBytes(currentBuffer, bufferPosition, toCopy);
128 bufferPosition += toCopy;
132 assert left == 0: "Insufficient bytes to copy: numBytes=" + numBytes + " copied=" + (numBytes - left);
136 public long getFilePointer() {
137 return currentBufferIndex < 0 ? 0 : bufferStart + bufferPosition;
141 public void seek(long pos) throws IOException {
142 if (currentBuffer==null || pos < bufferStart || pos >= bufferStart + BUFFER_SIZE) {
143 currentBufferIndex = (int) (pos / BUFFER_SIZE);
144 switchCurrentBuffer(false);
146 bufferPosition = (int) (pos % BUFFER_SIZE);