1 package org.codehaus.plexus.archiver.util;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.nio.file.Files;
7
8 import org.codehaus.plexus.components.io.functions.FileSupplier;
9 import org.codehaus.plexus.components.io.resources.PlexusIoResource;
10
11 import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
12
13
14
15
16 public class ResourceUtils {
17
18
19
20
21 private ResourceUtils() {
22
23 }
24
25
26
27
28
29 public static boolean isUptodate(PlexusIoResource source, File destination) {
30 return isUptodate(source, destination.lastModified());
31 }
32
33
34
35
36
37 public static boolean isUptodate(PlexusIoResource source, long destinationDate) {
38 return isUptodate(source.getLastModified(), destinationDate);
39 }
40
41
42
43
44
45 public static boolean isUptodate(long sourceDate, long destinationDate) {
46 if (sourceDate == PlexusIoResource.UNKNOWN_MODIFICATION_DATE) {
47 return false;
48 }
49
50 if (destinationDate == 0) {
51 return false;
52 }
53
54 return destinationDate >= sourceDate;
55 }
56
57
58
59
60 public static void copyFile(PlexusIoResource in, File outFile) throws IOException {
61 try (InputStream input = in.getContents()) {
62 Files.copy(input, outFile.toPath(), REPLACE_EXISTING);
63 }
64 }
65
66
67
68
69 public static void copyFile(InputStream input, File outFile) throws IOException {
70 Files.copy(input, outFile.toPath(), REPLACE_EXISTING);
71 }
72
73
74
75
76 public static boolean isSame(PlexusIoResource resource, File file) {
77 if (resource instanceof FileSupplier) {
78 File resourceFile = ((FileSupplier) resource).getFile();
79 return file.equals(resourceFile);
80 }
81 return false;
82 }
83
84
85
86
87
88
89 public static boolean isCanonicalizedSame(PlexusIoResource resource, File file) throws IOException {
90 if (resource instanceof FileSupplier) {
91 File resourceFile = ((FileSupplier) resource).getFile();
92 return file.getCanonicalFile().equals(resourceFile.getCanonicalFile());
93 }
94 return false;
95 }
96 }