23
23
import java .io .InputStream ;
24
24
import java .net .URL ;
25
25
import java .nio .file .attribute .FileTime ;
26
+ import java .util .function .Supplier ;
26
27
27
28
import org .apache .commons .io .IOUtils ;
28
29
import org .apache .commons .io .output .DeferredFileOutputStream ;
@@ -50,6 +51,9 @@ public class PlexusIoFileResource
50
51
@ Nonnull
51
52
private final PlexusIoResourceAttributes attributes ;
52
53
54
+ @ Nonnull
55
+ private final FileAttributes fileAttributes ;
56
+
53
57
private final ContentSupplier contentSupplier ;
54
58
55
59
private final DeferredFileOutputStream dfos ;
@@ -61,23 +65,41 @@ protected PlexusIoFileResource( @Nonnull File file, @Nonnull String name,
61
65
this ( file , name , attrs , null , null );
62
66
}
63
67
64
- @ SuppressWarnings ( "ConstantConditions" )
65
68
PlexusIoFileResource ( @ Nonnull final File file , @ Nonnull String name , @ Nonnull PlexusIoResourceAttributes attrs ,
66
69
final ContentSupplier contentSupplier , final InputStreamTransformer streamTransformer )
67
70
throws IOException
68
71
{
69
- super ( name , file .lastModified (), file .length (), file .isFile (), file .isDirectory (), file .exists () );
70
- this .file = file ;
72
+ this ( file , name , attrs ,
73
+ new FileAttributes ( file , true ),
74
+ contentSupplier , streamTransformer );
75
+ }
71
76
77
+ PlexusIoFileResource ( @ Nonnull final File file , @ Nonnull String name ,
78
+ @ Nonnull PlexusIoResourceAttributes attrs , @ Nonnull FileAttributes fileAttributes ,
79
+ final ContentSupplier contentSupplier , final InputStreamTransformer streamTransformer )
80
+ throws IOException
81
+ {
82
+ super ( name , fileAttributes .getLastModifiedTime ().toMillis (), fileAttributes .getSize (),
83
+ fileAttributes .isRegularFile (), fileAttributes .isDirectory (),
84
+ fileAttributes .isRegularFile () || fileAttributes .isDirectory () || fileAttributes .isSymbolicLink () || fileAttributes .isOther () );
85
+ this .file = file ;
86
+ this .attributes = requireNonNull ( attrs , "attributes is null for file " + file .getName () );
87
+ this .fileAttributes = requireNonNull ( fileAttributes , "fileAttributes is null for file " + file .getName () );
72
88
this .contentSupplier = contentSupplier != null ? contentSupplier : getRootContentSupplier ( file );
73
89
74
90
boolean hasTransformer = streamTransformer != null && streamTransformer != identityTransformer ;
75
91
InputStreamTransformer transToUse = streamTransformer != null ? streamTransformer : identityTransformer ;
76
92
77
93
dfos = hasTransformer && file .isFile () ? asDeferredStream ( this .contentSupplier , transToUse , this ) : null ;
78
- if ( attrs == null )
79
- throw new IllegalArgumentException ( "attrs is null for file " + file .getName () );
80
- this .attributes = attrs ;
94
+ }
95
+
96
+ private static <T > T requireNonNull ( T t , String message )
97
+ {
98
+ if ( t == null )
99
+ {
100
+ throw new IllegalArgumentException ( message );
101
+ }
102
+ return t ;
81
103
}
82
104
83
105
private static DeferredFileOutputStream asDeferredStream ( @ Nonnull ContentSupplier supplier ,
@@ -159,11 +181,7 @@ public long getSize()
159
181
{
160
182
if ( dfos == null )
161
183
{
162
- if ( attributes instanceof FileAttributes )
163
- {
164
- return ( ( FileAttributes ) attributes ).getSize ();
165
- }
166
- return getFile ().length ();
184
+ return fileAttributes .getSize ();
167
185
}
168
186
if ( dfos .isInMemory () )
169
187
{
@@ -177,11 +195,7 @@ public long getSize()
177
195
178
196
public boolean isDirectory ()
179
197
{
180
- if ( attributes instanceof FileAttributes )
181
- {
182
- return ( ( FileAttributes ) attributes ).isDirectory ();
183
- }
184
- return getFile ().isDirectory ();
198
+ return fileAttributes .isDirectory ();
185
199
}
186
200
187
201
public boolean isExisting ()
@@ -195,11 +209,7 @@ public boolean isExisting()
195
209
196
210
public boolean isFile ()
197
211
{
198
- if ( attributes instanceof FileAttributes )
199
- {
200
- return ( ( FileAttributes ) attributes ).isRegularFile ();
201
- }
202
- return getFile ().isFile ();
212
+ return fileAttributes .isRegularFile ();
203
213
}
204
214
205
215
@ Nonnull
@@ -208,15 +218,18 @@ public PlexusIoResourceAttributes getAttributes()
208
218
return attributes ;
209
219
}
210
220
221
+ @ Nonnull
222
+ public FileAttributes getFileAttributes ()
223
+ {
224
+ return fileAttributes ;
225
+ }
226
+
211
227
public long getLastModified ()
212
228
{
213
- if ( attributes instanceof FileAttributes )
229
+ FileTime lastModified = fileAttributes .getLastModifiedTime ();
230
+ if ( lastModified != null )
214
231
{
215
- FileTime lastModified = ( ( FileAttributes ) attributes ).getLastModifiedTime ();
216
- if ( lastModified != null )
217
- {
218
- return lastModified .toMillis ();
219
- }
232
+ return lastModified .toMillis ();
220
233
}
221
234
return AttributeUtils .getLastModified ( getFile () );
222
235
}
0 commit comments