FileDocCategorySizeDatePackage
MockRAMOutputStream.javaAPI DocApache Lucene 2.1.02664Wed Feb 14 10:46:34 GMT 2007org.apache.lucene.store

MockRAMOutputStream

public class MockRAMOutputStream extends RAMOutputStream
Used by MockRAMDirectory to create an output stream that will throw an IOException on fake disk full, track max disk space actually used, and maybe throw random IOExceptions.

Fields Summary
private MockRAMDirectory
dir
private boolean
first
Constructors Summary
public MockRAMOutputStream(MockRAMDirectory dir, RAMFile f)
Construct an empty output buffer.


        
       
    super(f);
    this.dir = dir;
  
Methods Summary
public voidclose()

    super.close();

    // Now compute actual disk usage & track the maxUsedSize
    // in the MockRAMDirectory:
    long size = dir.getRecomputedActualSizeInBytes();
    if (size > dir.maxUsedSize) {
      dir.maxUsedSize = size;
    }
  
public voidflushBuffer(byte[] src, int len)

    long freeSpace = dir.maxSize - dir.sizeInBytes();
    long realUsage = 0;

    // Enforce disk full:
    if (dir.maxSize != 0 && freeSpace <= len) {
      // Compute the real disk free.  This will greatly slow
      // down our test but makes it more accurate:
      realUsage = dir.getRecomputedActualSizeInBytes();
      freeSpace = dir.maxSize - realUsage;
    }

    if (dir.maxSize != 0 && freeSpace <= len) {
      if (freeSpace > 0 && freeSpace < len) {
        realUsage += freeSpace;
        super.flushBuffer(src, (int) freeSpace);
      }
      if (realUsage > dir.maxUsedSize) {
        dir.maxUsedSize = realUsage;
      }
      throw new IOException("fake disk full at " + dir.sizeInBytes() + " bytes");
    } else {
      super.flushBuffer(src, len);
    }

    if (first) {
      // Maybe throw random exception; only do this on first
      // write to a new file:
      first = false;
      dir.maybeThrowIOException();
    }