From 4a2522798a212a858a5ea700be5e8b12198598eb Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Sun, 1 Sep 2024 10:52:52 +0100 Subject: [PATCH] Default from a stream broken. --- .../java/com/sshtools/jini/config/INISet.java | 39 +++++++++---------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/config/src/main/java/com/sshtools/jini/config/INISet.java b/config/src/main/java/com/sshtools/jini/config/INISet.java index 8b6bdf7..4468669 100644 --- a/config/src/main/java/com/sshtools/jini/config/INISet.java +++ b/config/src/main/java/com/sshtools/jini/config/INISet.java @@ -251,6 +251,7 @@ public final static class Builder { private Optional> writerFactory = Optional.empty(); private final String name; + public boolean closeDefaultIniStream; public Builder(String name) { this.name = name; @@ -322,26 +323,15 @@ public Builder withSchema(Class base, String resource) { } public Builder withDefault(Class base, String resource) { - try (var in = base.getResourceAsStream(resource)) { - return withDefault(in); - } catch (IOException ioe) { - throw new UncheckedIOException(ioe); - } + closeDefaultIniStream = true; + return withDefault(base.getResourceAsStream(resource)); } public Builder withOptionalDefault(Class base, String resource) { - try { - var in = base.getResourceAsStream(resource); - if(in == null) - return this; - try { - return withDefault(in); - } finally { - in.close(); - } - } catch (IOException ioe) { - throw new UncheckedIOException(ioe); - } + var in = base.getResourceAsStream(resource); + if(in == null) + return this; + return withDefault(in); } public Builder withSchema(Path path) { @@ -358,8 +348,8 @@ public Builder withSchema(INISchema schema) { } public Builder withDefault(InputStream in) { - this.defaultIniStream = Optional.of(in); - return this; + this.defaultIniStream = Optional.of(in); + return this; } public Builder withDefault(INI defaultIni) { @@ -474,12 +464,21 @@ private INISet(Builder builder) { this.systemPropertyOverrides = builder.systemPropertyOverrides; this.schema = builder.schema; if(builder.defaultIniStream.isPresent()) { - try(var in = builder.defaultIniStream.get()) { + var in = builder.defaultIniStream.get(); + try { this.defaultIni = Optional.of(readerFactory.map(Supplier::get).orElseGet(INISet::defaultReader).build().read(in)); } catch (IOException e) { throw new UncheckedIOException(e); } catch (ParseException e) { throw new IllegalArgumentException(e); + } finally { + if(builder.closeDefaultIniStream) { + try { + in.close(); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } } } else {