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;
22 /** A memory-resident {@link IndexInput} implementation.
25 public class RAMInputStream extends IndexInput implements Cloneable {
26 static final int BUFFER_SIZE = RAMOutputStream.BUFFER_SIZE;
31 private byte[] currentBuffer;
32 private int currentBufferIndex;
34 private int bufferPosition;
35 private long bufferStart;
36 private int bufferLength;
38 public RAMInputStream(RAMFile f) throws IOException {
41 if (length/BUFFER_SIZE >= Integer.MAX_VALUE) {
42 throw new IOException("Too large RAMFile! "+length);
45 // make sure that we switch to the
46 // first needed buffer lazily
47 currentBufferIndex = -1;
57 public long length() {
62 public byte readByte() throws IOException {
63 if (bufferPosition >= bufferLength) {
65 switchCurrentBuffer(true);
67 return currentBuffer[bufferPosition++];
71 public void readBytes(byte[] b, int offset, int len) throws IOException {
73 if (bufferPosition >= bufferLength) {
75 switchCurrentBuffer(true);
78 int remainInBuffer = bufferLength - bufferPosition;
79 int bytesToCopy = len < remainInBuffer ? len : remainInBuffer;
80 System.arraycopy(currentBuffer, bufferPosition, b, offset, bytesToCopy);
81 offset += bytesToCopy;
83 bufferPosition += bytesToCopy;
87 private final void switchCurrentBuffer(boolean enforceEOF) throws IOException {
88 bufferStart = (long) BUFFER_SIZE * (long) currentBufferIndex;
89 if (currentBufferIndex >= file.numBuffers()) {
90 // end of file reached, no more buffers left
92 throw new IOException("Read past EOF");
94 // Force EOF if a read takes place at this position
96 bufferPosition = BUFFER_SIZE;
99 currentBuffer = file.getBuffer(currentBufferIndex);
101 long buflen = length - bufferStart;
102 bufferLength = buflen > BUFFER_SIZE ? BUFFER_SIZE : (int) buflen;
107 public void copyBytes(IndexOutput out, long numBytes) throws IOException {
108 assert numBytes >= 0: "numBytes=" + numBytes;
110 long left = numBytes;
112 if (bufferPosition == bufferLength) {
113 ++currentBufferIndex;
114 switchCurrentBuffer(true);
117 final int bytesInBuffer = bufferLength - bufferPosition;
118 final int toCopy = (int) (bytesInBuffer < left ? bytesInBuffer : left);
119 out.writeBytes(currentBuffer, bufferPosition, toCopy);
120 bufferPosition += toCopy;
124 assert left == 0: "Insufficient bytes to copy: numBytes=" + numBytes + " copied=" + (numBytes - left);
128 public long getFilePointer() {
129 return currentBufferIndex < 0 ? 0 : bufferStart + bufferPosition;
133 public void seek(long pos) throws IOException {
134 if (currentBuffer==null || pos < bufferStart || pos >= bufferStart + BUFFER_SIZE) {
135 currentBufferIndex = (int) (pos / BUFFER_SIZE);
136 switchCurrentBuffer(false);
138 bufferPosition = (int) (pos % BUFFER_SIZE);