FileDocCategorySizeDatePackage
MockRAMOutputStream.javaAPI DocApache Lucene 2.2.02859Sat Jun 16 22:20:24 BST 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
byte[]
singleByte
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 voidwriteByte(byte b)

    singleByte[0] = b;
    writeBytes(singleByte, 0, 1);
  
public voidwriteBytes(byte[] b, int offset, 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.writeBytes(b, offset, (int) freeSpace);
      }
      if (realUsage > dir.maxUsedSize) {
        dir.maxUsedSize = realUsage;
      }
      throw new IOException("fake disk full at " + dir.getRecomputedActualSizeInBytes() + " bytes");
    } else {
      super.writeBytes(b, offset, len);
    }

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