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:
@@ -77,14 +77,15 @@ public class CssLinker extends AbstractLinker {
|
|||||||
|
|
||||||
private String name(final TreeLogger logger, final PublicResource r)
|
private String name(final TreeLogger logger, final PublicResource r)
|
||||||
throws UnableToCompleteException {
|
throws UnableToCompleteException {
|
||||||
final ByteArrayOutputStream tmp = new ByteArrayOutputStream();
|
byte[] out;
|
||||||
try (InputStream in = r.getContents(logger)) {
|
try (ByteArrayOutputStream tmp = new ByteArrayOutputStream();
|
||||||
|
InputStream in = r.getContents(logger)) {
|
||||||
final byte[] buf = new byte[2048];
|
final byte[] buf = new byte[2048];
|
||||||
int n;
|
int n;
|
||||||
while ((n = in.read(buf)) >= 0) {
|
while ((n = in.read(buf)) >= 0) {
|
||||||
tmp.write(buf, 0, n);
|
tmp.write(buf, 0, n);
|
||||||
}
|
}
|
||||||
tmp.close();
|
out = tmp.toByteArray();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
final UnableToCompleteException ute = new UnableToCompleteException();
|
final UnableToCompleteException ute = new UnableToCompleteException();
|
||||||
ute.initCause(e);
|
ute.initCause(e);
|
||||||
@@ -98,7 +99,7 @@ public class CssLinker extends AbstractLinker {
|
|||||||
} else {
|
} else {
|
||||||
base = "";
|
base = "";
|
||||||
}
|
}
|
||||||
return base + Util.computeStrongName(tmp.toByteArray()) + ".cache.css";
|
return base + Util.computeStrongName(out) + ".cache.css";
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class CssPubRsrc extends PublicResource {
|
private static class CssPubRsrc extends PublicResource {
|
||||||
|
@@ -49,6 +49,7 @@ import org.eclipse.jgit.revwalk.RevWalk;
|
|||||||
import org.eclipse.jgit.treewalk.TreeWalk;
|
import org.eclipse.jgit.treewalk.TreeWalk;
|
||||||
import org.eclipse.jgit.util.NB;
|
import org.eclipse.jgit.util.NB;
|
||||||
|
|
||||||
|
import java.io.FilterOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
@@ -248,10 +249,21 @@ public class CatServlet extends HttpServlet {
|
|||||||
rsp.setDateHeader("Last-Modified", when);
|
rsp.setDateHeader("Last-Modified", when);
|
||||||
CacheHeaders.setNotCacheable(rsp);
|
CacheHeaders.setNotCacheable(rsp);
|
||||||
|
|
||||||
OutputStream out;
|
try (OutputStream out = openOutputStream(
|
||||||
ZipOutputStream zo;
|
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)) {
|
if (!registry.isSafeInline(contentType)) {
|
||||||
// The content may not be safe to transmit inline, as a browser might
|
// 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
|
// 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=\""
|
rsp.setHeader("Content-Disposition", "attachment; filename=\""
|
||||||
+ safeFileName(path, suffix) + ".zip" + "\"");
|
+ 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.setComment(fromCommit.name() + ":" + path);
|
||||||
e.setSize(blobLoader.getSize());
|
e.setSize(blobLoader.getSize());
|
||||||
e.setTime(when);
|
e.setTime(when);
|
||||||
zo.putNextEntry(e);
|
zo.putNextEntry(e);
|
||||||
out = zo;
|
return new FilterOutputStream(zo) {
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
|
try {
|
||||||
|
zo.closeEntry();
|
||||||
|
} finally {
|
||||||
|
super.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
rsp.setContentType(contentType.toString());
|
rsp.setContentType(contentType.toString());
|
||||||
rsp.setHeader("Content-Length", "" + blobLoader.getSize());
|
rsp.setHeader("Content-Length", "" + blobLoader.getSize());
|
||||||
|
|
||||||
out = rsp.getOutputStream();
|
return rsp.getOutputStream();
|
||||||
zo = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
private static String safeFileName(String fileName, final String suffix) {
|
||||||
|
@@ -83,13 +83,6 @@ public class FileContentUtil {
|
|||||||
raw = null;
|
raw = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
BinaryResult result;
|
|
||||||
if (raw != null) {
|
|
||||||
result = BinaryResult.create(raw);
|
|
||||||
} else {
|
|
||||||
result = asBinaryResult(obj);
|
|
||||||
}
|
|
||||||
|
|
||||||
String type;
|
String type;
|
||||||
if (mode == org.eclipse.jgit.lib.FileMode.SYMLINK) {
|
if (mode == org.eclipse.jgit.lib.FileMode.SYMLINK) {
|
||||||
type = X_GIT_SYMLINK;
|
type = X_GIT_SYMLINK;
|
||||||
@@ -97,11 +90,16 @@ public class FileContentUtil {
|
|||||||
type = registry.getMimeType(path, raw).toString();
|
type = registry.getMimeType(path, raw).toString();
|
||||||
type = resolveContentType(project, path, FileMode.FILE, type);
|
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() {
|
BinaryResult result = new BinaryResult() {
|
||||||
@Override
|
@Override
|
||||||
public void writeTo(OutputStream os) throws IOException {
|
public void writeTo(OutputStream os) throws IOException {
|
||||||
|
@@ -76,6 +76,7 @@ class TagSet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void prepare(TagMatcher m) {
|
void prepare(TagMatcher m) {
|
||||||
|
@SuppressWarnings("resource")
|
||||||
RevWalk rw = null;
|
RevWalk rw = null;
|
||||||
try {
|
try {
|
||||||
for (Ref currentRef : m.include) {
|
for (Ref currentRef : m.include) {
|
||||||
|
@@ -391,17 +391,14 @@ public class PatchListLoader implements Callable<PatchList> {
|
|||||||
Map<String, ObjectId> resolved = new HashMap<>();
|
Map<String, ObjectId> resolved = new HashMap<>();
|
||||||
for (Map.Entry<String, MergeResult<? extends Sequence>> entry : r.entrySet()) {
|
for (Map.Entry<String, MergeResult<? extends Sequence>> entry : r.entrySet()) {
|
||||||
MergeResult<? extends Sequence> p = entry.getValue();
|
MergeResult<? extends Sequence> p = entry.getValue();
|
||||||
TemporaryBuffer buf =
|
try (TemporaryBuffer buf =
|
||||||
new TemporaryBuffer.LocalFile(null, 10 * 1024 * 1024);
|
new TemporaryBuffer.LocalFile(null, 10 * 1024 * 1024)) {
|
||||||
try {
|
|
||||||
fmt.formatMerge(buf, p, "BASE", oursName, theirsName, "UTF-8");
|
fmt.formatMerge(buf, p, "BASE", oursName, theirsName, "UTF-8");
|
||||||
buf.close();
|
buf.close();
|
||||||
|
|
||||||
try (InputStream in = buf.openInputStream()) {
|
try (InputStream in = buf.openInputStream()) {
|
||||||
resolved.put(entry.getKey(), ins.insert(Constants.OBJ_BLOB, buf.length(), in));
|
resolved.put(entry.getKey(), ins.insert(Constants.OBJ_BLOB, buf.length(), in));
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
buf.destroy();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -59,20 +59,8 @@ class InstallPlugin implements RestModifyView<TopLevelResource, Input> {
|
|||||||
throw new MethodNotAllowedException("remote installation is disabled");
|
throw new MethodNotAllowedException("remote installation is disabled");
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
InputStream in;
|
try (InputStream in = openStream(input)) {
|
||||||
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 {
|
|
||||||
loader.installPluginFromStream(name, in);
|
loader.installPluginFromStream(name, in);
|
||||||
} finally {
|
|
||||||
in.close();
|
|
||||||
}
|
}
|
||||||
} catch (PluginInstallException e) {
|
} catch (PluginInstallException e) {
|
||||||
StringWriter buf = new StringWriter();
|
StringWriter buf = new StringWriter();
|
||||||
@@ -93,6 +81,18 @@ class InstallPlugin implements RestModifyView<TopLevelResource, Input> {
|
|||||||
return created ? Response.created(info) : Response.ok(info);
|
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)
|
@RequiresCapability(GlobalCapability.ADMINISTRATE_SERVER)
|
||||||
static class Overwrite implements RestModifyView<PluginResource, Input> {
|
static class Overwrite implements RestModifyView<PluginResource, Input> {
|
||||||
private final PluginLoader loader;
|
private final PluginLoader loader;
|
||||||
|
@@ -46,15 +46,12 @@ public abstract class BaseDataSourceType implements DataSourceType {
|
|||||||
if (path == null) {
|
if (path == null) {
|
||||||
return ScriptRunner.NOOP;
|
return ScriptRunner.NOOP;
|
||||||
}
|
}
|
||||||
InputStream in = ReviewDb.class.getResourceAsStream(path);
|
|
||||||
if (in == null) {
|
|
||||||
throw new IllegalStateException("SQL script " + path + " not found");
|
|
||||||
}
|
|
||||||
ScriptRunner runner;
|
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);
|
runner = new ScriptRunner(path, in);
|
||||||
} finally {
|
|
||||||
in.close();
|
|
||||||
}
|
}
|
||||||
return runner;
|
return runner;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user