Use try-with-resources statements

- instead of finally blocks
- in cases of missing try-finally

Change-Id: I94f481a33d8e6a3180c436245d6e95e4d525280c
This commit is contained in:
Urs Wolfer
2015-06-11 21:44:59 +02:00
parent b17ceb8587
commit 5e90c63dea
112 changed files with 727 additions and 1332 deletions

View File

@@ -522,8 +522,7 @@ public class RestApiServlet extends HttpServlet {
IllegalArgumentException, NoSuchMethodException, IllegalAccessException,
InstantiationException, InvocationTargetException, MethodNotAllowedException {
if (isType(JSON_TYPE, req.getContentType())) {
BufferedReader br = req.getReader();
try {
try (BufferedReader br = req.getReader()) {
JsonReader json = new JsonReader(br);
json.setLenient(true);
@@ -537,8 +536,6 @@ public class RestApiServlet extends HttpServlet {
return parseString(json.nextString(), type);
}
return OutputFormat.JSON.newGson().fromJson(json, type);
} finally {
br.close();
}
} else if (("PUT".equals(req.getMethod()) || "POST".equals(req.getMethod()))
&& acceptsRawInput(type)) {
@@ -548,8 +545,7 @@ public class RestApiServlet extends HttpServlet {
} else if (hasNoBody(req)) {
return createInstance(type);
} else if (isType("text/plain", req.getContentType())) {
BufferedReader br = req.getReader();
try {
try (BufferedReader br = req.getReader()) {
char[] tmp = new char[256];
StringBuilder sb = new StringBuilder();
int n;
@@ -557,8 +553,6 @@ public class RestApiServlet extends HttpServlet {
sb.append(tmp, 0, n);
}
return parseString(sb.toString(), type);
} finally {
br.close();
}
} else if ("POST".equals(req.getMethod())
&& isType(FORM_TYPE, req.getContentType())) {
@@ -772,11 +766,8 @@ public class RestApiServlet extends HttpServlet {
}
if (req == null || !"HEAD".equals(req.getMethod())) {
OutputStream dst = res.getOutputStream();
try {
try (OutputStream dst = res.getOutputStream()) {
bin.writeTo(dst);
} finally {
dst.close();
}
}
} finally {
@@ -1078,9 +1069,9 @@ public class RestApiServlet extends HttpServlet {
private static BinaryResult compress(BinaryResult bin)
throws IOException {
TemporaryBuffer.Heap buf = heap(HEAP_EST_SIZE, 20 << 20);
GZIPOutputStream gz = new GZIPOutputStream(buf);
bin.writeTo(gz);
gz.close();
try (GZIPOutputStream gz = new GZIPOutputStream(buf)) {
bin.writeTo(gz);
}
return asBinaryResult(buf).setContentType(bin.getContentType());
}