Use key.getLength() everywhere here.