git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269859 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -58,6 +58,7 @@ import org.apache.tools.ant.BuildException; | |||
| import org.apache.tools.ant.DirectoryScanner; | |||
| import org.apache.tools.ant.Project; | |||
| import org.apache.tools.ant.types.EnumeratedAttribute; | |||
| import org.apache.tools.ant.util.FileUtils; | |||
| import java.io.File; | |||
| import java.io.BufferedReader; | |||
| @@ -65,7 +66,6 @@ import java.io.FileReader; | |||
| import java.io.IOException; | |||
| import java.io.BufferedWriter; | |||
| import java.io.FileWriter; | |||
| import java.util.Random; | |||
| import java.util.Enumeration; | |||
| import java.util.NoSuchElementException; | |||
| @@ -148,6 +148,8 @@ public class FixCRLF extends MatchingTask { | |||
| private File srcDir; | |||
| private File destDir = null; | |||
| private FileUtils fileUtils = FileUtils.newFileUtils(); | |||
| /** | |||
| * Defaults the properties based on the system type. | |||
| * <ul><li>Unix: eol="LF" tab="asis" eof="remove" | |||
| @@ -355,19 +357,6 @@ public class FixCRLF extends MatchingTask { | |||
| } | |||
| } | |||
| /** | |||
| * Creates a temporary file. | |||
| */ | |||
| private File createTempFile() { | |||
| String name = "fixcrlf" | |||
| + (new Random(System.currentTimeMillis())).nextLong(); | |||
| if (destDir == null) { | |||
| return new File(srcDir, name); | |||
| } else { | |||
| return new File(destDir, name); | |||
| } | |||
| } | |||
| /** | |||
| * Checks for the inequality of two files | |||
| */ | |||
| @@ -413,6 +402,7 @@ public class FixCRLF extends MatchingTask { | |||
| private void processFile(String file) throws BuildException { | |||
| File srcFile = new File(srcDir, file); | |||
| File destD = destDir == null ? srcDir : destDir; | |||
| File tmpFile = null; | |||
| BufferedWriter outWriter; | |||
| OneLiner.BufferLine line; | |||
| @@ -423,7 +413,7 @@ public class FixCRLF extends MatchingTask { | |||
| try { | |||
| // Set up the output Writer | |||
| try { | |||
| tmpFile = createTempFile(); | |||
| tmpFile = fileUtils.createTempFile("fixcrlf", "", destD); | |||
| FileWriter writer = new FileWriter(tmpFile); | |||
| outWriter = new BufferedWriter(writer); | |||
| } catch (IOException e) { | |||
| @@ -554,8 +544,7 @@ public class FixCRLF extends MatchingTask { | |||
| throw new BuildException(e); | |||
| } // end of try-catch | |||
| File destFile = new File(destDir == null ? srcDir : destDir, | |||
| file); | |||
| File destFile = new File(destD, file); | |||
| try { | |||
| lines.close(); | |||
| @@ -61,7 +61,6 @@ import java.io.FilenameFilter; | |||
| import java.util.Vector; | |||
| import java.util.Enumeration; | |||
| import java.util.StringTokenizer; | |||
| import java.util.Random; | |||
| import org.apache.tools.ant.BuildException; | |||
| import org.apache.tools.ant.DirectoryScanner; | |||
| import org.apache.tools.ant.Project; | |||
| @@ -73,6 +72,7 @@ import org.apache.tools.ant.types.Reference; | |||
| import org.apache.tools.ant.types.EnumeratedAttribute; | |||
| import org.apache.tools.ant.types.Commandline; | |||
| import org.apache.tools.ant.types.FileSet; | |||
| import org.apache.tools.ant.util.FileUtils; | |||
| /** | |||
| * This task makes it easy to generate Javadoc documentation for a collection | |||
| @@ -279,6 +279,8 @@ public class Javadoc extends Task { | |||
| private boolean useExternalFile = false; | |||
| private File tmpList = null; | |||
| private FileUtils fileUtils = FileUtils.newFileUtils(); | |||
| /** | |||
| * Work around command line length limit by using an external file | |||
| * for the sourcefiles. | |||
| @@ -924,7 +926,7 @@ public class Javadoc extends Task { | |||
| */ | |||
| if (useExternalFile) { | |||
| if (tmpList == null) { | |||
| tmpList = createTempFile(); | |||
| tmpList = fileUtils.createTempFile("javadoc", "", null); | |||
| toExecute.createArgument().setValue("@" + tmpList.getAbsolutePath()); | |||
| } | |||
| srcListWriter = new PrintWriter(new FileWriter(tmpList.getAbsolutePath(), | |||
| @@ -1055,7 +1057,7 @@ public class Javadoc extends Task { | |||
| PrintWriter packageListWriter = null; | |||
| try { | |||
| if (useExternalFile) { | |||
| tmpList = createTempFile(); | |||
| tmpList = fileUtils.createTempFile("javadoc", "", null); | |||
| toExecute.createArgument().setValue("@" + tmpList.getAbsolutePath()); | |||
| packageListWriter = new PrintWriter(new FileWriter(tmpList)); | |||
| } | |||
| @@ -1148,13 +1150,6 @@ public class Javadoc extends Task { | |||
| project.getProperties()); | |||
| } | |||
| /** | |||
| * Creates a temporary file. | |||
| */ | |||
| private File createTempFile() { | |||
| return new File("javadoc" + (new Random(System.currentTimeMillis())).nextLong()); | |||
| } | |||
| private String getJavadocExecutableName() | |||
| { | |||
| // This is the most common extension case - exe for windows and OS/2, | |||
| @@ -63,12 +63,12 @@ import org.apache.tools.ant.taskdefs.LogStreamHandler; | |||
| import org.apache.tools.ant.types.Path; | |||
| import org.apache.tools.ant.types.Commandline; | |||
| import org.apache.tools.ant.types.FileSet; | |||
| import org.apache.tools.ant.util.FileUtils; | |||
| import java.io.File; | |||
| import java.io.PrintWriter; | |||
| import java.io.FileWriter; | |||
| import java.io.IOException; | |||
| import java.util.Random; | |||
| /** | |||
| * This is the default implementation for the CompilerAdapter interface. | |||
| @@ -108,6 +108,8 @@ public abstract class DefaultCompilerAdapter implements CompilerAdapter { | |||
| protected static String lSep = System.getProperty("line.separator"); | |||
| protected Javac attributes; | |||
| private FileUtils fileUtils = FileUtils.newFileUtils(); | |||
| public void setJavac( Javac attributes ) { | |||
| this.attributes = attributes; | |||
| src = attributes.getSrcdir(); | |||
| @@ -362,7 +364,7 @@ public abstract class DefaultCompilerAdapter implements CompilerAdapter { | |||
| if (Commandline.toString(args).length() > 4096) { | |||
| PrintWriter out = null; | |||
| try { | |||
| tmpFile = new File("jikes"+(new Random(System.currentTimeMillis())).nextLong()); | |||
| tmpFile = fileUtils.createTempFile("jikes", "", null); | |||
| out = new PrintWriter(new FileWriter(tmpFile)); | |||
| for (int i = firstFileName; i < args.length; i++) { | |||
| out.println(args[i]); | |||
| @@ -62,6 +62,7 @@ import org.apache.tools.ant.taskdefs.ExecTask; | |||
| import org.apache.tools.ant.taskdefs.condition.Os; | |||
| import org.apache.tools.ant.types.FileSet; | |||
| import org.apache.tools.ant.types.Commandline; | |||
| import org.apache.tools.ant.util.FileUtils; | |||
| import java.io.File; | |||
| import java.io.IOException; | |||
| @@ -71,7 +72,6 @@ import java.io.OutputStream; | |||
| import java.util.Enumeration; | |||
| import java.util.Vector; | |||
| import java.util.Random; | |||
| import java.text.DecimalFormat; | |||
| /** | |||
| @@ -93,6 +93,8 @@ public class Cab extends MatchingTask { | |||
| private static String myos; | |||
| private FileUtils fileUtils = FileUtils.newFileUtils(); | |||
| /** | |||
| * This is the name/location of where to | |||
| * create the .cab file. | |||
| @@ -213,27 +215,6 @@ public class Cab extends MatchingTask { | |||
| return command; | |||
| } | |||
| private static int counter = new Random().nextInt() % 100000; | |||
| protected File createTempFile(String prefix, String suffix) | |||
| { | |||
| if (suffix == null) | |||
| { | |||
| suffix = ".tmp"; | |||
| } | |||
| String name = prefix + | |||
| new DecimalFormat("#####").format(new Integer(counter++)) + | |||
| suffix; | |||
| String tmpdir = System.getProperty("java.io.tmpdir"); | |||
| // java.io.tmpdir is not present in 1.1 | |||
| if (tmpdir == null) | |||
| return new File(name); | |||
| else | |||
| return new File(tmpdir, name); | |||
| } | |||
| /** | |||
| * Creates a list file. This temporary file contains a list of all files | |||
| * to be included in the cab, one file per line. | |||
| @@ -241,7 +222,7 @@ public class Cab extends MatchingTask { | |||
| protected File createListFile(Vector files) | |||
| throws IOException | |||
| { | |||
| File listFile = createTempFile("ant", null); | |||
| File listFile = fileUtils.createTempFile("ant", "", null); | |||
| PrintWriter writer = new PrintWriter(new FileOutputStream(listFile)); | |||
| @@ -342,7 +323,7 @@ public class Cab extends MatchingTask { | |||
| exec.setDir(baseDir); | |||
| if (!doVerbose) { | |||
| outFile = createTempFile("ant", null); | |||
| outFile = fileUtils.createTempFile("ant", "", null); | |||
| exec.setOutput(outFile); | |||
| } | |||
| @@ -61,6 +61,7 @@ import org.apache.tools.ant.util.regexp.Regexp; | |||
| import org.apache.tools.ant.types.RegularExpression; | |||
| import org.apache.tools.ant.types.Substitution; | |||
| import org.apache.tools.ant.types.FileSet; | |||
| import org.apache.tools.ant.util.FileUtils; | |||
| import java.io.BufferedReader; | |||
| import java.io.BufferedWriter; | |||
| import java.io.File; | |||
| @@ -162,6 +163,8 @@ public class ReplaceRegExp extends Task | |||
| private RegularExpression regex; | |||
| private Substitution subs; | |||
| private FileUtils fileUtils = FileUtils.newFileUtils(); | |||
| /*** | |||
| * Default Constructor | |||
| */ | |||
| @@ -260,7 +263,7 @@ public class ReplaceRegExp extends Task | |||
| throws IOException | |||
| { | |||
| File parentDir = new File(new File(f.getAbsolutePath()).getParent()); | |||
| File temp = createTempFile(parentDir); | |||
| File temp = fileUtils.createTempFile("replace", ".txt", parentDir); | |||
| FileReader r = null; | |||
| FileWriter w = null; | |||
| @@ -420,13 +423,6 @@ public class ReplaceRegExp extends Task | |||
| } | |||
| } | |||
| /** | |||
| * Creates a temporary file. | |||
| */ | |||
| private File createTempFile(File dir) { | |||
| return new File(dir, "replace" + (new Random(System.currentTimeMillis())).nextLong()+".txt"); | |||
| } | |||
| } | |||
| @@ -63,8 +63,10 @@ import java.io.FileWriter; | |||
| import java.io.FileInputStream; | |||
| import java.io.FileOutputStream; | |||
| import java.lang.reflect.Method; | |||
| import java.util.StringTokenizer; | |||
| import java.text.DecimalFormat; | |||
| import java.util.Random; | |||
| import java.util.Stack; | |||
| import java.util.StringTokenizer; | |||
| import org.apache.tools.ant.BuildException; | |||
| import org.apache.tools.ant.Project; | |||
| @@ -79,9 +81,12 @@ import org.apache.tools.ant.types.FilterSetCollection; | |||
| * @author duncan@x180.com | |||
| * @author <a href="mailto:conor@apache.org">Conor MacNeill</a> | |||
| * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||
| * | |||
| * @version $Revision$ | |||
| */ | |||
| public class FileUtils { | |||
| private static Random rand = new Random(System.currentTimeMillis()); | |||
| private static Object lockReflection = new Object(); | |||
| private static java.lang.reflect.Method setLastModified = null; | |||
| @@ -467,5 +472,37 @@ public class FileUtils { | |||
| } | |||
| return new File(path); | |||
| } | |||
| /** | |||
| * Create a temporary file in a given directory. | |||
| * | |||
| * <p>The file denoted by the returned abstract pathname did not | |||
| * exist before this method was invoked, any subsequent invocation | |||
| * of this method will yield a different file name.</p> | |||
| * | |||
| * <p>This method is different to File.createTempFile of JDK 1.2 | |||
| * as it doesn't create the file itself and doesn't use platform | |||
| * specific temporary directory when the parentDir attribute is | |||
| * null.</p> | |||
| * | |||
| * @param parentDir Directory to create the temporary file in - | |||
| * current working directory will be assumed if this parameter is | |||
| * null. | |||
| * | |||
| * @since 1.8 | |||
| */ | |||
| public File createTempFile(String prefix, String suffix, File parentDir) { | |||
| File result = null; | |||
| DecimalFormat fmt = new DecimalFormat("#####"); | |||
| synchronized (rand) { | |||
| do { | |||
| result = new File(parentDir, | |||
| prefix + fmt.format(rand.nextInt()) | |||
| + suffix); | |||
| } while (result.exists()); | |||
| } | |||
| return result; | |||
| } | |||
| } | |||
| @@ -273,6 +273,31 @@ public class FileUtilsTest extends TestCase { | |||
| assertEquals(f, new File("a")); | |||
| } | |||
| /** | |||
| * Test createTempFile | |||
| */ | |||
| public void testCreateTempFile() { | |||
| File parent = new File((new File("/tmp")).getAbsolutePath()); | |||
| File tmp1 = fu.createTempFile("pre", ".suf", parent); | |||
| assertTrue("new file", !tmp1.exists()); | |||
| String name = tmp1.getName(); | |||
| assertTrue("starts with pre", name.startsWith("pre")); | |||
| assertTrue("ends with .suf", name.endsWith(".suf")); | |||
| assertEquals("is inside parent dir", | |||
| parent.getAbsolutePath(), | |||
| tmp1.getParent()); | |||
| File tmp2 = fu.createTempFile("pre", ".suf", parent); | |||
| assertTrue("files are different", | |||
| !tmp1.getAbsolutePath().equals(tmp2.getAbsolutePath())); | |||
| // null parent dir | |||
| File tmp3 = fu.createTempFile("pre", ".suf", null); | |||
| assertEquals((new File(tmp3.getName())).getAbsolutePath(), | |||
| tmp3.getAbsolutePath()); | |||
| } | |||
| /** | |||
| * adapt file separators to local conventions | |||
| */ | |||