13
13
import java .net .HttpURLConnection ;
14
14
import java .net .URL ;
15
15
import java .net .URLConnection ;
16
+ import java .nio .file .Files ;
17
+ import java .nio .file .attribute .BasicFileAttributes ;
16
18
import java .time .Duration ;
17
19
import java .time .ZonedDateTime ;
18
20
import java .time .temporal .ChronoUnit ;
@@ -36,29 +38,56 @@ public class GCModel implements Serializable {
36
38
*/
37
39
private static class FileInformation implements Serializable {
38
40
private static final long serialVersionUID = 1L ;
41
+ private static final Logger logger = Logger .getLogger (FileInformation .class .getName ());
39
42
43
+ public long creationTime ;
40
44
public long lastModified ;
41
45
public long length ;
42
46
43
47
public FileInformation () {
44
- this (-1 , -1 );
48
+ creationTime = 0 ;
49
+ creationTime = 0 ;
50
+ length = 0 ;
45
51
}
46
52
47
- public FileInformation (long lastModified , long length ) {
48
- super ();
53
+ public FileInformation (File file ) {
54
+ if (file == null )
55
+ throw new IllegalArgumentException ("File must not be null!" );
49
56
50
- this .lastModified = lastModified ;
51
- this .length = length ;
57
+ Optional <BasicFileAttributes > fileAttributes = getFileAttributes (file );
58
+ this .lastModified = file .lastModified ();
59
+ this .creationTime = determineCreationDate (file , fileAttributes );
60
+ this .length = file .length ();
61
+ }
62
+
63
+ private Optional <BasicFileAttributes > getFileAttributes (File file ) {
64
+ try {
65
+ return Optional .of (Files .readAttributes (file .toPath (), BasicFileAttributes .class ));
66
+ }
67
+ catch (IOException ex ) {
68
+ logger .log (Level .WARNING , "Failed to read attributes of file " + file + ". Reason: " + ex .getMessage ());
69
+ logger .log (Level .FINER , "Details: " , ex );
70
+ }
71
+ return Optional .empty ();
72
+ }
73
+
74
+ private long determineCreationDate (File file , Optional <BasicFileAttributes > fileAttributes ) {
75
+ if (fileAttributes .isPresent ()) {
76
+ return fileAttributes .get ().creationTime ().toMillis ();
77
+ }
78
+ else {
79
+ // Creation date is unavailable on unix based oS
80
+ return file .lastModified ();
81
+ }
52
82
}
53
83
54
84
public void setFileInformation (FileInformation other ) {
85
+ this .creationTime = other .creationTime ;
55
86
this .lastModified = other .lastModified ;
56
87
this .length = other .length ;
57
88
}
58
89
59
- /**
60
- * @see java.lang.Object#equals(java.lang.Object)
61
- */
90
+ @ Override
62
91
public boolean equals (Object other ) {
63
92
if (this == other ) {
64
93
return true ;
@@ -72,8 +101,15 @@ public boolean equals(Object other) {
72
101
73
102
FileInformation fileInfo = (FileInformation )other ;
74
103
75
- return fileInfo .lastModified == lastModified
76
- && fileInfo .length == length ;
104
+ return fileInfo .lastModified == lastModified && fileInfo .creationTime == creationTime && fileInfo .length == length ;
105
+ }
106
+
107
+ @ Override
108
+ public int hashCode () {
109
+ int result = (int ) (creationTime ^ (creationTime >>> 32 ));
110
+ result = 31 * result + (int ) (lastModified ^ (lastModified >>> 32 ));
111
+ result = 31 * result + (int ) (length ^ (length >>> 32 ));
112
+ return result ;
77
113
}
78
114
79
115
@ Override
@@ -189,6 +225,10 @@ public long getLastModified() {
189
225
return fileInformation .lastModified ;
190
226
}
191
227
228
+ public long getCreationTime () {
229
+ return fileInformation .creationTime ;
230
+ }
231
+
192
232
public URL getURL () {
193
233
return url ;
194
234
}
@@ -232,19 +272,28 @@ public void printDetailedInformation() {
232
272
233
273
private FileInformation readFileInformation (URL url ) {
234
274
FileInformation fileInformation = new FileInformation ();
235
- URLConnection urlConnection = null ;
275
+ URLConnection urlConnection ;
236
276
try {
237
- urlConnection = url .openConnection ();
238
277
if (url .getProtocol ().startsWith ("http" )) {
239
- ((HttpURLConnection )urlConnection ).setRequestMethod ("HEAD" );
278
+ urlConnection = url .openConnection ();
279
+ ((HttpURLConnection ) urlConnection ).setRequestMethod ("HEAD" );
280
+ try (InputStream inputStream = urlConnection .getInputStream ()) {
281
+ fileInformation .length = urlConnection .getContentLength ();
282
+ fileInformation .lastModified = urlConnection .getLastModified ();
283
+ }
240
284
}
241
- try (InputStream inputStream = urlConnection .getInputStream ()) {
242
- fileInformation .length = urlConnection .getContentLength ();
243
- fileInformation .lastModified = urlConnection .getLastModified ();
285
+ else {
286
+ if (url .getProtocol ().startsWith ("file" )) {
287
+ File file = new File (url .getFile ());
288
+ if (file .exists ()) {
289
+ fileInformation = new FileInformation (file );
290
+ }
291
+ }
244
292
}
245
293
}
246
294
catch (IOException e ) {
247
- if (LOG .isLoggable (Level .WARNING )) LOG .log (Level .WARNING , "Failed to obtain age and length of URL " + url , e );
295
+ if (LOG .isLoggable (Level .WARNING ))
296
+ LOG .log (Level .WARNING , "Failed to obtain age and length of URL " + url , e );
248
297
}
249
298
250
299
return fileInformation ;
@@ -257,7 +306,7 @@ public void setURL(URL url) {
257
306
258
307
public boolean isDifferent (File otherFile ) {
259
308
// we just ignore the file name for now...
260
- FileInformation fileInformation = new FileInformation (otherFile . lastModified (), otherFile . length () );
309
+ FileInformation fileInformation = new FileInformation (otherFile );
261
310
262
311
return !this .fileInformation .equals (fileInformation );
263
312
}
0 commit comments