View Javadoc
1   package org.codehaus.plexus.archiver.util;
2   
3   import java.io.File;
4   import java.io.FileOutputStream;
5   import java.io.IOException;
6   import java.io.InputStream;
7   import java.io.OutputStream;
8   import org.codehaus.plexus.components.io.functions.FileSupplier;
9   import org.codehaus.plexus.components.io.resources.PlexusIoResource;
10  import org.codehaus.plexus.util.IOUtil;
11  
12  /**
13   * Utility class for work with {@link PlexusIoResource} instances.
14   */
15  public class ResourceUtils
16  {
17  
18      /**
19       * Private constructor, to prevent accidental implementation.
20       */
21      private ResourceUtils()
22      {
23          // Does nothing
24      }
25  
26      /**
27       * Queries, whether the given source is up-to-date relative to
28       * the given destination.
29       */
30      public static boolean isUptodate( PlexusIoResource source, File destination )
31      {
32          return isUptodate( source, destination.lastModified() );
33      }
34  
35      /**
36       * Queries, whether the given source is up-to-date relative to
37       * the given modification date.
38       */
39      public static boolean isUptodate( PlexusIoResource source, long destinationDate )
40      {
41          final long s = source.getLastModified();
42          if ( s == PlexusIoResource.UNKNOWN_MODIFICATION_DATE )
43          {
44              return false;
45          }
46  
47          if ( destinationDate == 0 )
48          {
49              return false;
50          }
51  
52          return destinationDate > s;
53      }
54  
55      /**
56       * Queries, whether the given source is up-to-date relative to
57       * the given modification date.
58       */
59      public static boolean isUptodate( long sourceDate, long destinationDate )
60      {
61          if ( sourceDate == PlexusIoResource.UNKNOWN_MODIFICATION_DATE )
62          {
63              return false;
64          }
65  
66          if ( destinationDate == 0 )
67          {
68              return false;
69          }
70  
71          return destinationDate > sourceDate;
72      }
73  
74      /**
75       * Copies the sources contents to the given destination file.
76       */
77      public static void copyFile( PlexusIoResource in, File outFile )
78          throws IOException
79      {
80          InputStream input = null;
81          OutputStream output = null;
82          try
83          {
84              input = in.getContents();
85              output = new FileOutputStream( outFile );
86              IOUtil.copy( input, output );
87              output.close();
88              output = null;
89              input.close();
90              input = null;
91          }
92          finally
93          {
94              IOUtil.close( input );
95              IOUtil.close( output );
96          }
97      }
98  
99      /**
100      * Copies the sources contents to the given destination file.
101      */
102     public static void copyFile( InputStream input, File outFile )
103         throws IOException
104     {
105         OutputStream output = null;
106         try
107         {
108             output = new FileOutputStream( outFile );
109             IOUtil.copy( input, output );
110             output.close();
111             output = null;
112             input.close();
113             input = null;
114         }
115         finally
116         {
117             IOUtil.close( input );
118             IOUtil.close( output );
119         }
120     }
121 
122     /**
123      * Checks, whether the resource and the file are identical.
124      */
125     public static boolean isSame( PlexusIoResource resource, File file )
126     {
127         if ( resource instanceof FileSupplier )
128         {
129             File resourceFile = ( (FileSupplier) resource ).getFile();
130             return file.equals( resourceFile );
131         }
132         return false;
133     }
134 
135     /**
136      * Checks, whether the resource and the file are identical.
137      * Uses {@link File#getCanonicalFile()} for comparison, which is much
138      * slower than comparing the files.
139      */
140     public static boolean isCanonicalizedSame( PlexusIoResource resource, File file )
141         throws IOException
142     {
143         if ( resource instanceof FileSupplier )
144         {
145             File resourceFile = ( (FileSupplier) resource ).getFile();
146             return file.getCanonicalFile().equals( resourceFile.getCanonicalFile() );
147         }
148         return false;
149     }
150 
151 }