Skip to content

Commit c92f4ac

Browse files
author
Niall Pemberton
committed
IO-132 Detect changes on File's size and type as well as last modified and "refresh" the values that were compared
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1005693 13f79535-47bb-0310-9956-ffa450edef68
1 parent 6c289ff commit c92f4ac

2 files changed

Lines changed: 52 additions & 25 deletions

File tree

src/main/java/org/apache/commons/io/monitor/FilesystemEntry.java

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class FilesystemEntry implements Serializable {
4848
private boolean exists;
4949
private boolean directory;
5050
private long lastModified;
51+
private long length;
5152

5253
/**
5354
* Construct a new monitor for a specified {@link File}.
@@ -74,18 +75,39 @@ public FilesystemEntry(FilesystemEntry parent, File file) {
7475
}
7576

7677
/**
77-
* Refresh the attributes from the underlying {@link File}.
78+
* Refresh the attributes from the {@link File}, indicating
79+
* whether the file has changed.
7880
* <p>
79-
* This implementation refreshes the <code>name</code>, <code>exists</code>
80-
* <code>directory</code> and <code>lastModified</code> properties.
81-
*/
82-
public void refresh(File file) {
83-
name = file.getName();
84-
exists = file.exists();
85-
if (exists) {
86-
directory = file.isDirectory();
87-
lastModified = file.lastModified();
88-
}
81+
* This implementation refreshes the <code>name</code>, <code>exists</code>,
82+
* <code>directory</code>, <code>lastModified</code> and <code>length</code>
83+
* properties.
84+
* <p>
85+
* The <code>exists</code>, <code>directory</code>, <code>lastModified</code>
86+
* and <code>length</code> properties are compared for changes
87+
*
88+
* @param file the file instance to compare to
89+
* @return <code>true</code> if the file has changed, otherwise <code>false</code>
90+
*/
91+
public boolean refresh(File file) {
92+
93+
// cache original values
94+
boolean origExists = exists;
95+
long origLastModified = lastModified;
96+
boolean origDirectory = directory;
97+
long origLength = length;
98+
99+
// refresh the values
100+
name = file.getName();
101+
exists = file.exists();
102+
directory = (exists ? file.isDirectory() : false);
103+
lastModified = (exists ? file.lastModified() : 0);
104+
length = (exists && !directory ? file.length() : 0);
105+
106+
// Return if there are changes
107+
return (exists != origExists ||
108+
lastModified != origLastModified ||
109+
directory != origDirectory ||
110+
length != origLength);
89111
}
90112

91113
/**
@@ -101,18 +123,6 @@ public FilesystemEntry newChildInstance(File file) {
101123
return new FilesystemEntry(this, file);
102124
}
103125

104-
/**
105-
* Indicate whether the file has changed or not.
106-
* <p>
107-
* This implementation compares the <code>lastModified<code>
108-
* value of the {@link File} with the stored value.
109-
*
110-
* @return whether the file has changed or not
111-
*/
112-
public boolean hasChanged(File file) {
113-
return (lastModified != file.lastModified());
114-
}
115-
116126
/**
117127
* Return the parent entry.
118128
*
@@ -198,6 +208,24 @@ public void setLastModified(long lastModified) {
198208
this.lastModified = lastModified;
199209
}
200210

211+
/**
212+
* Return the length.
213+
*
214+
* @return the length
215+
*/
216+
public long getLength() {
217+
return length;
218+
}
219+
220+
/**
221+
* Set the length.
222+
*
223+
* @param length the length
224+
*/
225+
public void setLength(long length) {
226+
this.length = length;
227+
}
228+
201229
/**
202230
* Indicate whether the file existed the last time it
203231
* was checked.

src/main/java/org/apache/commons/io/monitor/FilesystemObserver.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,15 +428,14 @@ private void doCreate(FilesystemEntry entry) {
428428
* @param file The current file
429429
*/
430430
private void doMatch(FilesystemEntry entry, File file) {
431-
if (entry.hasChanged(file)) {
431+
if (entry.refresh(file)) {
432432
for (FilesystemListener listener : listeners) {
433433
if (entry.isDirectory()) {
434434
listener.onDirectoryChange(file);
435435
} else {
436436
listener.onFileChange(file);
437437
}
438438
}
439-
entry.refresh(file);
440439
}
441440
}
442441

0 commit comments

Comments
 (0)