86 lines
2.8 KiB
Java
86 lines
2.8 KiB
Java
import java.io.BufferedReader;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
public class ConfigReader {
|
|
|
|
public static DownloadConfig readConfig(String fileName) {
|
|
InputStream inputStream = ConfigReader.class.getClassLoader().getResourceAsStream(fileName);
|
|
|
|
if (inputStream == null) {
|
|
throw new IllegalArgumentException("Config file not found in resources: " + fileName);
|
|
}
|
|
|
|
String configFileName = null;
|
|
int totalSizeMB = 0;
|
|
int chunkCount = 0;
|
|
int minStepDelayMs = 0;
|
|
int maxStepDelayMs = 0;
|
|
double minStepDownloadMB = 0;
|
|
double maxStepDownloadMB = 0;
|
|
|
|
try (BufferedReader reader = new BufferedReader(
|
|
new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
|
|
|
|
String line;
|
|
while ((line = reader.readLine()) != null) {
|
|
line = line.trim();
|
|
|
|
if (line.isEmpty()) {
|
|
continue;
|
|
}
|
|
|
|
String[] parts = line.split("=", 2);
|
|
if (parts.length != 2) {
|
|
continue;
|
|
}
|
|
|
|
String key = parts[0].trim();
|
|
String value = parts[1].trim();
|
|
|
|
switch (key) {
|
|
case "fileName":
|
|
configFileName = value;
|
|
break;
|
|
case "totalSizeMB":
|
|
totalSizeMB = Integer.parseInt(value);
|
|
break;
|
|
case "chunkCount":
|
|
chunkCount = Integer.parseInt(value);
|
|
break;
|
|
case "minStepDelayMs":
|
|
minStepDelayMs = Integer.parseInt(value);
|
|
break;
|
|
case "maxStepDelayMs":
|
|
maxStepDelayMs = Integer.parseInt(value);
|
|
break;
|
|
case "minStepDownloadMB":
|
|
minStepDownloadMB = Double.parseDouble(value);
|
|
break;
|
|
case "maxStepDownloadMB":
|
|
maxStepDownloadMB = Double.parseDouble(value);
|
|
break;
|
|
default:
|
|
// Ignore unknown keys to keep parsing simple
|
|
break;
|
|
}
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
throw new RuntimeException("Error reading config file: " + fileName, e);
|
|
}
|
|
|
|
return new DownloadConfig(
|
|
configFileName,
|
|
totalSizeMB,
|
|
chunkCount,
|
|
minStepDelayMs,
|
|
maxStepDelayMs,
|
|
minStepDownloadMB,
|
|
maxStepDownloadMB
|
|
);
|
|
}
|
|
}
|