public void ensureCapacity(long size) {
    long blocks = (size + allocationSize - 1) / allocationSize;
    while (blocks > allocations.size()) {
      MappedFileChannel mfc = null;
      if (baseDirectory != null) {
        final String name = baseDirectory + "/alloc-" + allocations.size();
        RandomAccessFile raf = null;
        try {
          raf = new RandomAccessFile(name, "rw");
          mfChannels.add(mfc = new MappedFileChannel(raf, allocationByteSize));
        } catch (IOException e) {
          try {
            raf.close();
          } catch (IOException ignored) {
          }
          throw new IllegalStateException("Unable to create allocation " + name, e);
        }
      }
      allocations.add(createAllocation(mfc));
    }
  }