Skip to content

Commit

Permalink
make resource handling spotbugs friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
xzel23 committed Nov 2, 2024
1 parent 4d9462a commit 9200412
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 24 deletions.
10 changes: 5 additions & 5 deletions utility-db/src/main/java/com/dua3/utility/db/DbUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import javax.sql.DataSource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.UncheckedIOException;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -63,11 +64,10 @@ public final class DbUtil {
static {
try {
// load properties
String resource = "jdbc_drivers.properties";
Map<Object, Object> p = LangUtil.loadProperties(
Objects.requireNonNull(DbUtil.class.getResourceAsStream(resource),
() -> "could not load resource " + resource)
);
Map<Object, Object> p;
try (InputStream in = DbUtil.class.getResourceAsStream("jdbc_drivers.properties")) {
p = LangUtil.loadProperties(in);
}

// parse entries
p.forEach((key1, value) -> {
Expand Down
21 changes: 2 additions & 19 deletions utility/src/main/java/com/dua3/utility/lang/BuildInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.time.ZonedDateTime;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -71,22 +70,6 @@ public static BuildInfo create(Properties properties) {
return create(version, buildTime);
}

/**
* Load properties from stream and return BuildInfo.
*
* @param in {@link InputStream} to read the build info properties from
* @return BuildInfo instance
* @throws IllegalStateException if buildinfo could not be loaded
*/
public static BuildInfo create(InputStream in) {
try (in) {
return create(LangUtil.loadProperties(in));
} catch (Exception e) {
LOG.warn("could not load build properties", e);
throw new IllegalStateException("could not load build properties", e);
}
}

/**
* Load properties from resource and return BuildInfo.
*
Expand All @@ -96,8 +79,8 @@ public static BuildInfo create(InputStream in) {
* @throws IllegalStateException if buildinfo could not be loaded
*/
public static BuildInfo create(Class<?> cls, String resource) throws IOException {
try (InputStream in = Objects.requireNonNull(cls.getResourceAsStream(resource), () -> "could not load resource: " + resource)) {
return create(in);
try (InputStream in = cls.getResourceAsStream(resource)) {
return create(LangUtil.loadProperties(in));
}
}

Expand Down

0 comments on commit 9200412

Please sign in to comment.