From b6da0f3f129cb8b4a0c37fed0fef05eb127e534d Mon Sep 17 00:00:00 2001 From: Steve Loughran Date: Tue, 3 Aug 2004 23:18:35 +0000 Subject: [PATCH] 1. some factoring out of the timestamp checks for even more reuse. That way the granularity logic can all go in one place. 2. a refactoring of close() to ignore exceptions. I made this static because I want to use it everywhere we close things in a finally clause. NB, note that all four methods would all be unified if the writer/reader/instream/outstream classes had a base class "Closeable" with method void close(). No, I have not refactored everything to use these yet. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@276747 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/tools/ant/util/FileUtils.java | 96 ++++++++++++++++++- 1 file changed, 95 insertions(+), 1 deletion(-) diff --git a/src/main/org/apache/tools/ant/util/FileUtils.java b/src/main/org/apache/tools/ant/util/FileUtils.java index f7c95764b..fc8bb47a6 100644 --- a/src/main/org/apache/tools/ant/util/FileUtils.java +++ b/src/main/org/apache/tools/ant/util/FileUtils.java @@ -30,6 +30,9 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Reader; +import java.io.PrintWriter; +import java.io.Writer; +import java.io.OutputStream; import java.lang.reflect.Method; import java.net.MalformedURLException; import java.net.URL; @@ -1340,9 +1343,10 @@ public class FileUtils { } long sourceTime=source.lastModified(); long destTime=dest.lastModified(); - return destTime>=sourceTime+granularity; + return isUpToDate(sourceTime, destTime, granularity); } + /** * returns true if the source is older than the dest * @param source source file (should be the older) @@ -1354,6 +1358,96 @@ public class FileUtils { return isUpToDate(source, dest, getFileTimestampGranularity()); } + /** + * compare two timestamps for being up to date, use granularity too., + * + * @param sourceTime timestamp of source file + * @param destTime timestamp of dest file + * @param granularity os/filesys granularity + * @return true if the dest file is considered up to date + */ + public boolean isUpToDate(long sourceTime,long destTime, long granularity) { + if(destTime==-1) { + return false; + } + return destTime >= sourceTime + granularity; + } + + /** + * compare two timestamps for being up to date, use the + * current granularity + * + * @param sourceTime timestamp of source file + * @param destTime timestamp of dest file + * @return true if the dest file is considered up to date + */ + public boolean isUpToDate(long sourceTime, long destTime) { + return isUpToDate(sourceTime, destTime,getFileTimestampGranularity()); + } + + + /** + * close a writer without throwing any exception if something went wrong. + * Do not attempt to close it if the file is null + * @param device output writer, can be null + */ + public static void close(Writer device) { + if (device != null) { + try { + device.close(); + } catch (IOException ioex) { + //ignore + } + } + } + + /** + * close a stream without throwing any exception if something went wrong. + * Do not attempt to close it if the file is null + * + * @param device stream, can be null + */ + public static void close(Reader device) { + if ( device != null ) { + try { + device.close(); + } catch (IOException ioex) { + //ignore + } + } + } + + /** + * close a stream without throwing any exception if something went wrong. + * Do not attempt to close it if the file is null + * + * @param device stream, can be null + */ + public static void close(OutputStream device) { + if ( device != null ) { + try { + device.close(); + } catch (IOException ioex) { + //ignore + } + } + } + + /** + * close a stream without throwing any exception if something went wrong. + * Do not attempt to close it if the file is null + * + * @param device stream, can be null + */ + public static void close(InputStream device) { + if ( device != null ) { + try { + device.close(); + } catch (IOException ioex) { + //ignore + } + } + } }