Browse Source

Fix unclosed resource warning in SmartOutputStream

Change-Id: Ia4b96ae1c2cc9357802487384ee32a80ed40334b
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
stable-5.1
Matthias Sohn 5 years ago
parent
commit
4209a0f84b
  1. 30
      org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/SmartOutputStream.java

30
org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/SmartOutputStream.java

@ -105,17 +105,16 @@ class SmartOutputStream extends TemporaryBuffer {
// If output hasn't started yet, the entire thing fit into our // If output hasn't started yet, the entire thing fit into our
// buffer. Try to use a proper Content-Length header, and also // buffer. Try to use a proper Content-Length header, and also
// deflate the response with gzip if it will be smaller. // deflate the response with gzip if it will be smaller.
TemporaryBuffer out = this; if (256 < this.length() && acceptsGzipEncoding(req)) {
if (256 < out.length() && acceptsGzipEncoding(req)) {
TemporaryBuffer gzbuf = new TemporaryBuffer.Heap(LIMIT); TemporaryBuffer gzbuf = new TemporaryBuffer.Heap(LIMIT);
try { try {
try (GZIPOutputStream gzip = new GZIPOutputStream(gzbuf)) { try (GZIPOutputStream gzip = new GZIPOutputStream(gzbuf)) {
out.writeTo(gzip, null); this.writeTo(gzip, null);
} }
if (gzbuf.length() < out.length()) { if (gzbuf.length() < this.length()) {
out = gzbuf;
rsp.setHeader(HDR_CONTENT_ENCODING, ENCODING_GZIP); rsp.setHeader(HDR_CONTENT_ENCODING, ENCODING_GZIP);
writeResponse(gzbuf);
return;
} }
} catch (IOException err) { } catch (IOException err) {
// Most likely caused by overflowing the buffer, meaning // Most likely caused by overflowing the buffer, meaning
@ -123,15 +122,18 @@ class SmartOutputStream extends TemporaryBuffer {
// copy and use the original. // copy and use the original.
} }
} }
writeResponse(this);
}
}
// The Content-Length cannot overflow when cast to an int, our private void writeResponse(TemporaryBuffer out) throws IOException {
// hardcoded LIMIT constant above assures us we wouldn't store // The Content-Length cannot overflow when cast to an int, our
// more than 2 GiB of content in memory. // hardcoded LIMIT constant above assures us we wouldn't store
rsp.setContentLength((int) out.length()); // more than 2 GiB of content in memory.
try (OutputStream os = rsp.getOutputStream()) { rsp.setContentLength((int) out.length());
out.writeTo(os, null); try (OutputStream os = rsp.getOutputStream()) {
os.flush(); out.writeTo(os, null);
} os.flush();
} }
} }
} }

Loading…
Cancel
Save