Fix remaining try-with-resources warnings

Many of these could be fixed by extracting the conditional object
creation into a method and using that from the try header. One, a lazy
creation of a RevWalk, just does not work with this pattern, so
suppressing the warning is the best we can do.

Change-Id: I7cf2706d282f1851dcf6e1a78ef89eb87a0e8c54
This commit is contained in:
Dave Borowitz
2015-08-28 09:19:48 -04:00
parent b8336f176a
commit 57d5186ab6
7 changed files with 60 additions and 57 deletions

View File

@@ -77,14 +77,15 @@ public class CssLinker extends AbstractLinker {
private String name(final TreeLogger logger, final PublicResource r)
throws UnableToCompleteException {
final ByteArrayOutputStream tmp = new ByteArrayOutputStream();
try (InputStream in = r.getContents(logger)) {
byte[] out;
try (ByteArrayOutputStream tmp = new ByteArrayOutputStream();
InputStream in = r.getContents(logger)) {
final byte[] buf = new byte[2048];
int n;
while ((n = in.read(buf)) >= 0) {
tmp.write(buf, 0, n);
}
tmp.close();
out = tmp.toByteArray();
} catch (IOException e) {
final UnableToCompleteException ute = new UnableToCompleteException();
ute.initCause(e);
@@ -98,7 +99,7 @@ public class CssLinker extends AbstractLinker {
} else {
base = "";
}
return base + Util.computeStrongName(tmp.toByteArray()) + ".cache.css";
return base + Util.computeStrongName(out) + ".cache.css";
}
private static class CssPubRsrc extends PublicResource {

View File

@@ -49,6 +49,7 @@ import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.util.NB;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
@@ -248,10 +249,21 @@ public class CatServlet extends HttpServlet {
rsp.setDateHeader("Last-Modified", when);
CacheHeaders.setNotCacheable(rsp);
OutputStream out;
ZipOutputStream zo;
try (OutputStream out = openOutputStream(
req, rsp, blobLoader, fromCommit, when, path, suffix, raw)) {
if (raw != null) {
out.write(raw);
} else {
blobLoader.copyTo(out);
}
}
}
final MimeType contentType = registry.getMimeType(path, raw);
private OutputStream openOutputStream(HttpServletRequest req,
HttpServletResponse rsp, ObjectLoader blobLoader,
RevCommit fromCommit, long when, String path, String suffix, byte[] raw)
throws IOException {
MimeType contentType = registry.getMimeType(path, raw);
if (!registry.isSafeInline(contentType)) {
// The content may not be safe to transmit inline, as a browser might
// interpret it as HTML or JavaScript hosted by this site. Such code
@@ -266,33 +278,30 @@ public class CatServlet extends HttpServlet {
rsp.setHeader("Content-Disposition", "attachment; filename=\""
+ safeFileName(path, suffix) + ".zip" + "\"");
zo = new ZipOutputStream(rsp.getOutputStream());
final ZipOutputStream zo = new ZipOutputStream(rsp.getOutputStream());
final ZipEntry e = new ZipEntry(safeFileName(path, rand(req, suffix)));
ZipEntry e = new ZipEntry(safeFileName(path, rand(req, suffix)));
e.setComment(fromCommit.name() + ":" + path);
e.setSize(blobLoader.getSize());
e.setTime(when);
zo.putNextEntry(e);
out = zo;
return new FilterOutputStream(zo) {
@Override
public void close() throws IOException {
try {
zo.closeEntry();
} finally {
super.close();
}
}
};
} else {
rsp.setContentType(contentType.toString());
rsp.setHeader("Content-Length", "" + blobLoader.getSize());
out = rsp.getOutputStream();
zo = null;
return rsp.getOutputStream();
}
if (raw != null) {
out.write(raw);
} else {
blobLoader.copyTo(out);
}
if (zo != null) {
zo.closeEntry();
}
out.close();
}
private static String safeFileName(String fileName, final String suffix) {

View File

@@ -83,13 +83,6 @@ public class FileContentUtil {
raw = null;
}
BinaryResult result;
if (raw != null) {
result = BinaryResult.create(raw);
} else {
result = asBinaryResult(obj);
}
String type;
if (mode == org.eclipse.jgit.lib.FileMode.SYMLINK) {
type = X_GIT_SYMLINK;
@@ -97,11 +90,16 @@ public class FileContentUtil {
type = registry.getMimeType(path, raw).toString();
type = resolveContentType(project, path, FileMode.FILE, type);
}
return result.setContentType(type).base64();
return asBinaryResult(raw, obj).setContentType(type).base64();
}
}
private static BinaryResult asBinaryResult(final ObjectLoader obj) {
private static BinaryResult asBinaryResult(byte[] raw,
final ObjectLoader obj) {
if (raw != null) {
return BinaryResult.create(raw);
}
BinaryResult result = new BinaryResult() {
@Override
public void writeTo(OutputStream os) throws IOException {

View File

@@ -76,6 +76,7 @@ class TagSet {
}
void prepare(TagMatcher m) {
@SuppressWarnings("resource")
RevWalk rw = null;
try {
for (Ref currentRef : m.include) {

View File

@@ -391,17 +391,14 @@ public class PatchListLoader implements Callable<PatchList> {
Map<String, ObjectId> resolved = new HashMap<>();
for (Map.Entry<String, MergeResult<? extends Sequence>> entry : r.entrySet()) {
MergeResult<? extends Sequence> p = entry.getValue();
TemporaryBuffer buf =
new TemporaryBuffer.LocalFile(null, 10 * 1024 * 1024);
try {
try (TemporaryBuffer buf =
new TemporaryBuffer.LocalFile(null, 10 * 1024 * 1024)) {
fmt.formatMerge(buf, p, "BASE", oursName, theirsName, "UTF-8");
buf.close();
try (InputStream in = buf.openInputStream()) {
resolved.put(entry.getKey(), ins.insert(Constants.OBJ_BLOB, buf.length(), in));
}
} finally {
buf.destroy();
}
}

View File

@@ -59,20 +59,8 @@ class InstallPlugin implements RestModifyView<TopLevelResource, Input> {
throw new MethodNotAllowedException("remote installation is disabled");
}
try {
InputStream in;
if (input.raw != null) {
in = input.raw.getInputStream();
} else {
try {
in = new URL(input.url).openStream();
} catch (IOException e) {
throw new BadRequestException(e.getMessage());
}
}
try {
try (InputStream in = openStream(input)) {
loader.installPluginFromStream(name, in);
} finally {
in.close();
}
} catch (PluginInstallException e) {
StringWriter buf = new StringWriter();
@@ -93,6 +81,18 @@ class InstallPlugin implements RestModifyView<TopLevelResource, Input> {
return created ? Response.created(info) : Response.ok(info);
}
private InputStream openStream(Input input)
throws IOException, BadRequestException {
if (input.raw != null) {
return input.raw.getInputStream();
}
try {
return new URL(input.url).openStream();
} catch (IOException e) {
throw new BadRequestException(e.getMessage());
}
}
@RequiresCapability(GlobalCapability.ADMINISTRATE_SERVER)
static class Overwrite implements RestModifyView<PluginResource, Input> {
private final PluginLoader loader;

View File

@@ -46,15 +46,12 @@ public abstract class BaseDataSourceType implements DataSourceType {
if (path == null) {
return ScriptRunner.NOOP;
}
InputStream in = ReviewDb.class.getResourceAsStream(path);
if (in == null) {
throw new IllegalStateException("SQL script " + path + " not found");
}
ScriptRunner runner;
try {
try (InputStream in = ReviewDb.class.getResourceAsStream(path)) {
if (in == null) {
throw new IllegalStateException("SQL script " + path + " not found");
}
runner = new ScriptRunner(path, in);
} finally {
in.close();
}
return runner;
}