]> source.dussan.org Git - gitblit.git/commitdiff
Close file descriptor leak (issue-199)
authorJames Moger <james.moger@gitblit.com>
Wed, 15 May 2013 19:49:58 +0000 (15:49 -0400)
committerJames Moger <james.moger@gitblit.com>
Wed, 15 May 2013 19:49:58 +0000 (15:49 -0400)
src/main/java/com/gitblit/GitBlit.java

index 93293d85001b60fb17328fe08893d4bda56ca7e7..f017d2186c2a08cd0e816bb04fdbb20351dc43f5 100644 (file)
@@ -3509,6 +3509,8 @@ public class GitBlit implements ServletContextListener {
                        // extract the resource to the directory if it does not exist\r
                        File f = new File(toDir, resource.substring(path.length()));\r
                        if (!f.exists()) {\r
+                               InputStream is = null;
+                               OutputStream os = null;
                                try {\r
                                        if (resource.charAt(resource.length() - 1) == '/') {\r
                                                // directory\r
@@ -3517,20 +3519,33 @@ public class GitBlit implements ServletContextListener {
                                        } else {\r
                                                // file\r
                                                f.getParentFile().mkdirs();\r
-                                               InputStream is = context.getResourceAsStream(resource);\r
-                                               OutputStream os = new FileOutputStream(f);\r
+                                               is = context.getResourceAsStream(resource);
+                                               os = new FileOutputStream(f);
                                                byte [] buffer = new byte[4096];\r
                                                int len = 0;\r
                                                while ((len = is.read(buffer)) > -1) {\r
                                                        os.write(buffer, 0, len);\r
                                                }\r
-                                               is.close();\r
-                                               os.close();\r
                                        }\r
                                } catch (FileNotFoundException e) {\r
                                        logger.error("Failed to find resource \"" + resource + "\"", e);\r
                                } catch (IOException e) {\r
                                        logger.error("Failed to copy resource \"" + resource + "\" to " + f, e);\r
+                               } finally {
+                                       if (is != null) {
+                                               try {
+                                                       is.close();
+                                               } catch (IOException e) {
+                                                       // ignore
+                                               }
+                                       }
+                                       if (os != null) {
+                                               try {
+                                                       os.close();
+                                               } catch (IOException e) {
+                                                       // ignore
+                                               }
+                                       }
                                }\r
                        }\r
                }\r