Coverage Report - org.codehaus.plexus.archiver.util.ResourceUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ResourceUtils
47%
22/46
50%
8/16
2.5
 
 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  0
     {
 23  
         // Does nothing
 24  0
     }
 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  0
         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  0
         final long s = source.getLastModified();
 42  0
         if ( s == PlexusIoResource.UNKNOWN_MODIFICATION_DATE )
 43  
         {
 44  0
             return false;
 45  
         }
 46  
 
 47  0
         if ( destinationDate == 0 )
 48  
         {
 49  0
             return false;
 50  
         }
 51  
 
 52  0
         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  19
         if ( sourceDate == PlexusIoResource.UNKNOWN_MODIFICATION_DATE )
 62  
         {
 63  0
             return false;
 64  
         }
 65  
 
 66  19
         if ( destinationDate == 0 )
 67  
         {
 68  1
             return false;
 69  
         }
 70  
 
 71  18
         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  0
         InputStream input = null;
 81  0
         OutputStream output = null;
 82  
         try
 83  
         {
 84  0
             input = in.getContents();
 85  0
             output = new FileOutputStream( outFile );
 86  0
             IOUtil.copy( input, output );
 87  0
             output.close();
 88  0
             output = null;
 89  0
             input.close();
 90  0
             input = null;
 91  
         }
 92  
         finally
 93  
         {
 94  0
             IOUtil.close( input );
 95  0
             IOUtil.close( output );
 96  0
         }
 97  0
     }
 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  10
         OutputStream output = null;
 106  
         try
 107  
         {
 108  10
             output = new FileOutputStream( outFile );
 109  10
             IOUtil.copy( input, output );
 110  10
             output.close();
 111  10
             output = null;
 112  10
             input.close();
 113  10
             input = null;
 114  
         }
 115  
         finally
 116  
         {
 117  10
             IOUtil.close( input );
 118  10
             IOUtil.close( output );
 119  10
         }
 120  10
     }
 121  
 
 122  
     /**
 123  
      * Checks, whether the resource and the file are identical.
 124  
      */
 125  
     public static boolean isSame( PlexusIoResource resource, File file )
 126  
     {
 127  51501
         if ( resource instanceof FileSupplier )
 128  
         {
 129  49818
             File resourceFile = ( (FileSupplier) resource ).getFile();
 130  49818
             return file.equals( resourceFile );
 131  
         }
 132  1683
         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  1
         if ( resource instanceof FileSupplier )
 144  
         {
 145  1
             File resourceFile = ( (FileSupplier) resource ).getFile();
 146  1
             return file.getCanonicalFile().equals( resourceFile.getCanonicalFile() );
 147  
         }
 148  0
         return false;
 149  
     }
 150  
 
 151  
 }