diff --git a/WHATSNEW b/WHATSNEW index 98e8e0fd5..e372ffb69 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -184,6 +184,9 @@ Other changes: different separator. This is useful if you want to run certain ported Unix tools. +* Copy has a new outputencoding attribute that can be used to change + the encoding while copying files. Bugzilla Report 18217. + Changes from Ant 1.5.2 to Ant 1.5.3 =================================== diff --git a/docs/manual/CoreTasks/copy.html b/docs/manual/CoreTasks/copy.html index 9bc156fd9..0f79edee9 100644 --- a/docs/manual/CoreTasks/copy.html +++ b/docs/manual/CoreTasks/copy.html @@ -104,6 +104,19 @@ operation as filtersets
null
if not set.
+ *
+ * @since Ant 1.6
+ */
+ public String getOutputEncoding() {
+ return outputEncoding;
}
/**
@@ -526,8 +549,8 @@ public class Copy extends Task {
}
fileUtils.copyFile(fromFile, toFile, executionFilters,
filterChains, forceOverwrite,
- preserveLastModified, encoding,
- getProject());
+ preserveLastModified, inputEncoding,
+ outputEncoding, getProject());
} catch (IOException ioe) {
String msg = "Failed to copy " + fromFile + " to " + toFile
+ " due to " + ioe.getMessage();
diff --git a/src/main/org/apache/tools/ant/taskdefs/Move.java b/src/main/org/apache/tools/ant/taskdefs/Move.java
index 7dca8834c..4249aa93b 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Move.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Move.java
@@ -1,7 +1,7 @@
/*
* The Apache Software License, Version 1.1
*
- * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
+ * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -172,7 +172,9 @@ public class Move extends Copy {
getFilterChains(),
forceOverwrite,
getPreserveLastModified(),
- getEncoding(), getProject());
+ getEncoding(),
+ getOutputEncoding(),
+ getProject());
f = new File(fromFile);
if (!f.delete()) {
diff --git a/src/main/org/apache/tools/ant/taskdefs/Sync.java b/src/main/org/apache/tools/ant/taskdefs/Sync.java
index 187696962..04457b14c 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Sync.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Sync.java
@@ -330,6 +330,13 @@ public class Sync extends Task {
_copy.setEncoding(encoding);
}
+ /**
+ * Sets the character encoding for output files.
+ */
+ public void setOutputEncoding(String encoding) {
+ _copy.setOutputEncoding(encoding);
+ }
+
/**
* Subclass Copy in order to access it's file/dir maps.
diff --git a/src/main/org/apache/tools/ant/util/FileUtils.java b/src/main/org/apache/tools/ant/util/FileUtils.java
index ec1ff948b..30d264187 100644
--- a/src/main/org/apache/tools/ant/util/FileUtils.java
+++ b/src/main/org/apache/tools/ant/util/FileUtils.java
@@ -248,6 +248,29 @@ public class FileUtils {
encoding, project);
}
+ /**
+ * Convienence method to copy a file from a source to a
+ * destination specifying if token filtering must be used, if
+ * filter chains must be used, if source files may overwrite
+ * newer destination files and the last modified time of
+ * destFile
file should be made equal
+ * to the last modified time of sourceFile
.
+ *
+ * @throws IOException
+ *
+ * @since Ant 1.6
+ */
+ public void copyFile(String sourceFile, String destFile,
+ FilterSetCollection filters, Vector filterChains,
+ boolean overwrite, boolean preserveLastModified,
+ String inputEncoding, String outputEncoding,
+ Project project)
+ throws IOException {
+ copyFile(new File(sourceFile), new File(destFile), filters,
+ filterChains, overwrite, preserveLastModified,
+ inputEncoding, outputEncoding, project);
+ }
+
/**
* Convienence method to copy a file from a source to a destination.
* No filtering is performed.
@@ -334,6 +357,28 @@ public class FileUtils {
boolean overwrite, boolean preserveLastModified,
String encoding, Project project)
throws IOException {
+ copyFile(sourceFile, destFile, filters, filterChains,
+ overwrite, preserveLastModified, encoding, encoding, project);
+ }
+
+ /**
+ * Convienence method to copy a file from a source to a
+ * destination specifying if token filtering must be used, if
+ * filter chains must be used, if source files may overwrite
+ * newer destination files and the last modified time of
+ * destFile
file should be made equal
+ * to the last modified time of sourceFile
.
+ *
+ * @throws IOException
+ *
+ * @since 1.15, Ant 1.6
+ */
+ public void copyFile(File sourceFile, File destFile,
+ FilterSetCollection filters, Vector filterChains,
+ boolean overwrite, boolean preserveLastModified,
+ String inputEncoding, String outputEncoding,
+ Project project)
+ throws IOException {
if (overwrite || !destFile.exists() ||
destFile.lastModified() < sourceFile.lastModified()) {
@@ -354,23 +399,30 @@ public class FileUtils {
final boolean filterChainsAvailable = (filterChains != null
&& filterChains.size() > 0);
- if (filterSetsAvailable || filterChainsAvailable) {
+ if (filterSetsAvailable || filterChainsAvailable
+ || (inputEncoding != null
+ && !inputEncoding.equals(outputEncoding))
+ || (inputEncoding == null && outputEncoding != null)) {
BufferedReader in = null;
BufferedWriter out = null;
try {
- if (encoding == null) {
+ if (inputEncoding == null) {
in = new BufferedReader(new FileReader(sourceFile));
- out = new BufferedWriter(new FileWriter(destFile));
} else {
in =
new BufferedReader(new InputStreamReader(
new FileInputStream(sourceFile),
- encoding));
+ inputEncoding));
+ }
+
+ if (outputEncoding == null) {
+ out = new BufferedWriter(new FileWriter(destFile));
+ } else {
out =
new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(destFile),
- encoding));
+ outputEncoding));
}
if (filterChainsAvailable) {
diff --git a/src/testcases/org/apache/tools/ant/taskdefs/CopyTest.java b/src/testcases/org/apache/tools/ant/taskdefs/CopyTest.java
index e94e87147..1689395b9 100644
--- a/src/testcases/org/apache/tools/ant/taskdefs/CopyTest.java
+++ b/src/testcases/org/apache/tools/ant/taskdefs/CopyTest.java
@@ -1,7 +1,7 @@
/*
* The Apache Software License, Version 1.1
*
- * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
+ * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -150,4 +150,12 @@ public class CopyTest extends BuildFileTest {
"copytest_single_file_fileset.tmp");
assertTrue(file.exists());
}
+
+ public void testTranscoding() throws IOException {
+ executeTarget("testTranscoding");
+ FileUtils fileUtils = FileUtils.newFileUtils();
+ File f1 = getProject().resolveFile("copy/expected/utf-8");
+ File f2 = getProject().resolveFile("copytest1.tmp");
+ assertTrue(fileUtils.contentEquals(f1, f2));
+ }
}