Fix JRuby memory exhaustion vulnerability

This pull request fixes JRuby memory exhaustion vulnerability which may lead to DoS attack. 

It is very similar to the one described here:
https://groups.google.com/forum/#!msg/ruby-security-ann/DeJpjTAg1FA/CADdUQ6N_qMJ