FileDocCategorySizeDatePackage
RAMOutputStream.javaAPI DocApache Lucene 2.1.03135Wed Feb 14 10:46:40 GMT 2007org.apache.lucene.store

RAMOutputStream

public class RAMOutputStream extends BufferedIndexOutput
A memory-resident {@link IndexOutput} implementation.
version
$Id: RAMOutputStream.java 488330 2006-12-18 16:45:29Z mikemccand $

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 = file.addBuffer(BUFFER_SIZE);
      else
        buffer = (byte[]) file.buffers.get(bufferNumber);

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

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

    file.setLastModified(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.setLength(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.get(buffer++), length);
      pos = nextPos;
    }