diff --git a/src/main/java/com/welovecoding/nbeditorconfig/io/reader/FileInfoReader.java b/src/main/java/com/welovecoding/nbeditorconfig/io/reader/FileInfoReader.java index 893d5d9..b1745ec 100644 --- a/src/main/java/com/welovecoding/nbeditorconfig/io/reader/FileInfoReader.java +++ b/src/main/java/com/welovecoding/nbeditorconfig/io/reader/FileInfoReader.java @@ -174,18 +174,7 @@ public static String trimTrailingWhitespace(Collection lines, String lin } public static String trimTrailingWhitespace(String text, String lineEnding) { - List lines = new ArrayList<>(); - { - BufferedReader reader = new BufferedReader(new StringReader(text)); - try { - for (String line = reader.readLine(); line != null; line = reader.readLine()) { - lines.add(line); - } - reader.close(); - } catch (IOException ex) { - Exceptions.printStackTrace(ex); - } - } + List lines = readLines(text); return trimTrailingWhitespace(lines, lineEnding); } @@ -199,9 +188,14 @@ public static String replaceLineEndings(Collection lines, String lineEnd } public static String replaceLineEndings(String text, String lineEnding) { + List lines = readLines(text); + return replaceLineEndings(lines, lineEnding); + } + + public static List readLines(String text) { List lines = new ArrayList<>(); - { - BufferedReader reader = new BufferedReader(new StringReader(text)); + try (BufferedReader reader = new BufferedReader(new StringReader(text))) { + try { for (String line = reader.readLine(); line != null; line = reader.readLine()) { lines.add(line); @@ -210,7 +204,9 @@ public static String replaceLineEndings(String text, String lineEnding) { } catch (IOException ex) { Exceptions.printStackTrace(ex); } + } catch (IOException ex) { + Exceptions.printStackTrace(ex); } - return replaceLineEndings(lines, lineEnding); + return lines; } }