From 9ae5d99ebe852fbb42a386b4c4969bb974e4d107 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Tue, 25 Aug 2009 08:50:37 +0000 Subject: [PATCH] Use NIO when copying files without filtering. PR 30094. Submitted by J Bleijenbergh and Robin Verduijn git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@807523 13f79535-47bb-0310-9956-ffa450edef68 --- CONTRIBUTORS | 2 + WHATSNEW | 4 ++ contributors.xml | 8 +++ .../apache/tools/ant/util/ResourceUtils.java | 56 ++++++++++++++++--- 4 files changed, 63 insertions(+), 7 deletions(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index b06e78f22..09573efd7 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -123,6 +123,7 @@ Ingenonsya France Ingmar Stein Irene Rusman Ivan Ivanov +J Bleijenbergh Jack J. Woehr James Duncan Davidson Jan Cumps @@ -261,6 +262,7 @@ Robert Streich Robert Watkins Roberto Scaramuzzi Robin Green +Robin Verduijn Rob Oxspring Rob van Oostrum Rodrigo Schmidt diff --git a/WHATSNEW b/WHATSNEW index 799a63721..535152da4 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -905,6 +905,10 @@ Other changes: a property to the number of rows affected by a task execution. Bugzilla Report 40923. + * when Ant copies files without filtering, it will now use NIO + channels. + Bugzilla Report 30094. + Changes from Ant 1.7.0 TO Ant 1.7.1 ============================================= diff --git a/contributors.xml b/contributors.xml index dc8fde697..30e33ab67 100644 --- a/contributors.xml +++ b/contributors.xml @@ -518,6 +518,10 @@ Ivan Ivanov + + J + Bleijenbergh + Jack J. @@ -1066,6 +1070,10 @@ Robin Green + + Robin + Verduijn + Rob Oxspring diff --git a/src/main/org/apache/tools/ant/util/ResourceUtils.java b/src/main/org/apache/tools/ant/util/ResourceUtils.java index 7f821a2f6..c2e03168e 100644 --- a/src/main/org/apache/tools/ant/util/ResourceUtils.java +++ b/src/main/org/apache/tools/ant/util/ResourceUtils.java @@ -17,19 +17,22 @@ */ package org.apache.tools.ant.util; -import java.io.File; -import java.io.Reader; -import java.io.InputStream; -import java.io.IOException; -import java.io.OutputStream; +import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.BufferedWriter; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; +import java.io.OutputStream; import java.io.OutputStreamWriter; -import java.io.BufferedInputStream; +import java.io.Reader; +import java.nio.channels.FileChannel; import java.util.Arrays; -import java.util.Vector; import java.util.Iterator; +import java.util.Vector; import org.apache.tools.ant.Project; import org.apache.tools.ant.ProjectComponent; @@ -433,6 +436,45 @@ public class ResourceUtils { FileUtils.close(out); FileUtils.close(in); } + } else if (source.as(FileProvider.class) != null + && dest.as(FileProvider.class) != null) { + File sourceFile = + ((FileProvider) source.as(FileProvider.class)).getFile(); + File destFile = + ((FileProvider) dest.as(FileProvider.class)).getFile(); + + File parent = destFile.getParentFile(); + if (parent != null && !parent.isDirectory() + && !destFile.getParentFile().mkdirs()) { + throw new IOException("failed to create the parent directory" + + " for " + destFile); + } + + FileInputStream in = null; + FileOutputStream out = null; + FileChannel srcChannel = null; + FileChannel destChannel = null; + + try { + in = new FileInputStream(sourceFile); + out = new FileOutputStream(destFile); + + srcChannel = in.getChannel(); + destChannel = out.getChannel(); + + long position = 0; + long count = srcChannel.size(); + while (position < count) { + position += + srcChannel.transferTo(position, FileUtils.BUF_SIZE, + destChannel); + } + } finally { + FileUtils.close(srcChannel); + FileUtils.close(destChannel); + FileUtils.close(out); + FileUtils.close(in); + } } else { InputStream in = null; OutputStream out = null;