Skip to content

Commit aaf7676

Browse files
author
Vladimir Kotal
authored
use try-with-resources (#3788)
1 parent 82105c7 commit aaf7676

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

opengrok-web/src/main/java/org/opengrok/web/api/v1/controller/FileController.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,18 @@ public class FileController {
5555
public static final String PATH = "/file";
5656

5757
private StreamingOutput transfer(File file) throws FileNotFoundException {
58-
InputStream in = new FileInputStream(file);
58+
if (!file.exists()) {
59+
throw new FileNotFoundException(String.format("file %s does not exist", file));
60+
}
61+
5962
return out -> {
60-
try {
63+
try (InputStream in = new FileInputStream(file)) {
6164
byte[] buffer = new byte[1024];
6265
int len = in.read(buffer);
6366
while (len != -1) {
6467
out.write(buffer, 0, len);
6568
len = in.read(buffer);
6669
}
67-
} finally {
68-
in.close();
6970
}
7071
};
7172
}
@@ -103,7 +104,7 @@ public StreamingOutput getContentPlain(@Context HttpServletRequest request,
103104
@Produces(MediaType.APPLICATION_OCTET_STREAM)
104105
public StreamingOutput getContentOctets(@Context HttpServletRequest request,
105106
@Context HttpServletResponse response,
106-
@QueryParam("path") final String path) throws IOException, ParseException, NoPathParameterException {
107+
@QueryParam("path") final String path) throws IOException, NoPathParameterException {
107108

108109
File file = toFile(path);
109110

0 commit comments

Comments
 (0)