FileDocCategorySizeDatePackage
RAMOutputStream.javaAPI DocApache Lucene 2.0.02978Fri May 26 09:54:20 BST 2006org.apache.lucene.store

RAMOutputStream

public class RAMOutputStream extends BufferedIndexOutput
A memory-resident {@link IndexOutput} implementation.
version
$Id: RAMOutputStream.java 408916 2006-05-23 15:01:11Z yonik $

Fields Summary
private RAMFile
file
private long
pointer
Constructors Summary
public RAMOutputStream()
Construct an empty output buffer.


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

    file = f;
  
Methods Summary
public voidclose()

    super.close();
  
public voidflushBuffer(byte[] src, int len)

    byte[] buffer;
    int bufferPos = 0;
    while (bufferPos != len) {
      int bufferNumber = (int)(pointer/BUFFER_SIZE);
      int bufferOffset = (int)(pointer%BUFFER_SIZE);
      int bytesInBuffer = BUFFER_SIZE - bufferOffset;
      int remainInSrcBuffer = len - bufferPos;
      int bytesToCopy = bytesInBuffer >= remainInSrcBuffer ? remainInSrcBuffer : bytesInBuffer;

      if (bufferNumber == file.buffers.size()) {
        buffer = new byte[BUFFER_SIZE];
        file.buffers.addElement(buffer);
      } else {
        buffer = (byte[]) file.buffers.elementAt(bufferNumber);
      }

      System.arraycopy(src, bufferPos, buffer, bufferOffset, bytesToCopy);
      bufferPos += bytesToCopy;
      pointer += bytesToCopy;
    }

    if (pointer > file.length)
      file.length = pointer;

    file.lastModified = System.currentTimeMillis();
  
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.length = 0;
  
public voidseek(long pos)

    super.seek(pos);
    pointer = pos;
  
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.elementAt(buffer++), length);
      pos = nextPos;
    }