Browse Source

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
master
Steve Loughran 21 years ago
parent
commit
b6da0f3f12
1 changed files with 95 additions and 1 deletions
  1. +95
    -1
      src/main/org/apache/tools/ant/util/FileUtils.java

+ 95
- 1
src/main/org/apache/tools/ant/util/FileUtils.java View File

@@ -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
}
}
}

}


Loading…
Cancel
Save