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); } /**