FileDocCategorySizeDatePackage
RAMInputStream.javaAPI DocApache Lucene 2.2.03325Sat Jun 16 22:20:36 BST 2007org.apache.lucene.store

RAMInputStream

public class RAMInputStream extends IndexInput implements Cloneable
A memory-resident {@link IndexInput} implementation.
version
$Id: RAMInputStream.java 542561 2007-05-29 15:14:07Z mikemccand $

Fields Summary
static final int
BUFFER_SIZE
private RAMFile
file
private long
length
private byte[]
currentBuffer
private int
currentBufferIndex
private int
bufferPosition
private long
bufferStart
private int
bufferLength
Constructors Summary
public RAMInputStream(RAMFile f)


     
    file = f;
    length = file.length;

    // make sure that we switch to the
    // first needed buffer lazily
    currentBufferIndex = -1;
    currentBuffer = null;
  
Methods Summary
public voidclose()

    // nothing to do here
  
public longgetFilePointer()

    return currentBufferIndex < 0 ? 0 : bufferStart + bufferPosition;
  
public longlength()

    return length;
  
public bytereadByte()

    if (bufferPosition >= bufferLength) {
      currentBufferIndex++;
      switchCurrentBuffer();
    }
    return currentBuffer[bufferPosition++];
  
public voidreadBytes(byte[] b, int offset, int len)

    while (len > 0) {
      if (bufferPosition >= bufferLength) {
        currentBufferIndex++;
        switchCurrentBuffer();
      }

      int remainInBuffer = bufferLength - bufferPosition;
      int bytesToCopy = len < remainInBuffer ? len : remainInBuffer;
      System.arraycopy(currentBuffer, bufferPosition, b, offset, bytesToCopy);
      offset += bytesToCopy;
      len -= bytesToCopy;
      bufferPosition += bytesToCopy;
    }
  
public voidseek(long pos)

    long bufferStart = currentBufferIndex * BUFFER_SIZE;
    if (pos < bufferStart || pos >= bufferStart + BUFFER_SIZE) {
      currentBufferIndex = (int) (pos / BUFFER_SIZE);
      switchCurrentBuffer();
    }
    bufferPosition = (int) (pos % BUFFER_SIZE);
  
private final voidswitchCurrentBuffer()

    if (currentBufferIndex >= file.buffers.size()) {
      // end of file reached, no more buffers left
      throw new IOException("Read past EOF");
    } else {
      currentBuffer = (byte[]) file.buffers.get(currentBufferIndex);
      bufferPosition = 0;
      bufferStart = BUFFER_SIZE * currentBufferIndex;
      bufferLength = (int) (length - bufferStart);
      if (bufferLength > BUFFER_SIZE) {
        bufferLength = BUFFER_SIZE;
      }
    }