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