Skip to content

Commit

Permalink
Create InputStreams in try-with-resources
Browse files Browse the repository at this point in the history
JsonUtil.getJsonObject closes the Readers, but not the InputStream.
It is the caller's responsibility to close the InputStream properly.
  • Loading branch information
bencomp committed Oct 30, 2023
1 parent 785964d commit 47ea303
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import edu.harvard.iq.dataverse.util.json.JsonUtil;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
Expand Down Expand Up @@ -150,7 +151,10 @@ public Response updateCitationsForDataset(@PathParam("id") String id) throws IOE
logger.warning("Failed to get citations from " + url.toString());
return error(Status.fromStatusCode(status), "Failed to get citations from " + url.toString());
}
JsonObject report = JsonUtil.getJsonObject(connection.getInputStream());
JsonObject report;
try (InputStream inStream = connection.getInputStream()) {
report = JsonUtil.getJsonObject(inStream);
}
JsonObject links = report.getJsonObject("links");
JsonArray data = report.getJsonArray("data");
Iterator<JsonValue> iter = data.iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ public JsonObject execute(CommandContext ctxt) throws CommandException {

try {
StorageIO<DataFile> dataAccess = dataFile.getStorageIO();
InputStream inputStream = dataAccess.getAuxFileAsInputStream(provJsonExtension);
JsonObject jsonObject = null;
if (null != inputStream) {
jsonObject = JsonUtil.getJsonObject(inputStream);
try (InputStream inputStream = dataAccess.getAuxFileAsInputStream(provJsonExtension)) {
JsonObject jsonObject = null;
if (null != inputStream) {
jsonObject = JsonUtil.getJsonObject(inputStream);
}
return jsonObject;
}
return jsonObject;
} catch (IOException ex) {
String error = "Exception caught in DataAccess.getStorageIO(dataFile) getting file. Error: " + ex;
throw new IllegalCommandException(error, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public static JsonObject getJsonObject(String serializedJson) {

/**
* Return the contents of the {@link InputStream} as a JSON object.
*
* The caller of this method is responsible for closing the provided stream.
* @param stream the input stream to read from
* @throws JsonException when parsing fails.
*/
Expand Down

0 comments on commit 47ea303

Please sign in to comment.