From 9ce98074308e646c89b868a37f5ed90c0ce3396b Mon Sep 17 00:00:00 2001 From: Peter Reilly Date: Mon, 31 May 2004 08:21:02 +0000 Subject: [PATCH] Add a binary flag to the concat task PR: 26312 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@276496 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 2 + docs/manual/CoreTasks/concat.html | 24 ++++ .../org/apache/tools/ant/taskdefs/Concat.java | 124 +++++++++++++++++- 3 files changed, 149 insertions(+), 1 deletion(-) diff --git a/WHATSNEW b/WHATSNEW index 14f22a5da..19a542ee9 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -185,6 +185,8 @@ Other changes: a granularity of two seconds). The default remains to round up. Bugzilla Report 17934. +* A binary option has been added to . Bugzilla Report 26312. + Changes from Ant 1.6.0 to Ant 1.6.1 ============================================= diff --git a/docs/manual/CoreTasks/concat.html b/docs/manual/CoreTasks/concat.html index 20a0fac1d..3b89a7849 100644 --- a/docs/manual/CoreTasks/concat.html +++ b/docs/manual/CoreTasks/concat.html @@ -123,6 +123,22 @@ No + + binary + + since ant 1.6.2 + 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. + + No + @@ -264,6 +280,14 @@ </concat> +

Concatenate a number of binary files.

+
+   <concat destfile="${build.dir}/dist.bin" binary="yes">
+     <fileset file="${src.dir}/scripts/dist.sh">
+     <fileset file="${build.dir}/dist.tar.bz2">
+   </concat>
+        
+

diff --git a/src/main/org/apache/tools/ant/taskdefs/Concat.java b/src/main/org/apache/tools/ant/taskdefs/Concat.java index e672ce797..8a7cdcf92 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Concat.java +++ b/src/main/org/apache/tools/ant/taskdefs/Concat.java @@ -32,6 +32,7 @@ import java.io.Reader; 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 @@ public class Concat extends Task { /** Stores the output file encoding. */ private String outputEncoding = null; + /** Stores the binary attribute */ + private boolean binary = false; + // Child elements. /** @@ -233,6 +237,7 @@ public class Concat extends Task { textBuffer.append(text); } + /** * Add a header to the concatenated output * @param header the header @@ -291,6 +296,20 @@ public class Concat extends Task { this.outputWriter = outputWriter; } + /** + * 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. */ @@ -299,6 +318,36 @@ public class Concat extends Task { // 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 @@ public class Concat extends Task { return; } - cat(); + if (binary) { + binaryCat(); + } else { + cat(); + } } /** @@ -403,6 +456,75 @@ public class Concat extends Task { } } + /** 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); + } + } + } + } + /** perform the concatenation */ private void cat() { OutputStream os = null;