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

RAMOutputStream

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

Fields Summary
static final int
BUFFER_SIZE
private RAMFile
file
private byte[]
currentBuffer
private int
currentBufferIndex
private int
bufferPosition
private long
bufferStart
private int
bufferLength
Constructors Summary
public RAMOutputStream()
Construct an empty output buffer.


        
    
    this(new RAMFile());
  
RAMOutputStream(RAMFile f)

    file = f;

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

    flush();
  
public voidflush()

    file.setLastModified(System.currentTimeMillis());
    setFileLength();
  
public longgetFilePointer()

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

    return file.length;
  
public voidreset()
Resets this to an empty buffer.

    try {
      seek(0);
    } catch (IOException e) {                     // should never happen
      throw new RuntimeException(e.toString());
    }

    file.setLength(0);
  
public voidseek(long pos)

    // set the file length in case we seek back
    // and flush() has not been called yet
    setFileLength();
    if (pos < bufferStart || pos >= bufferStart + bufferLength) {
      currentBufferIndex = (int) (pos / BUFFER_SIZE);
      switchCurrentBuffer();
    }

    bufferPosition = (int) (pos % BUFFER_SIZE);
  
private voidsetFileLength()

    long pointer = bufferStart + bufferPosition;
    if (pointer > file.length) {
      file.setLength(pointer);
    }
  
private final voidswitchCurrentBuffer()

    if (currentBufferIndex == file.buffers.size()) {
      currentBuffer = file.addBuffer(BUFFER_SIZE);
    } else {
      currentBuffer = (byte[]) file.buffers.get(currentBufferIndex);
    }
    bufferPosition = 0;
    bufferStart = BUFFER_SIZE * currentBufferIndex;
    bufferLength = currentBuffer.length;
  
public voidwriteByte(byte b)

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

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

      int remainInBuffer = currentBuffer.length - bufferPosition;
      int bytesToCopy = len < remainInBuffer ? len : remainInBuffer;
      System.arraycopy(b, offset, currentBuffer, bufferPosition, bytesToCopy);
      offset += bytesToCopy;
      len -= bytesToCopy;
      bufferPosition += bytesToCopy;
    }
  
public voidwriteTo(org.apache.lucene.store.IndexOutput out)
Copy the current contents of this buffer to the named output.

    flush();
    final long end = file.length;
    long pos = 0;
    int buffer = 0;
    while (pos < end) {
      int length = BUFFER_SIZE;
      long nextPos = pos + length;
      if (nextPos > end) {                        // at the last buffer
        length = (int)(end - pos);
      }
      out.writeBytes((byte[])file.buffers.get(buffer++), length);
      pos = nextPos;
    }