Fix resource leak in HubStatusServlet
package org.openqa.grid.web.servlet;
+import com.google.common.io.CharStreams;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
@@ -147,18 +148,16 @@ private JsonObject getSlotCounts() {
}
private JsonObject getRequestJSON(HttpServletRequest request) throws IOException {
- JsonObject requestJSON = null;
- BufferedReader rd = new BufferedReader(new InputStreamReader(request.getInputStream()));
+ JsonObject requestJSON = new JsonObject();
+
+ try (BufferedReader rd = new BufferedReader(new InputStreamReader(request.getInputStream()))) {
StringBuilder s = new StringBuilder();
- String line;
- while ((line = rd.readLine()) != null) {
- s.append(line);
- }
- rd.close();
+ CharStreams.copy(rd, s);
String json = s.toString();
if (!"".equals(json)) {
requestJSON = new JsonParser().parse(json).getAsJsonObject();
}
+ }
return requestJSON;
}
}
