Skip to content

Commit 09d3b40

Browse files
committed
Made directoryArchiver obey InputStream contract
1 parent a75229c commit 09d3b40

File tree

4 files changed

+41
-13
lines changed

4 files changed

+41
-13
lines changed

src/main/java/org/codehaus/plexus/archiver/AbstractArchiver.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ public void addSymlink(String symlinkName, int permissions, String symlinkDestin
355355
}
356356

357357
protected ArchiveEntry asArchiveEntry( @Nonnull final PlexusIoResource resource, final String destFileName,
358-
final int permissions )
358+
final int permissions, PlexusIoResourceCollection collection)
359359
throws ArchiverException
360360
{
361361
if ( !resource.isExisting() )
@@ -365,7 +365,7 @@ protected ArchiveEntry asArchiveEntry( @Nonnull final PlexusIoResource resource,
365365

366366
if ( resource.isFile() )
367367
{
368-
return ArchiveEntry.createFileEntry( destFileName, resource, permissions );
368+
return ArchiveEntry.createFileEntry( destFileName, resource, permissions, collection );
369369
}
370370
else
371371
{
@@ -392,7 +392,7 @@ protected ArchiveEntry asArchiveEntry( final PlexusIoResourceCollection collecti
392392
}
393393
}
394394

395-
return asArchiveEntry( resource, destFileName, permissions );
395+
return asArchiveEntry( resource, destFileName, permissions, collection );
396396
}
397397
catch ( final IOException e )
398398
{
@@ -403,7 +403,7 @@ protected ArchiveEntry asArchiveEntry( final PlexusIoResourceCollection collecti
403403
public void addResource( final PlexusIoResource resource, final String destFileName, final int permissions )
404404
throws ArchiverException
405405
{
406-
resources.add( asArchiveEntry( resource, destFileName, permissions ) );
406+
resources.add( asArchiveEntry( resource, destFileName, permissions, null ) );
407407
}
408408

409409
public void addFile( @Nonnull final File inputFile, @Nonnull String destFileName, int permissions )

src/main/java/org/codehaus/plexus/archiver/ArchiveEntry.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributes;
3030
import org.codehaus.plexus.components.io.resources.PlexusIoFileResource;
3131
import org.codehaus.plexus.components.io.resources.PlexusIoResource;
32+
import org.codehaus.plexus.components.io.resources.PlexusIoResourceCollection;
3233
import org.codehaus.plexus.components.io.resources.PlexusIoResourceWithAttributes;
3334

3435
/**
@@ -43,6 +44,9 @@ public class ArchiveEntry
4344
public static final int DIRECTORY = 2;
4445

4546
public static final int SYMLINK = 3;
47+
48+
private PlexusIoResourceCollection collection;
49+
4650
@Nonnull private PlexusIoResource resource;
4751

4852
private final String name;
@@ -59,11 +63,14 @@ public class ArchiveEntry
5963
* @param resource original filename
6064
* @param type FILE or DIRECTORY
6165
* @param mode octal unix style permissions
66+
* @param collection
6267
*/
63-
private ArchiveEntry( String name, @Nonnull PlexusIoResource resource, int type, int mode )
68+
private ArchiveEntry( String name, @Nonnull PlexusIoResource resource, int type, int mode,
69+
PlexusIoResourceCollection collection )
6470
{
6571
this.name = name;
6672
this.resource = resource;
73+
this.collection = collection;
6774
this.attributes = ( resource instanceof PlexusIoResourceWithAttributes )
6875
? ( (PlexusIoResourceWithAttributes) resource ).getAttributes() : null;
6976
this.type = type;
@@ -108,7 +115,7 @@ public File getFile()
108115
public InputStream getInputStream()
109116
throws IOException
110117
{
111-
return resource.getContents();
118+
return collection != null ? collection.getInputStream( resource ) : resource.getContents();
112119
}
113120

114121
/**
@@ -139,15 +146,16 @@ public int getMode()
139146
( type == FILE ? UnixStat.FILE_FLAG : type == SYMLINK ? UnixStat.LINK_FLAG : UnixStat.DIR_FLAG );
140147
}
141148

142-
public static ArchiveEntry createFileEntry( String target, PlexusIoResource resource, int permissions )
149+
public static ArchiveEntry createFileEntry( String target, PlexusIoResource resource, int permissions,
150+
PlexusIoResourceCollection collection )
143151
throws ArchiverException
144152
{
145153
if ( resource.isDirectory() )
146154
{
147155
throw new ArchiverException( "Not a file: " + resource.getName() );
148156
}
149157
final int type = resource.isSymbolicLink() ? SYMLINK : FILE;
150-
return new ArchiveEntry( target, resource, type, permissions );
158+
return new ArchiveEntry( target, resource, type, permissions, collection );
151159
}
152160

153161
public static ArchiveEntry createFileEntry( String target, File file, int permissions )
@@ -169,7 +177,7 @@ public static ArchiveEntry createFileEntry( String target, File file, int permis
169177
}
170178

171179
final PlexusIoFileResource res = PlexusIoFileResource.justAFile( file, attrs );
172-
return new ArchiveEntry( target, res, FILE, permissions );
180+
return new ArchiveEntry( target, res, FILE, permissions, null );
173181
}
174182

175183
public static ArchiveEntry createDirectoryEntry( String target, @Nonnull PlexusIoResource resource, int permissions )
@@ -180,7 +188,7 @@ public static ArchiveEntry createDirectoryEntry( String target, @Nonnull PlexusI
180188
throw new ArchiverException( "Not a directory: " + resource.getName() );
181189
}
182190
final int type = resource.isSymbolicLink() ? SYMLINK : DIRECTORY;
183-
return new ArchiveEntry( target, resource, type, permissions );
191+
return new ArchiveEntry( target, resource, type, permissions, null );
184192
}
185193

186194
public static ArchiveEntry createDirectoryEntry( String target, final File file, int permissions )
@@ -192,7 +200,7 @@ public static ArchiveEntry createDirectoryEntry( String target, final File file,
192200
}
193201

194202
final PlexusIoFileResource res = new PlexusIoFileResource( file, ArchiverAttributeUtils.getFileAttributes(file));
195-
return new ArchiveEntry( target, res, DIRECTORY, permissions );
203+
return new ArchiveEntry( target, res, DIRECTORY, permissions, null );
196204
}
197205

198206
public static ArchiveEntry createEntry( String target, File file, int filePerm, int dirPerm )
@@ -215,7 +223,8 @@ else if ( file.isFile() )
215223
public static ArchiveEntry createSymlinkEntry(String symlinkName, int permissions, String symlinkDestination)
216224
{
217225
File symlinkFile = new File(symlinkName);
218-
final ArchiveEntry archiveEntry = new ArchiveEntry(symlinkName, new PlexusIoVirtualSymlinkResource(symlinkFile, symlinkDestination), SYMLINK, permissions);
226+
final ArchiveEntry archiveEntry = new ArchiveEntry(symlinkName, new PlexusIoVirtualSymlinkResource(symlinkFile, symlinkDestination), SYMLINK, permissions,
227+
null );
219228
return archiveEntry;
220229
}
221230

src/main/java/org/codehaus/plexus/archiver/dir/DirectoryArchiver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ protected void copyFile( final ArchiveEntry entry, final String vPath )
131131
throw new ArchiverException( "Unable to create directory or parent directory of " + outFile );
132132
}
133133
}
134-
ResourceUtils.copyFile( in, outFile );
134+
ResourceUtils.copyFile( entry.getInputStream(), outFile );
135135

136136
if ( !isIgnorePermissions() )
137137
{

src/main/java/org/codehaus/plexus/archiver/util/ResourceUtils.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,25 @@ public static void copyFile( PlexusIoResource in, File outFile )
9292
}
9393
}
9494

95+
/**
96+
* Copies the sources contents to the given destination file.
97+
*/
98+
public static void copyFile( InputStream input, File outFile )
99+
throws IOException
100+
{
101+
OutputStream output = null;
102+
try
103+
{
104+
output = new FileOutputStream( outFile );
105+
IOUtil.copy( input, output );
106+
}
107+
finally
108+
{
109+
IOUtil.close( input );
110+
IOUtil.close( output );
111+
}
112+
}
113+
95114
/**
96115
* Checks, whether the resource and the file are identical.
97116
*/

0 commit comments

Comments
 (0)