Skip to content

Commit

Permalink
Default from a stream broken.
Browse files Browse the repository at this point in the history
  • Loading branch information
brett-smith committed Sep 1, 2024
1 parent c56c15a commit 4a25227
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions config/src/main/java/com/sshtools/jini/config/INISet.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ public final static class Builder {
private Optional<Supplier<INIWriter.Builder>> writerFactory = Optional.empty();

private final String name;
public boolean closeDefaultIniStream;

public Builder(String name) {
this.name = name;
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 4a25227

Please sign in to comment.