You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by jg...@apache.org on 2012/02/27 23:16:41 UTC

svn commit: r1294360 - /ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Replace.java

Author: jglick
Date: Mon Feb 27 22:16:41 2012
New Revision: 1294360

URL: http://svn.apache.org/viewvc?rev=1294360&view=rev
Log:
#52742: safer stream closing.

Modified:
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Replace.java

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Replace.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Replace.java?rev=1294360&r1=1294359&r2=1294360&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Replace.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Replace.java Mon Feb 27 22:16:41 2012
@@ -23,11 +23,10 @@ import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
-import java.io.FileReader;
-import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.Reader;
 import java.io.Writer;
@@ -351,8 +350,9 @@ public class Replace extends MatchingTas
      * a StringBuffer. Compatible with the Replacefilter.
      * @since 1.7
      */
-    private class FileInput {
+    private class FileInput /* JDK 5: implements Closeable */ {
         private StringBuffer outputBuffer;
+        private final InputStream is;
         private Reader reader;
         private char[] buffer;
         private static final int BUFF_SIZE = 4096;
@@ -365,11 +365,13 @@ public class Replace extends MatchingTas
         FileInput(File source) throws IOException {
             outputBuffer = new StringBuffer();
             buffer = new char[BUFF_SIZE];
-            if (encoding == null) {
-                reader = new BufferedReader(new FileReader(source));
-            } else {
-                reader = new BufferedReader(new InputStreamReader(
-                        new FileInputStream(source), encoding));
+            is = new FileInputStream(source);
+            try {
+                reader = new BufferedReader(encoding != null ? new InputStreamReader(is, encoding) : new InputStreamReader(is));
+            } finally {
+                if (reader == null) {
+                    is.close();
+                }
             }
         }
 
@@ -401,15 +403,8 @@ public class Replace extends MatchingTas
          * Closes the file.
          * @throws IOException When the file cannot be closed.
          */
-        void close() throws IOException {
-            reader.close();
-        }
-
-        /**
-         * Closes file but doesn't throw exception
-         */
-        void closeQuietly() {
-            FileUtils.close(reader);
+        public void close() throws IOException {
+            is.close();
         }
 
     }
@@ -419,8 +414,9 @@ public class Replace extends MatchingTas
      * Replacefilter.
      * @since 1.7
      */
-    private class FileOutput {
+    private class FileOutput /* JDK 5: implements Closeable */ {
         private StringBuffer inputBuffer;
+        private final OutputStream os;
         private Writer writer;
 
         /**
@@ -429,12 +425,14 @@ public class Replace extends MatchingTas
          * @throws IOException When the file cannot be read from.
          */
         FileOutput(File out) throws IOException {
-                if (encoding == null) {
-                    writer = new BufferedWriter(new FileWriter(out));
-                } else {
-                    writer = new BufferedWriter(new OutputStreamWriter
-                            (new FileOutputStream(out), encoding));
+            os = new FileOutputStream(out);
+            try {
+                writer = new BufferedWriter(encoding != null ? new OutputStreamWriter(os, encoding) : new OutputStreamWriter(os));
+            } finally {
+                if (writer == null) {
+                    os.close();
                 }
+            }
         }
 
         /**
@@ -475,16 +473,10 @@ public class Replace extends MatchingTas
          * Closes the file.
          * @throws IOException When the file cannot be closed.
          */
-        void close() throws IOException {
-            writer.close();
+        public void close() throws IOException {
+            os.close();
         }
 
-        /**
-         * Closes file but doesn't throw exception
-         */
-        void closeQuietly() {
-            FileUtils.close(writer);
-        }
     }
 
     /**
@@ -665,62 +657,52 @@ public class Replace extends MatchingTas
                                      + " doesn't exist", getLocation());
         }
 
-        File temp = null;
-        FileInput in = null;
-        FileOutput out = null;
-        try {
-            in = new FileInput(src);
+        int repCountStart = replaceCount;
+        logFilterChain(src.getPath());
 
-            temp = FILE_UTILS.createTempFile("rep", ".tmp",
+        try {
+            File temp = FILE_UTILS.createTempFile("rep", ".tmp",
                     src.getParentFile(), false, true);
-            out = new FileOutput(temp);
-
-            int repCountStart = replaceCount;
-
-            logFilterChain(src.getPath());
-
-            out.setInputBuffer(buildFilterChain(in.getOutputBuffer()));
-
-            while (in.readChunk()) {
-                if (processFilterChain()) {
-                    out.process();
+            try {
+                FileInput in = new FileInput(src);
+                try {
+                    FileOutput out = new FileOutput(temp);
+                    try {
+                        out.setInputBuffer(buildFilterChain(in.getOutputBuffer()));
+
+                        while (in.readChunk()) {
+                            if (processFilterChain()) {
+                                out.process();
+                            }
+                        }
+
+                        flushFilterChain();
+
+                        out.flush();
+                    } finally {
+                        out.close();
+                    }
+                } finally {
+                    in.close();
                 }
-            }
-
-            flushFilterChain();
-
-            out.flush();
-            in.close();
-            in = null;
-            out.close();
-            out = null;
-
-            boolean changes = (replaceCount != repCountStart);
-            if (changes) {
-                fileCount++;
-                long origLastModified = src.lastModified();
-                FILE_UTILS.rename(temp, src);
-                if (preserveLastModified) {
-                    FILE_UTILS.setFileLastModified(src, origLastModified);
+                boolean changes = (replaceCount != repCountStart);
+                if (changes) {
+                    fileCount++;
+                    long origLastModified = src.lastModified();
+                    FILE_UTILS.rename(temp, src);
+                    if (preserveLastModified) {
+                        FILE_UTILS.setFileLastModified(src, origLastModified);
+                    }
+                }
+            } finally {
+                if (temp.isFile() && !temp.delete()) {
+                    temp.deleteOnExit();
                 }
-                temp = null;
             }
         } catch (IOException ioe) {
             throw new BuildException("IOException in " + src + " - "
                     + ioe.getClass().getName() + ":"
                     + ioe.getMessage(), ioe, getLocation());
-        } finally {
-            if (null != in) {
-                in.closeQuietly();
-            }
-            if (null != out) {
-                out.closeQuietly();
-            }
-            if (temp != null) {
-                if (!temp.delete()) {
-                    temp.deleteOnExit();
-                }
-            }
         }
     }