Merge pull request #11779 from rajadilipkolli

* gh-11779:
  Use try-with-resources in HttpTunnelPayload

Closes gh-11779
pull/18281/head
Andy Wilkinson 5 years ago
commit bf56b24ca5

@ -84,11 +84,11 @@ public class HttpTunnelPayload {
headers.setContentLength(this.data.remaining()); headers.setContentLength(this.data.remaining());
headers.add(SEQ_HEADER, Long.toString(getSequence())); headers.add(SEQ_HEADER, Long.toString(getSequence()));
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
WritableByteChannel body = Channels.newChannel(message.getBody()); try (WritableByteChannel body = Channels.newChannel(message.getBody())) {
while (this.data.hasRemaining()) { while (this.data.hasRemaining()) {
body.write(this.data); body.write(this.data);
} }
body.close(); }
} }
/** /**
@ -117,15 +117,15 @@ public class HttpTunnelPayload {
} }
String seqHeader = message.getHeaders().getFirst(SEQ_HEADER); String seqHeader = message.getHeaders().getFirst(SEQ_HEADER);
Assert.state(StringUtils.hasLength(seqHeader), "Missing sequence header"); Assert.state(StringUtils.hasLength(seqHeader), "Missing sequence header");
ReadableByteChannel body = Channels.newChannel(message.getBody()); try (ReadableByteChannel body = Channels.newChannel(message.getBody())) {
ByteBuffer payload = ByteBuffer.allocate((int) length); ByteBuffer payload = ByteBuffer.allocate((int) length);
while (payload.hasRemaining()) { while (payload.hasRemaining()) {
body.read(payload); body.read(payload);
} }
body.close();
payload.flip(); payload.flip();
return new HttpTunnelPayload(Long.valueOf(seqHeader), payload); return new HttpTunnelPayload(Long.valueOf(seqHeader), payload);
} }
}
/** /**
* Return the payload data for the given source {@link ReadableByteChannel} or null if * Return the payload data for the given source {@link ReadableByteChannel} or null if

Loading…
Cancel
Save