tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1581578 - in /tomcat/trunk/java/org/apache/catalina/manager: ManagerServlet.java host/HostManagerServlet.java
Date Tue, 25 Mar 2014 22:58:20 GMT
Author: markt
Date: Tue Mar 25 22:58:19 2014
New Revision: 1581578

URL: http://svn.apache.org/r1581578
Log:
try with resources

Modified:
    tomcat/trunk/java/org/apache/catalina/manager/ManagerServlet.java
    tomcat/trunk/java/org/apache/catalina/manager/host/HostManagerServlet.java

Modified: tomcat/trunk/java/org/apache/catalina/manager/ManagerServlet.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/manager/ManagerServlet.java?rev=1581578&r1=1581577&r2=1581578&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/manager/ManagerServlet.java (original)
+++ tomcat/trunk/java/org/apache/catalina/manager/ManagerServlet.java Tue Mar 25 22:58:19
2014
@@ -1531,12 +1531,10 @@ public class ManagerServlet extends Http
             String msg = smClient.getString("managerServlet.deleteFail", war);
             throw new IOException(msg);
         }
-        ServletInputStream istream = null;
-        BufferedOutputStream ostream = null;
-        try {
-            istream = request.getInputStream();
-            ostream =
-                new BufferedOutputStream(new FileOutputStream(war), 1024);
+
+        try (ServletInputStream istream = request.getInputStream();
+                BufferedOutputStream ostream =
+                        new BufferedOutputStream(new FileOutputStream(war), 1024)) {
             byte buffer[] = new byte[1024];
             while (true) {
                 int n = istream.read(buffer);
@@ -1546,33 +1544,12 @@ public class ManagerServlet extends Http
                 ostream.write(buffer, 0, n);
             }
             ostream.flush();
-            ostream.close();
-            ostream = null;
-            istream.close();
-            istream = null;
         } catch (IOException e) {
             if (war.exists() && !war.delete()) {
                 writer.println(
                         smClient.getString("managerServlet.deleteFail", war));
             }
             throw e;
-        } finally {
-            if (ostream != null) {
-                try {
-                    ostream.close();
-                } catch (Throwable t) {
-                    ExceptionUtils.handleThrowable(t);
-                }
-                ostream = null;
-            }
-            if (istream != null) {
-                try {
-                    istream.close();
-                } catch (Throwable t) {
-                    ExceptionUtils.handleThrowable(t);
-                }
-                istream = null;
-            }
         }
 
     }
@@ -1643,11 +1620,8 @@ public class ManagerServlet extends Http
             if (fileSrc.isDirectory()) {
                 result = copyInternal(fileSrc, fileDest, buf);
             } else {
-                FileInputStream is = null;
-                FileOutputStream os = null;
-                try {
-                    is = new FileInputStream(fileSrc);
-                    os = new FileOutputStream(fileDest);
+                try (FileInputStream is = new FileInputStream(fileSrc);
+                        FileOutputStream os = new FileOutputStream(fileDest)){
                     int len = 0;
                     while (true) {
                         len = is.read(buf);
@@ -1658,21 +1632,6 @@ public class ManagerServlet extends Http
                 } catch (IOException e) {
                     e.printStackTrace();
                     result = false;
-                } finally {
-                    if (is != null) {
-                        try {
-                            is.close();
-                        } catch (IOException e) {
-                            // Ignore
-                        }
-                    }
-                    if (os != null) {
-                        try {
-                            os.close();
-                        } catch (IOException e) {
-                            // Ignore
-                        }
-                    }
                 }
             }
         }

Modified: tomcat/trunk/java/org/apache/catalina/manager/host/HostManagerServlet.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/manager/host/HostManagerServlet.java?rev=1581578&r1=1581577&r2=1581578&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/manager/host/HostManagerServlet.java (original)
+++ tomcat/trunk/java/org/apache/catalina/manager/host/HostManagerServlet.java Tue Mar 25
22:58:19 2014
@@ -379,11 +379,9 @@ public class HostManagerServlet
                         "hostManagerServlet.configBaseCreateFail", name));
                 return;
             }
-            InputStream is = null;
-            OutputStream os = null;
-            try {
-                is = getServletContext().getResourceAsStream("/manager.xml");
-                os = new FileOutputStream(new File(configBaseFile, "manager.xml"));
+            try (InputStream is = getServletContext().getResourceAsStream("/manager.xml");
+                    OutputStream os = new FileOutputStream(
+                            new File(configBaseFile, "manager.xml"))) {
                 byte buffer[] = new byte[512];
                 int len = buffer.length;
                 while (true) {
@@ -396,21 +394,6 @@ public class HostManagerServlet
                 writer.println(smClient.getString(
                         "hostManagerServlet.managerXml"));
                 return;
-            } finally {
-                if (is != null) {
-                    try {
-                        is.close();
-                    } catch (IOException e) {
-                        // Ignore
-                    }
-                }
-                if (os != null) {
-                    try {
-                        os.close();
-                    } catch (IOException e) {
-                        // Ignore
-                    }
-                }
             }
         }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Mime
View raw message