fix: long files not read correctly

dev
Exlll 7 years ago
parent 108133abc0
commit e4f604a2cd

@ -10,7 +10,11 @@ enum ConfigReader {
static String read(Path path) throws IOException { static String read(Path path) throws IOException {
try (Scanner scanner = new Scanner(path)) { try (Scanner scanner = new Scanner(path)) {
scanner.useDelimiter("\\z"); scanner.useDelimiter("\\z");
return scanner.next(); StringBuilder builder = new StringBuilder();
while (scanner.hasNext()) {
builder.append(scanner.next());
}
return builder.toString();
} }
} }
} }

@ -16,8 +16,8 @@ import java.util.Map;
public abstract class Configuration { public abstract class Configuration {
private final Path configPath; private final Path configPath;
private final YamlSerializer serializer;
private final CommentAdder adder; private final CommentAdder adder;
private YamlSerializer serializer;
/** /**
* Constructs a new {@code Configuration} instance. * Constructs a new {@code Configuration} instance.
@ -30,13 +30,18 @@ public abstract class Configuration {
*/ */
protected Configuration(Path configPath) { protected Configuration(Path configPath) {
this.configPath = configPath; this.configPath = configPath;
this.serializer = new YamlSerializer(
createConstructor(), createRepresenter(),
createDumperOptions(), createResolver()
);
this.adder = new CommentAdder(new Comments(getClass())); this.adder = new CommentAdder(new Comments(getClass()));
} }
private void initSerializer() {
if (serializer == null) {
this.serializer = new YamlSerializer(
createConstructor(), createRepresenter(),
createDumperOptions(), createResolver()
);
}
}
/** /**
* Loads {@code this} configuration from a configuration file. The file is * Loads {@code this} configuration from a configuration file. The file is
* located at the path pointed to by the {@code Path} object used to create * located at the path pointed to by the {@code Path} object used to create
@ -62,6 +67,7 @@ public abstract class Configuration {
} }
private Map<String, Object> readAndDeserialize() throws IOException { private Map<String, Object> readAndDeserialize() throws IOException {
initSerializer();
String yaml = ConfigReader.read(configPath); String yaml = ConfigReader.read(configPath);
return serializer.deserialize(yaml); return serializer.deserialize(yaml);
} }
@ -100,6 +106,7 @@ public abstract class Configuration {
* @throws ParserException if invalid YAML * @throws ParserException if invalid YAML
*/ */
public final void save() throws IOException { public final void save() throws IOException {
initSerializer();
createParentDirectories(); createParentDirectories();
Map<String, Object> map = FieldMapper.instanceToMap(this); Map<String, Object> map = FieldMapper.instanceToMap(this);
version(map); version(map);

Loading…
Cancel
Save