peterreilly 2004/05/31 01:21:02
Modified: src/main/org/apache/tools/ant/taskdefs Concat.java
docs/manual/CoreTasks concat.html
. WHATSNEW
Log:
Add a binary flag to the concat task
PR: 26312
Revision Changes Path
1.36 +123 -1 ant/src/main/org/apache/tools/ant/taskdefs/Concat.java
Index: Concat.java
===================================================================
RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/Concat.java,v
retrieving revision 1.35
retrieving revision 1.36
diff -u -r1.35 -r1.36
--- Concat.java 10 May 2004 09:19:30 -0000 1.35
+++ Concat.java 31 May 2004 08:21:02 -0000 1.36
@@ -32,6 +32,7 @@
import java.io.StringReader;
import java.io.Writer;
import java.util.Enumeration;
+import java.util.Iterator;
import java.util.Vector;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
@@ -90,6 +91,9 @@
/** Stores the output file encoding. */
private String outputEncoding = null;
+ /** Stores the binary attribute */
+ private boolean binary = false;
+
// Child elements.
/**
@@ -233,6 +237,7 @@
textBuffer.append(text);
}
+
/**
* Add a header to the concatenated output
* @param header the header
@@ -292,6 +297,20 @@
}
/**
+ * set the binary attribute.
+ * if true, concat will concatenate the files
+ * byte for byte. This mode does not allow
+ * any filtering, or other modifications
+ * to the input streams.
+ * The default value is false.
+ * @since ant 1.6.2
+ * @param binary if true, enable binary mode
+ */
+ public void setBinary(boolean binary) {
+ this.binary = binary;
+ }
+
+ /**
* This method performs the concatenation.
*/
public void execute() {
@@ -299,6 +318,36 @@
// treat empty nested text as no text
sanitizeText();
+ // if binary check if incompatible attributes are used
+ if (binary) {
+ if (destinationFile == null) {
+ throw new BuildException(
+ "DestFile attribute is required for binary
concatenation");
+ }
+
+ if (textBuffer != null) {
+ throw new BuildException(
+ "Nested text is incompatible with binary concatenation");
+ }
+ if (encoding != null || outputEncoding != null) {
+ throw new BuildException(
+ "Seting input or output encoding is incompatible with
binary"
+ + " concatenation");
+ }
+ if (filterChains != null) {
+ throw new BuildException(
+ "Setting filters is incompatible with binary
concatenation");
+ }
+ if (fixLastLine) {
+ throw new BuildException(
+ "Setting fixlastline is incompatible with binary
concatenation");
+ }
+ if (header != null || footer != null) {
+ throw new BuildException(
+ "Nested header or footer is incompatible with binary
concatenation");
+ }
+ }
+
if (destinationFile != null && outputWriter != null) {
throw new BuildException(
"Cannot specify both a destination file and an output
writer");
@@ -366,7 +415,11 @@
return;
}
- cat();
+ if (binary) {
+ binaryCat();
+ } else {
+ cat();
+ }
}
/**
@@ -400,6 +453,75 @@
+ "is the same as the output
file.");
}
sourceFiles.addElement(file);
+ }
+ }
+
+ /** perform the binary concatenation */
+ private void binaryCat() {
+ log("Binary concatenation of " + sourceFiles.size()
+ + " files to " + destinationFile);
+ FileOutputStream out = null;
+ FileInputStream in = null;
+ byte[] buffer = new byte[8 * 1024];
+ try {
+ try {
+ out = new FileOutputStream(destinationFile);
+ } catch (Exception t) {
+ throw new BuildException(
+ "Unable to open " + destinationFile
+ + " for writing", t);
+ }
+ for (Iterator i = sourceFiles.iterator(); i.hasNext(); ) {
+ File sourceFile = (File) i.next();
+ try {
+ in = new FileInputStream(sourceFile);
+ } catch (Exception t) {
+ throw new BuildException(
+ "Unable to open input file " + sourceFile,
+ t);
+ }
+ int count = 0;
+ do {
+ try {
+ count = in.read(buffer, 0, buffer.length);
+ } catch (Exception t) {
+ throw new BuildException(
+ "Unable to read from " + sourceFile, t);
+ }
+ try {
+ if (count > 0) {
+ out.write(buffer, 0, count);
+ }
+ } catch (Exception t) {
+ throw new BuildException(
+ "Unable to write to " + destinationFile, t);
+ }
+ } while (count > 0);
+
+ try {
+ in.close();
+ } catch (Exception t) {
+ throw new BuildException(
+ "Unable to close " + sourceFile, t);
+ }
+ in = null;
+ }
+ } finally {
+ if (in != null) {
+ try {
+ in.close();
+ } catch (Throwable t) {
+ // Ignore
+ }
+ }
+ if (out != null) {
+ try {
+ out.close();
+ } catch (Exception ex) {
+ throw new BuildException(
+ "Unable to close " + destinationFile, ex);
+ }
+ }
}
}
1.13 +24 -0 ant/docs/manual/CoreTasks/concat.html
Index: concat.html
===================================================================
RCS file: /home/cvs/ant/docs/manual/CoreTasks/concat.html,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- concat.html 6 May 2004 09:08:42 -0000 1.12
+++ concat.html 31 May 2004 08:21:02 -0000 1.13
@@ -123,6 +123,22 @@
</td>
<td valign="top" align="center">No</td>
</tr>
+ <tr>
+ <td valign="top">binary</td>
+ <td valign="top">
+ <em>since ant 1.6.2</em>
+ If this attribute is set to true, the task concatenates the files
+ in a byte by byte fashion. If this attribute is false, concat
will
+ not normally work for binary files due to character encoding
+ issues.
+ If this option is set to true, the destfile attribute must be
+ set, and the task cannot used nested text.
+ Also the attributes encoding, outputencoding, filelastline
+ cannot be used.
+ The default is false.
+ </td>
+ <td valign="top" align="center">No</td>
+ </tr>
</table>
@@ -261,6 +277,14 @@
<contains value="project"/>
</linecontains>
</filterchain>
+ </concat>
+ </pre>
+
+ <p><b>Concatenate a number of binary files.</b></p>
+ <pre>
+ <concat destfile="${build.dir}/dist.bin" binary="yes">
+ <fileset file="${src.dir}/scripts/dist.sh">
+ <fileset file="${build.dir}/dist.tar.bz2">
</concat>
</pre>
1.609 +2 -0 ant/WHATSNEW
Index: WHATSNEW
===================================================================
RCS file: /home/cvs/ant/WHATSNEW,v
retrieving revision 1.608
retrieving revision 1.609
diff -u -r1.608 -r1.609
--- WHATSNEW 25 May 2004 11:48:19 -0000 1.608
+++ WHATSNEW 31 May 2004 08:21:02 -0000 1.609
@@ -185,6 +185,8 @@
a granularity of two seconds). The default remains to round up.
Bugzilla Report 17934.
+* A binary option has been added to <concat>. Bugzilla Report 26312.
+
Changes from Ant 1.6.0 to Ant 1.6.1
=============================================
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]