try-with-resources introduced
Code duplicates moved into separate method
StringBuffer replaced with StringBuilder