Inside the try-with-resources, the code closes in but reads from stream. While they currently reference the same object, this is confusing and easy to break during refactors; read from in to match the managed resource. Also, throwing a bare NoSuchElementException with no message makes debugging missing includes harder—consider including the include path (e.g., pass key into text(...) or throw from file(key) with a message).
InputStream stream = getClass().getResourceAsStream(file);
if (stream == null) {
throw new NoSuchElementException("Resource not found: " + file);
}
return text(stream);
}
private String text(final InputStream stream) {
try (InputStream in = stream) {
return CharStreams.toString(new InputStreamReader(in, StandardCharsets.UTF_8));
} catch (IOException x) {
Originally posted by @Copilot in #194 (comment)
Inside the try-with-resources, the code closes
inbut reads fromstream. While they currently reference the same object, this is confusing and easy to break during refactors; read frominto match the managed resource. Also, throwing a bareNoSuchElementExceptionwith no message makes debugging missing includes harder—consider including the include path (e.g., passkeyintotext(...)or throw fromfile(key)with a message).Originally posted by @Copilot in #194 (comment)