Makes getting lines that are not key value pairs possible
All checks were successful
KnarCraft/KnarLib/pipeline/head This commit looks good

This commit is contained in:
Kristian Knarvik 2024-04-29 05:56:19 +02:00
parent c35325c5a7
commit e9efb7cd34

View File

@ -14,7 +14,9 @@ import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; 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 <p>The file to write to</p> * @param file <p>The file to write to</p>
* @return <p>A buffered writer writing to the file</p> * @return <p>A buffered writer writing to the file</p>
@ -101,10 +103,38 @@ public final class FileHelper {
* @return <p>A map containing the read pairs</p> * @return <p>A map containing the read pairs</p>
* @throws IOException <p>If unable to read from the stream</p> * @throws IOException <p>If unable to read from the stream</p>
*/ */
@NotNull
public static Map<String, String> readKeyValuePairs(@NotNull BufferedReader bufferedReader, public static Map<String, String> readKeyValuePairs(@NotNull BufferedReader bufferedReader,
@NotNull String separator, @NotNull String separator,
@NotNull ColorConversion colorConversion) throws IOException { @NotNull ColorConversion colorConversion) throws IOException {
Map<String, String> readPairs = new HashMap<>(); Map<String, String> readPairs = new HashMap<>();
List<String> 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 <p>The buffered reader to read</p>
* @return <p>A list of the read lines</p>
* @throws IOException <p>If unable to read from the stream</p>
*/
@NotNull
public static List<String> readLines(@NotNull BufferedReader bufferedReader) throws IOException {
List<String> readLines = new ArrayList<>();
String line = bufferedReader.readLine(); String line = bufferedReader.readLine();
boolean firstLine = true; boolean firstLine = true;
@ -115,22 +145,17 @@ public final class FileHelper {
firstLine = false; firstLine = false;
} }
//Split at first separator //Split at first separator
int separatorIndex = line.indexOf(separator); if (line.isEmpty()) {
if (separatorIndex == -1) {
line = bufferedReader.readLine(); line = bufferedReader.readLine();
continue; continue;
} }
//Read the line readLines.add(line);
String key = line.substring(0, separatorIndex);
String value = ColorHelper.translateColorCodes(line.substring(separatorIndex + 1), colorConversion);
readPairs.put(key, value);
line = bufferedReader.readLine(); line = bufferedReader.readLine();
} }
bufferedReader.close(); bufferedReader.close();
return readPairs; return readLines;
} }
/** /**