This code was simply copied from Apache Commons Codec 1.3.