From e9efb7cd34b4259fab5ccd98254349f4f4df5b52 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Mon, 29 Apr 2024 05:56:19 +0200 Subject: [PATCH] Makes getting lines that are not key value pairs possible --- .../knarcraft/knarlib/util/FileHelper.java | 43 +++++++++++++++---- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/src/main/java/net/knarcraft/knarlib/util/FileHelper.java b/src/main/java/net/knarcraft/knarlib/util/FileHelper.java index 481eed2..5a9e785 100644 --- a/src/main/java/net/knarcraft/knarlib/util/FileHelper.java +++ b/src/main/java/net/knarcraft/knarlib/util/FileHelper.java @@ -14,7 +14,9 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; /** @@ -80,7 +82,7 @@ public final class FileHelper { } /** - * Gets a buffered writer from a string pointing to a file + * Gets a buffered writer from a buffered reader * * @param file

The file to write to

* @return

A buffered writer writing to the file

@@ -101,10 +103,38 @@ public final class FileHelper { * @return

A map containing the read pairs

* @throws IOException

If unable to read from the stream

*/ + @NotNull public static Map readKeyValuePairs(@NotNull BufferedReader bufferedReader, @NotNull String separator, @NotNull ColorConversion colorConversion) throws IOException { Map readPairs = new HashMap<>(); + List lines = readLines(bufferedReader); + + for (String line : lines) { + int separatorIndex = line.indexOf(separator); + if (separatorIndex == -1) { + continue; + } + + //Read the line + String key = line.substring(0, separatorIndex); + String value = ColorHelper.translateColorCodes(line.substring(separatorIndex + 1), colorConversion); + readPairs.put(key, value); + } + + return readPairs; + } + + /** + * Reads all lines from a buffered reader + * + * @param bufferedReader

The buffered reader to read

+ * @return

A list of the read lines

+ * @throws IOException

If unable to read from the stream

+ */ + @NotNull + public static List readLines(@NotNull BufferedReader bufferedReader) throws IOException { + List readLines = new ArrayList<>(); String line = bufferedReader.readLine(); boolean firstLine = true; @@ -115,22 +145,17 @@ public final class FileHelper { firstLine = false; } //Split at first separator - int separatorIndex = line.indexOf(separator); - if (separatorIndex == -1) { + if (line.isEmpty()) { line = bufferedReader.readLine(); continue; } - //Read the line - String key = line.substring(0, separatorIndex); - String value = ColorHelper.translateColorCodes(line.substring(separatorIndex + 1), colorConversion); - readPairs.put(key, value); - + readLines.add(line); line = bufferedReader.readLine(); } bufferedReader.close(); - return readPairs; + return readLines; } /**