View Javadoc
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   * Utility class for work with {@link PlexusIoResource} instances.
15   */
16  public class ResourceUtils {
17  
18      /**
19       * Private constructor, to prevent accidental implementation.
20       */
21      private ResourceUtils() {
22          // Does nothing
23      }
24  
25      /**
26       * Queries, whether the given source is up-to-date relative to
27       * the given destination.
28       */
29      public static boolean isUptodate(PlexusIoResource source, File destination) {
30          return isUptodate(source, destination.lastModified());
31      }
32  
33      /**
34       * Queries, whether the given source is up-to-date relative to
35       * the given modification date.
36       */
37      public static boolean isUptodate(PlexusIoResource source, long destinationDate) {
38          return isUptodate(source.getLastModified(), destinationDate);
39      }
40  
41      /**
42       * Queries, whether the given source is up-to-date relative to
43       * the given modification date.
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       * Copies the sources contents to the given destination file.
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       * Copies the sources contents to the given destination file.
68       */
69      public static void copyFile(InputStream input, File outFile) throws IOException {
70          Files.copy(input, outFile.toPath(), REPLACE_EXISTING);
71      }
72  
73      /**
74       * Checks, whether the resource and the file are identical.
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       * Checks, whether the resource and the file are identical.
86       * Uses {@link File#getCanonicalFile()} for comparison, which is much
87       * slower than comparing the files.
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  }