@Override
 public void close() throws IOException {
   flushBefore(raf.length());
   super.close();
   raf.close();
   file.delete();
 }
  @Override
  public void flushBefore(long pos) throws IOException {
    long readFromPos = flushedPos;
    super.flushBefore(pos);

    long bytesToRead = pos - readFromPos;
    raf.seek(readFromPos);

    if (bytesToRead < MAX_BUFFER_LEN) {
      byte buffer[] = new byte[(int) bytesToRead];
      raf.readFully(buffer);
      os.write(buffer);
    } else {
      byte buffer[] = new byte[MAX_BUFFER_LEN];
      while (bytesToRead > 0) {
        int count = (int) Math.min(MAX_BUFFER_LEN, bytesToRead);
        raf.readFully(buffer, 0, count);
        os.write(buffer, 0, count);
        bytesToRead -= count;
      }
    }

    os.flush();

    if (pos != streamPos) {
      raf.seek(streamPos); // Reset the position
    }
  }
  @Override
  public void flushBefore(long pos) throws IOException {
    long flushedPosition = getFlushedPosition();
    super.flushBefore(pos);

    long newFlushedPosition = getFlushedPosition();
    int nBytes = (int) (newFlushedPosition - flushedPosition);

    ramc.getData(os, nBytes, flushedPosition);
    ramc.freeBefore(newFlushedPosition);

    os.flush();
  }
 @Override
 public void close() throws IOException {
   flushBefore(length());
   super.close();
   ramc.close();
 }