def writeBlock(pieceIndex, offset, block)
      regions = @pieceMapper.findBlock(pieceIndex, offset, block.length)
      indexInBlock = 0
      regions.each do |region|
        # Get the IO for the file with path 'path'. If we are being used in a reactor, this is the IO facade. If we
        # are not then this is a real IO.
        io = @ioManager.get(region.path)
        if ! io
          # No IO for this file. 
          raise "This process doesn't have write permission for the file #{region.path}" if File.exists?(region.path) && ! File.writable?(region.path)

          # Ensure parent directories exist.
          dir = File.dirname region.path
          FileUtils.mkdir_p dir if ! File.directory?(dir)

          begin
            io = @ioManager.open(region.path)
          rescue
            @logger.error "Opening file #{region.path} failed: #{$!}"
            raise "Opening file #{region.path} failed"
          end
        end

        io.seek region.offset, IO::SEEK_SET
        begin
          io.write(block[indexInBlock, region.length])
          indexInBlock += region.length
        rescue
          # Error when writing...
          @logger.error "Writing block to file #{region.path} failed: #{$!}"
          piece = nil
          break
        end

        break if indexInBlock >= block.length
      end
    end
def writeBlock(pieceIndex, offset, block)
      regions = @pieceMapper.findBlock(pieceIndex, offset, block.length)
      indexInBlock = 0
      regions.each do |region|
        # Get the IO for the file with path 'path'. If we are being used in a reactor, this is the IO facade. If we
        # are not then this is a real IO.
        io = @ioManager.get(region.path)
        if ! io
          # No IO for this file. 
          raise "This process doesn't have write permission for the file #{region.path}" if File.exists?(region.path) && ! File.writable?(region.path)

          # Ensure parent directories exist.
          dir = File.dirname region.path
          FileUtils.mkdir_p dir if ! File.directory?(dir)

          begin
            io = @ioManager.open(region.path)
          rescue
            @logger.error "Opening file #{region.path} failed: #{$!}"
            raise "Opening file #{region.path} failed"
          end
        end

        io.seek region.offset, IO::SEEK_SET
        begin
          io.write(block[indexInBlock, region.length])
          indexInBlock += region.length
        rescue
          # Error when writing...
          @logger.error "Writing block to file #{region.path} failed: #{$!}"
          piece = nil
          break
        end

        break if indexInBlock >= block.length
      end
    end
Write a block to an in-progress piece. The block is written to
 piece 'peiceIndex', at offset 'offset'. The block data is in block.
 Throws exceptions on failure.