@@ -57,9 +57,9 @@ import org.apache.tools.ant.launch.Locator;
*/
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 ;
//get some non-crypto-grade randomness from various places.
private static Random rand = new Random(System.currentTimeMillis()
+Runtime.getRuntime().freeMemory()) ;
private boolean onNetWare = Os.isFamily("netware");
@@ -78,6 +78,7 @@ public class FileUtils {
*/
public static final long UNIX_FILE_TIMESTAMP_GRANULARITY = 1000;
// stolen from FilePathToURI of the Xerces-J team
static {
for (int i = 0; i <= 0x20; i++) {
@@ -648,65 +649,27 @@ public class FileUtils {
}
if (preserveLastModified) {
se tFileLastModified(destFile, sourceFile.lastModified());
de stFile.set LastModified(sourceFile.lastModified());
}
}
}
/**
* see whether we have a setLastModified method in File and return it.
*
* @return a method to setLastModified.
*/
protected final Method getSetLastModified() {
if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)) {
return null;
}
synchronized (lockReflection) {
if (setLastModified == null) {
try {
setLastModified =
java.io.File.class.getMethod("setLastModified",
new Class[] {Long.TYPE});
} catch (NoSuchMethodException nse) {
throw new BuildException("File.setlastModified not in JDK > 1.1?",
nse);
}
}
}
return setLastModified;
}
/**
* Calls File.setLastModified(long time) in a Java 1.1 compatible way.
* Calls File.setLastModified(long time). Originally written to
* to dynamically bind to that call on Java1.2+.
*
* @param file the file whose modified time is to be set
* @param time the time to which the last modified time is to be set.
*
* if this is -1, the current time is used.
* @throws BuildException if the time cannot be set.
*/
public void setFileLastModified(File file, long time)
throws BuildException {
if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)) {
return;
}
Long[] times = new Long[1];
if (time < 0) {
times[0] = new Long(System.currentTimeMillis());
} else {
times[0] = new Long(time);
}
try {
getSetLastModified().invoke(file, times);
} catch (java.lang.reflect.InvocationTargetException ite) {
Throwable nested = ite.getTargetException();
throw new BuildException("Exception setting the modification time "
+ "of " + file, nested);
} catch (Throwable other) {
throw new BuildException("Exception setting the modification time "
+ "of " + file, other);
time=System.currentTimeMillis();
}
file.setLastModified(time);
}
/**
@@ -1357,5 +1320,40 @@ public class FileUtils {
return UNIX_FILE_TIMESTAMP_GRANULARITY;
}
}
/**
* Returns true if the source is older than the dest.
* If the dest file does not exist, then the test returns false; it is
* implicitly not up do date.
* @param source source file (should be the older)
* @param dest dest file (should be the newer)
* @param granularity: an offset added to the source time.
* @return true if the source is older than the dest, taking the
* granularity into account
* @since Ant1.7
*/
public boolean isUpToDate(File source,File dest,long granularity) {
//do a check for the destination file existing
if(!dest.exists()) {
//if it does not, then the file is not up to date.
return false;
}
long sourceTime=source.lastModified();
long destTime=dest.lastModified();
return destTime>=sourceTime+granularity;
}
/**
* returns true if the source is older than the dest
* @param source source file (should be the older)
* @param dest dest file (should be the newer)
* @return true if the source is older than the dest, taking the granularity into account
* @since Ant1.7
*/
public boolean isUpToDate(File source, File dest) {
return isUpToDate(source, dest, getFileTimestampGranularity());
}
}