From abcff3a3b382f227f257b750415975c6694d4cb2 Mon Sep 17 00:00:00 2001 From: rajadilipkolli Date: Fri, 26 Jan 2018 00:25:38 +0530 Subject: [PATCH] Use try-with-resources in HttpTunnelPayload See gh-11779 --- .../tunnel/payload/HttpTunnelPayload.java | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/tunnel/payload/HttpTunnelPayload.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/tunnel/payload/HttpTunnelPayload.java index 68e0f8ea6e..b575c6f429 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/tunnel/payload/HttpTunnelPayload.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/tunnel/payload/HttpTunnelPayload.java @@ -84,11 +84,11 @@ public class HttpTunnelPayload { headers.setContentLength(this.data.remaining()); headers.add(SEQ_HEADER, Long.toString(getSequence())); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); - WritableByteChannel body = Channels.newChannel(message.getBody()); - while (this.data.hasRemaining()) { - body.write(this.data); + try (WritableByteChannel body = Channels.newChannel(message.getBody())) { + while (this.data.hasRemaining()) { + body.write(this.data); + } } - body.close(); } /** @@ -117,14 +117,14 @@ public class HttpTunnelPayload { } String seqHeader = message.getHeaders().getFirst(SEQ_HEADER); Assert.state(StringUtils.hasLength(seqHeader), "Missing sequence header"); - ReadableByteChannel body = Channels.newChannel(message.getBody()); - ByteBuffer payload = ByteBuffer.allocate((int) length); - while (payload.hasRemaining()) { - body.read(payload); + try (ReadableByteChannel body = Channels.newChannel(message.getBody())) { + ByteBuffer payload = ByteBuffer.allocate((int) length); + while (payload.hasRemaining()) { + body.read(payload); + } + payload.flip(); + return new HttpTunnelPayload(Long.valueOf(seqHeader), payload); } - body.close(); - payload.flip(); - return new HttpTunnelPayload(Long.valueOf(seqHeader), payload); } /**