Skip to content

Commit f0a30f1

Browse files
committed
DataHandle: add lastModified() and checksum()
These are optional to support, returning null by default.
1 parent c59b3b0 commit f0a30f1

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/main/java/org/scijava/io/handle/DataHandle.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.io.EOFException;
4040
import java.io.IOException;
4141
import java.io.InputStreamReader;
42+
import java.util.Date;
4243

4344
import org.scijava.io.location.Location;
4445
import org.scijava.plugin.WrapperPlugin;
@@ -79,6 +80,40 @@ public enum ByteOrder {
7980
*/
8081
boolean exists() throws IOException;
8182

83+
/**
84+
* Gets the last modified timestamp of the location.
85+
*
86+
* @return The last modified timestamp, or null if the handle does not support
87+
* this feature or if the location does not exist.
88+
* @throws IOException If something goes wrong with the last modified check.
89+
*/
90+
default Date lastModified() throws IOException {
91+
return null;
92+
}
93+
94+
/**
95+
* Gets a "fast" checksum which succinctly represents the contents of the data
96+
* stream. The term "fast" here refers to the idea that the checksum be
97+
* retrievable quickly, without actually performing a thorough computation
98+
* across the entire data stream. Typically, such a thing is feasible because
99+
* the checksum was calculated a priori; e.g., artifacts deployed to remote
100+
* Maven repositories are always deployed with corresponding checksum files.
101+
* <p>
102+
* No guarantee is made about the exact nature of the checksum (e.g., SHA-1 or
103+
* MD5), only that the value is deterministic for this particular location
104+
* with its current contents. In other words: if a checksum differs from a
105+
* previous inquiry, you can be sure the contents have changed; conversely, if
106+
* the checksum is still the same, the contents are highly likely to be
107+
* unchanged.
108+
* </p>
109+
*
110+
* @return The checksum, or null if the handle does not support this feature.
111+
* @throws IOException If something goes wrong when accessing the checksum.
112+
*/
113+
default String checksum() throws IOException {
114+
return null;
115+
}
116+
82117
/** Returns the current offset in the stream. */
83118
long offset() throws IOException;
84119

src/main/java/org/scijava/io/handle/FileHandle.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import java.io.IOException;
3636
import java.io.RandomAccessFile;
37+
import java.util.Date;
3738

3839
import org.scijava.io.location.FileLocation;
3940
import org.scijava.plugin.Plugin;
@@ -89,6 +90,12 @@ public boolean exists() {
8990
return get().getFile().exists();
9091
}
9192

93+
@Override
94+
public Date lastModified() {
95+
final long lastModified = get().getFile().lastModified();
96+
return lastModified == 0 ? null : new Date(lastModified);
97+
}
98+
9299
@Override
93100
public long offset() throws IOException {
94101
return raf().getFilePointer();

0 commit comments

Comments
 (0)