diff --git a/powerfactory/powerfactory-model/src/main/java/com/powsybl/powerfactory/model/StudyCaseLoader.java b/powerfactory/powerfactory-model/src/main/java/com/powsybl/powerfactory/model/StudyCaseLoader.java index 5beede21e73..cde1212d92c 100644 --- a/powerfactory/powerfactory-model/src/main/java/com/powsybl/powerfactory/model/StudyCaseLoader.java +++ b/powerfactory/powerfactory-model/src/main/java/com/powsybl/powerfactory/model/StudyCaseLoader.java @@ -35,9 +35,16 @@ static Optional load(Path file) { static Optional load(String fileName, Supplier inputStreamSupplier) { Objects.requireNonNull(inputStreamSupplier); for (StudyCaseLoader studyCaseLoader : ServiceLoader.load(StudyCaseLoader.class)) { - if (fileName.endsWith(studyCaseLoader.getExtension()) && - studyCaseLoader.test(inputStreamSupplier.get())) { - return Optional.of(studyCaseLoader.doLoad(fileName, inputStreamSupplier.get())); + if (fileName.endsWith(studyCaseLoader.getExtension())) { + try (var testIs = inputStreamSupplier.get()) { + if (studyCaseLoader.test(testIs)) { + try (var loadIs = inputStreamSupplier.get()) { + return Optional.of(studyCaseLoader.doLoad(fileName, loadIs)); + } + } + } catch (IOException e) { + throw new UncheckedIOException(e); + } } } return Optional.empty();