Skip to content

Commit

Permalink
Storage: fix bug due to lazy stream
Browse files Browse the repository at this point in the history
  • Loading branch information
yisiox committed Feb 13, 2024
1 parent fe5235b commit 13a8ef5
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/main/java/earl/util/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import java.io.FileWriter;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Stream;

import earl.exceptions.EarlException;
Expand Down Expand Up @@ -44,16 +46,14 @@ public <T> Stream<T> load(ParseFunction<T> parse) {
if (isFolderMade || isFileMade) {
return Stream.empty();
}
Stream<T> result = Files.lines(file.toPath())
.map((str) -> {
try {
return parse.apply(str);
} catch (EarlException e) {
throw new RuntimeException(e.getMessage());
}
});
Scanner sc = new Scanner(file);
List<T> result = new ArrayList<>();
while (sc.hasNext()) {
String entry = sc.nextLine();
result.add(parse.apply(entry));
}
wasLoadSuccessful = true;
return result;
return result.stream();
} catch (Exception e) {
return Stream.empty();
}
Expand All @@ -80,7 +80,7 @@ public void save(Stream<String> dataStream) throws EarlException {
throw new UncheckedIOException(e);
}
});
} catch (IOException e) {
} catch (Exception e) {
throw new EarlException("Fatal error while saving to storage.");
}
}
Expand Down

0 comments on commit 13a8ef5

Please sign in to comment.