Polish "Use try-with-resources to close resources automatically"
- Apply code formatting - Use try-with-resources in many other places that were missed in the pull request Closes gh-8045
This commit is contained in:
@@ -27,8 +27,7 @@ public final class Verify {
|
||||
}
|
||||
|
||||
public static void verify(File file, String entry) throws Exception {
|
||||
ZipFile zipFile = new ZipFile(file);
|
||||
try {
|
||||
try (ZipFile zipFile = new ZipFile(file)) {
|
||||
Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
||||
while (entries.hasMoreElements()) {
|
||||
if (entries.nextElement().getName().equals(entry)) {
|
||||
@@ -37,9 +36,6 @@ public final class Verify {
|
||||
}
|
||||
throw new AssertionError("No entry " + entry);
|
||||
}
|
||||
finally {
|
||||
zipFile.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,14 +97,10 @@ public class SampleIntegrationApplicationTests {
|
||||
}
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (Resource resource : resources) {
|
||||
InputStream inputStream = resource.getInputStream();
|
||||
try {
|
||||
try (InputStream inputStream = resource.getInputStream()) {
|
||||
builder.append(new String(
|
||||
StreamUtils.copyToByteArray(inputStream)));
|
||||
}
|
||||
finally {
|
||||
inputStream.close();
|
||||
}
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
@@ -64,21 +64,14 @@ public class SampleJettyApplicationTests {
|
||||
HttpHeaders requestHeaders = new HttpHeaders();
|
||||
requestHeaders.set("Accept-Encoding", "gzip");
|
||||
HttpEntity<?> requestEntity = new HttpEntity<>(requestHeaders);
|
||||
|
||||
ResponseEntity<byte[]> entity = this.restTemplate.exchange("/", HttpMethod.GET,
|
||||
requestEntity, byte[].class);
|
||||
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
|
||||
GZIPInputStream inflater = new GZIPInputStream(
|
||||
new ByteArrayInputStream(entity.getBody()));
|
||||
try {
|
||||
try (GZIPInputStream inflater = new GZIPInputStream(
|
||||
new ByteArrayInputStream(entity.getBody()))) {
|
||||
assertThat(StreamUtils.copyToString(inflater, Charset.forName("UTF-8")))
|
||||
.isEqualTo("Hello World");
|
||||
}
|
||||
finally {
|
||||
inflater.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -75,15 +75,11 @@ public class SampleTomcatApplicationTests {
|
||||
ResponseEntity<byte[]> entity = this.restTemplate.exchange("/", HttpMethod.GET,
|
||||
requestEntity, byte[].class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
GZIPInputStream inflater = new GZIPInputStream(
|
||||
new ByteArrayInputStream(entity.getBody()));
|
||||
try {
|
||||
try (GZIPInputStream inflater = new GZIPInputStream(
|
||||
new ByteArrayInputStream(entity.getBody()))) {
|
||||
assertThat(StreamUtils.copyToString(inflater, Charset.forName("UTF-8")))
|
||||
.isEqualTo("Hello World");
|
||||
}
|
||||
finally {
|
||||
inflater.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -70,15 +70,12 @@ public class SampleUndertowApplicationTests {
|
||||
ResponseEntity<byte[]> entity = this.restTemplate.exchange("/", HttpMethod.GET,
|
||||
requestEntity, byte[].class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
GZIPInputStream inflater = new GZIPInputStream(
|
||||
new ByteArrayInputStream(entity.getBody()));
|
||||
try {
|
||||
|
||||
try (GZIPInputStream inflater = new GZIPInputStream(
|
||||
new ByteArrayInputStream(entity.getBody()))) {
|
||||
assertThat(StreamUtils.copyToString(inflater, Charset.forName("UTF-8")))
|
||||
.isEqualTo("Hello World");
|
||||
}
|
||||
finally {
|
||||
inflater.close();
|
||||
}
|
||||
}
|
||||
|
||||
private void assertOkResponse(String path, String body) {
|
||||
|
||||
Reference in New Issue
Block a user