ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Christopher Taylor" <cstay...@nanshu.com>
Subject [PATCH] filtering on copy with non-default character encoded files mangles contents
Date Sat, 23 Feb 2002 10:04:27 GMT
Index: org/apache/tools/ant/taskdefs/Copy.java
===================================================================
RCS file:
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Copy.java
,v
retrieving revision 1.31
diff -c -u -r1.31 Copy.java
--- org/apache/tools/ant/taskdefs/Copy.java 5 Feb 2002 08:08:10 -0000 1.31
+++ org/apache/tools/ant/taskdefs/Copy.java 23 Feb 2002 09:59:37 -0000
@@ -94,6 +94,7 @@
     protected File destFile = null; // the destination file
     protected File destDir = null;  // the destination directory
     protected Vector filesets = new Vector();
+    protected String encoding = null; // the character encoding of the
files

     protected boolean filtering = false;
     protected boolean preserveLastModified = false;
@@ -131,6 +132,13 @@
     }

     /**
+     * Sets the character encoding
+     */
+    public void setEncoding (String encoding) {
+        this.encoding = encoding;
+    }
+
+    /**
      * Sets the destination directory.
      */
     public void setTodir(File destDir) {
@@ -422,7 +430,7 @@

executionFilters.addFilterSet((FilterSet)filterEnum.nextElement());
                     }
                     fileUtils.copyFile(fromFile, toFile, executionFilters,
-                                       forceOverwrite,
preserveLastModified);
+                                       forceOverwrite,
preserveLastModified, encoding);
                 } catch (IOException ioe) {
                     String msg = "Failed to copy " + fromFile + " to " +
toFile
                         + " due to " + ioe.getMessage();
Index: org/apache/tools/ant/util/FileUtils.java
===================================================================
RCS file:
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/util/FileUtils.jav
a,v
retrieving revision 1.13
diff -c -u -r1.13 FileUtils.java
--- org/apache/tools/ant/util/FileUtils.java 29 Jan 2002 17:12:20 -0000 1.13
+++ org/apache/tools/ant/util/FileUtils.java 23 Feb 2002 09:59:38 -0000
@@ -64,6 +64,8 @@
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;

 import java.lang.reflect.Method;
 import java.text.DecimalFormat;
@@ -157,6 +159,23 @@
     }

     /**
+     * Convienence method to copy a file from a source to a
+     * destination specifying if token filtering must be used, if
+     * source files may overwrite newer destination files, the
+     * last modified time of <code>destFile</code> file should be made
equal
+     * to the last modified time of <code>sourceFile</code>, and a specific
+     * character encoding should be used.
+     *
+     * @throws IOException
+     */
+    public void copyFile(String sourceFile, String destFile,
FilterSetCollection filters,
+                         boolean overwrite, boolean preserveLastModified,
String encoding)
+        throws IOException {
+        copyFile(new File(sourceFile), new File(destFile), filters,
+                 overwrite, preserveLastModified, encoding);
+    }
+
+    /**
      * Convienence method to copy a file from a source to a destination.
      * No filtering is performed.
      *
@@ -186,9 +205,24 @@
      */
     public void copyFile(File sourceFile, File destFile,
FilterSetCollection filters,
                          boolean overwrite) throws IOException {
-        copyFile(sourceFile, destFile, filters, overwrite, false);
+        copyFile(sourceFile, destFile, filters, overwrite, false, null);
     }

+
+    /**
+     * Convienence method to copy a file from a source to a
+     * destination specifying if token filtering must be used, if
+     * source files may overwrite newer destination files, and last
modified value
+     * should be preserved
+     *
+     * @throws IOException
+     */
+    public void copyFile (File sourceFile, File destFile,
FilterSetCollection filters,
+                         boolean overwrite, boolean preserveLastModified )
+        throws IOException {
+        copyFile
(sourceFile,destFile,filters,overwrite,preserveLastModified,null);
+    }
+
     /**
      * Convienence method to copy a file from a source to a
      * destination specifying if token filtering must be used, if
@@ -199,7 +233,7 @@
      * @throws IOException
      */
     public void copyFile(File sourceFile, File destFile,
FilterSetCollection filters,
-                         boolean overwrite, boolean preserveLastModified)
+                         boolean overwrite, boolean preserveLastModified,
String encoding)
         throws IOException {

         if (overwrite || !destFile.exists() ||
@@ -217,9 +251,19 @@
             }

             if (filters != null && filters.hasFilters()) {
-                BufferedReader in = new BufferedReader(new
FileReader(sourceFile));
-                BufferedWriter out = new BufferedWriter(new
FileWriter(destFile));
-
+
+                BufferedReader in = null;
+                BufferedWriter out = null;
+
+                if (encoding != null) {
+                    in = new BufferedReader( new InputStreamReader ( new
FileInputStream ( sourceFile ), encoding ) );
+                    out = new BufferedWriter ( new OutputStreamWriter ( new
FileOutputStream ( destFile ), encoding ) );
+                }
+                else {
+                    in = new BufferedReader(new FileReader(sourceFile));
+                    out = new BufferedWriter(new FileWriter(destFile));
+                }
+
                 int length;
                 String newline = null;
                 String line = in.readLine();



--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message