Skip to content

Commit bb57356

Browse files
committed
Fixed resource leaks, ensured consistent buffering
1 parent 5afea5a commit bb57356

29 files changed

+433
-159
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@
6868
<version>4.11</version>
6969
<scope>test</scope>
7070
</dependency>
71+
<dependency>
72+
<groupId>com.google.code.findbugs</groupId>
73+
<artifactId>jsr305</artifactId>
74+
<version>3.0.0</version>
75+
<scope>provided</scope>
76+
</dependency>
7177
</dependencies>
7278

7379
<build>

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* limitations under the License.
1818
*/
1919

20+
import java.io.Closeable;
2021
import java.io.File;
2122
import java.io.FileInputStream;
2223
import java.io.IOException;
@@ -985,7 +986,15 @@ protected abstract void close()
985986
throws IOException;
986987

987988
protected void cleanUp()
989+
throws IOException
988990
{
991+
for ( Object resource : resources )
992+
{
993+
if (resource instanceof PlexusIoProxyResourceCollection){
994+
resource = ( (PlexusIoProxyResourceCollection) resource ).getSrc();
995+
}
996+
if (resource instanceof Closeable ) ((Closeable)resource).close();
997+
}
989998
resources.clear();
990999
}
9911000

src/main/java/org/codehaus/plexus/archiver/bzip2/BZip2Compressor.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
import org.codehaus.plexus.archiver.util.Compressor;
2323
import org.codehaus.plexus.util.IOUtil;
2424

25-
import java.io.BufferedOutputStream;
26-
import java.io.FileOutputStream;
2725
import java.io.IOException;
26+
import java.io.OutputStream;
27+
28+
import static org.codehaus.plexus.archiver.util.Streams.bufferedOutputStream;
29+
import static org.codehaus.plexus.archiver.util.Streams.fileOutputStream;
2830

2931
/**
3032
* @version $Revision$ $Date$
@@ -42,9 +44,7 @@ public void compress()
4244
{
4345
try
4446
{
45-
BufferedOutputStream bos =
46-
new BufferedOutputStream( new FileOutputStream( getDestFile() ) );
47-
zOut = new BZip2CompressorOutputStream( bos );
47+
zOut = new BZip2CompressorOutputStream( bufferedOutputStream( fileOutputStream( getDestFile() ) ) );
4848
compress( getSource(), zOut );
4949
}
5050
catch ( IOException ioe )

src/main/java/org/codehaus/plexus/archiver/bzip2/BZip2UnArchiver.java

Lines changed: 22 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@
1717
* limitations under the License.
1818
*/
1919

20-
import java.io.BufferedInputStream;
2120
import java.io.File;
22-
import java.io.FileInputStream;
23-
import java.io.FileOutputStream;
2421
import java.io.IOException;
2522
import java.io.InputStream;
2623

2724
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
2825
import org.codehaus.plexus.archiver.AbstractUnArchiver;
2926
import org.codehaus.plexus.archiver.ArchiverException;
30-
import org.codehaus.plexus.util.IOUtil;
27+
28+
import javax.annotation.Nonnull;
29+
30+
import static org.codehaus.plexus.archiver.util.Streams.*;
3131

3232
/**
3333
* @author <a href="mailto:evenisse@codehaus.org">Emmanuel Venisse</a>
@@ -36,6 +36,8 @@
3636
public class BZip2UnArchiver
3737
extends AbstractUnArchiver
3838
{
39+
private final static String OPERATION_BZIP2 = "bzip2";
40+
3941
public BZip2UnArchiver()
4042
{
4143
}
@@ -50,51 +52,27 @@ protected void execute()
5052
{
5153
if ( getSourceFile().lastModified() > getDestFile().lastModified() )
5254
{
53-
getLogger().info( "Expanding " + getSourceFile().getAbsolutePath() + " to "
54-
+ getDestFile().getAbsolutePath() );
55+
getLogger().info(
56+
"Expanding " + getSourceFile().getAbsolutePath() + " to " + getDestFile().getAbsolutePath() );
5557

56-
FileOutputStream out = null;
57-
BZip2CompressorInputStream zIn = null;
58-
FileInputStream fis = null;
59-
BufferedInputStream bis = null;
60-
try
61-
{
62-
out = new FileOutputStream( getDestFile() );
63-
fis = new FileInputStream( getSourceFile() );
64-
bis = new BufferedInputStream( fis );
65-
zIn = getBZip2InputStream( bis );
66-
if ( zIn == null )
67-
{
68-
throw new ArchiverException( getSourceFile().getAbsolutePath() + " is an invalid bz2 file." );
69-
}
70-
byte[] buffer = new byte[8 * 1024];
71-
int count = 0;
72-
do
73-
{
74-
out.write( buffer, 0, count );
75-
count = zIn.read( buffer, 0, buffer.length );
76-
}
77-
while ( count != -1 );
78-
}
79-
catch ( IOException ioe )
80-
{
81-
String msg = "Problem expanding bzip2 " + ioe.getMessage();
82-
throw new ArchiverException( msg, ioe );
83-
}
84-
finally
85-
{
86-
IOUtil.close( bis );
87-
IOUtil.close( fis );
88-
IOUtil.close( out );
89-
IOUtil.close( zIn );
90-
}
58+
copyFully( getBZip2InputStream( bufferedInputStream( fileInputStream( getSourceFile(), OPERATION_BZIP2 ) ) ),
59+
bufferedOutputStream( fileOutputStream( getDestFile(), OPERATION_BZIP2 ) ), OPERATION_BZIP2 );
9160
}
9261
}
9362

94-
public static BZip2CompressorInputStream getBZip2InputStream( InputStream bis )
95-
throws IOException
63+
public static
64+
@Nonnull
65+
BZip2CompressorInputStream getBZip2InputStream( InputStream bis )
66+
throws ArchiverException
9667
{
97-
return new BZip2CompressorInputStream( bis );
68+
try
69+
{
70+
return new BZip2CompressorInputStream( bis );
71+
}
72+
catch ( IOException e )
73+
{
74+
throw new ArchiverException( "Trouble creating BZIP2 compressor, invalid file ?", e );
75+
}
9876
}
9977

10078
protected void execute( String path, File outputDirectory )

src/main/java/org/codehaus/plexus/archiver/bzip2/PlexusIoBzip2ResourceCollection.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
import java.io.InputStream;
77
import java.util.HashMap;
88

9+
import org.codehaus.plexus.archiver.util.Streams;
910
import org.codehaus.plexus.components.io.attributes.Java7FileAttributes;
1011
import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributes;
1112
import org.codehaus.plexus.components.io.resources.PlexusIoCompressedFileResourceCollection;
1213
import org.codehaus.plexus.components.io.resources.PlexusIoResourceCollection;
1314
import org.codehaus.plexus.util.IOUtil;
1415

16+
import javax.annotation.WillNotClose;
17+
1518

1619
/**
1720
* Implementation of {@link PlexusIoResourceCollection} for
@@ -20,18 +23,13 @@
2023
public class PlexusIoBzip2ResourceCollection
2124
extends PlexusIoCompressedFileResourceCollection
2225
{
23-
protected InputStream getInputStream( File file )
26+
protected @WillNotClose InputStream getInputStream( File file )
2427
throws IOException
2528
{
2629
InputStream fis = new FileInputStream( file );
2730
try
2831
{
2932
final InputStream result = BZip2UnArchiver.getBZip2InputStream( fis );
30-
if ( result == null )
31-
{
32-
throw new IOException( file.getPath()
33-
+ " is an invalid bzip2 file. " );
34-
}
3533
fis = null;
3634
return result;
3735
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class DirectoryArchiver
3434
extends AbstractArchiver
3535
{
3636
public void resetArchiver()
37+
throws IOException
3738
{
3839
cleanUp();
3940
}
@@ -161,6 +162,7 @@ else if ( !outFile.mkdirs() )
161162
}
162163

163164
protected void cleanUp()
165+
throws IOException
164166
{
165167
super.cleanUp();
166168
setIncludeEmptyDirs( false );

src/main/java/org/codehaus/plexus/archiver/ear/EarArchiver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void addArchives( File directoryName, String[] includes, String[] exclude
8080
}
8181

8282
protected void initZipOutputStream( ZipArchiveOutputStream zOut )
83-
throws IOException, ArchiverException
83+
throws ArchiverException, IOException
8484
{
8585
// If no webxml file is specified, it's an error.
8686
if ( deploymentDescriptor == null && !isInUpdateMode() )
@@ -129,6 +129,7 @@ protected void zipFile( ArchiveEntry entry, ZipArchiveOutputStream zOut, String
129129
* time this task gets executed.
130130
*/
131131
protected void cleanUp()
132+
throws IOException
132133
{
133134
descriptorAdded = false;
134135
super.cleanUp();

src/main/java/org/codehaus/plexus/archiver/gzip/GZipCompressor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919

2020
import org.codehaus.plexus.archiver.ArchiverException;
21+
import org.codehaus.plexus.archiver.util.Streams;
2122
import org.codehaus.plexus.archiver.util.Compressor;
2223
import org.codehaus.plexus.util.IOUtil;
2324

@@ -41,7 +42,7 @@ public void compress()
4142
{
4243
try
4344
{
44-
zOut = new GZIPOutputStream( new FileOutputStream( getDestFile() ) );
45+
zOut = new GZIPOutputStream( Streams.bufferedOutputStream( new FileOutputStream( getDestFile() ) ));
4546
compress( getSource(), zOut );
4647
}
4748
catch ( IOException ioe )

src/main/java/org/codehaus/plexus/archiver/gzip/GZipUnArchiver.java

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,24 @@
1919

2020
import org.codehaus.plexus.archiver.AbstractUnArchiver;
2121
import org.codehaus.plexus.archiver.ArchiverException;
22-
import org.codehaus.plexus.util.IOUtil;
2322

2423
import java.io.FileInputStream;
25-
import java.io.FileOutputStream;
26-
import java.io.IOException;
2724
import java.io.File;
25+
import java.io.IOException;
2826
import java.util.zip.GZIPInputStream;
2927

28+
import static org.codehaus.plexus.archiver.util.Streams.*;
29+
3030
/**
3131
* @author <a href="mailto:evenisse@codehaus.org">Emmanuel Venisse</a>
3232
* @version $Revision$ $Date$
3333
*/
3434
public class GZipUnArchiver
3535
extends AbstractUnArchiver
3636
{
37+
private static final String OPERATION_GZIP = "gzip";
38+
39+
3740
public GZipUnArchiver()
3841
{
3942
}
@@ -48,37 +51,24 @@ protected void execute()
4851
{
4952
if ( getSourceFile().lastModified() > getDestFile().lastModified() )
5053
{
51-
getLogger().info( "Expanding " + getSourceFile().getAbsolutePath() + " to "
52-
+ getDestFile().getAbsolutePath() );
54+
getLogger().info(
55+
"Expanding " + getSourceFile().getAbsolutePath() + " to " + getDestFile().getAbsolutePath() );
5356

54-
FileOutputStream out = null;
55-
GZIPInputStream zIn = null;
56-
FileInputStream fis = null;
57-
try
58-
{
59-
out = new FileOutputStream( getDestFile() );
60-
fis = new FileInputStream( getSourceFile() );
61-
zIn = new GZIPInputStream( fis );
62-
byte[] buffer = new byte[8 * 1024];
63-
int count = 0;
64-
do
65-
{
66-
out.write( buffer, 0, count );
67-
count = zIn.read( buffer, 0, buffer.length );
68-
}
69-
while ( count != -1 );
70-
}
71-
catch ( IOException ioe )
72-
{
73-
String msg = "Problem expanding gzip " + ioe.getMessage();
74-
throw new ArchiverException( msg, ioe );
75-
}
76-
finally
77-
{
78-
IOUtil.close( fis );
79-
IOUtil.close( out );
80-
IOUtil.close( zIn );
81-
}
57+
copyFully( getGzipInputStream( fileInputStream( getSourceFile(), OPERATION_GZIP ) ),
58+
fileOutputStream( getDestFile(), OPERATION_GZIP ), OPERATION_GZIP );
59+
}
60+
}
61+
62+
private GZIPInputStream getGzipInputStream( FileInputStream in )
63+
throws ArchiverException
64+
{
65+
try
66+
{
67+
return new GZIPInputStream( in );
68+
}
69+
catch ( IOException e )
70+
{
71+
throw new ArchiverException( "Problem creating GZIP input stream", e );
8272
}
8373
}
8474

src/main/java/org/codehaus/plexus/archiver/jar/JarArchiver.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@
1717
* limitations under the License.
1818
*/
1919

20-
import java.io.BufferedOutputStream;
20+
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
21+
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
22+
import org.codehaus.plexus.archiver.ArchiverException;
23+
import org.codehaus.plexus.archiver.zip.ZipArchiver;
24+
import org.codehaus.plexus.logging.Logger;
25+
import org.codehaus.plexus.logging.console.ConsoleLogger;
26+
import org.codehaus.plexus.util.IOUtil;
27+
2128
import java.io.ByteArrayInputStream;
2229
import java.io.ByteArrayOutputStream;
2330
import java.io.File;
2431
import java.io.FileInputStream;
25-
import java.io.FileOutputStream;
2632
import java.io.IOException;
2733
import java.io.InputStream;
2834
import java.io.OutputStreamWriter;
@@ -40,13 +46,8 @@
4046
import java.util.TreeMap;
4147
import java.util.Vector;
4248

43-
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
44-
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
45-
import org.codehaus.plexus.archiver.ArchiverException;
46-
import org.codehaus.plexus.archiver.zip.ZipArchiver;
47-
import org.codehaus.plexus.logging.Logger;
48-
import org.codehaus.plexus.logging.console.ConsoleLogger;
49-
import org.codehaus.plexus.util.IOUtil;
49+
import static org.codehaus.plexus.archiver.util.Streams.bufferedOutputStream;
50+
import static org.codehaus.plexus.archiver.util.Streams.fileOutputStream;
5051

5152
/**
5253
* Base class for tasks that build archives in JAR file format.
@@ -284,7 +285,7 @@ public void addConfiguredIndexJars( File indexJar )
284285
}
285286

286287
protected void initZipOutputStream( ZipArchiveOutputStream zOut )
287-
throws IOException, ArchiverException
288+
throws ArchiverException, IOException
288289
{
289290
if ( !skipWriting )
290291
{
@@ -537,8 +538,7 @@ protected boolean createEmptyZip( File zipFile )
537538
try
538539
{
539540
getLogger().debug( "Building MANIFEST-only jar: " + getDestFile().getAbsolutePath() );
540-
FileOutputStream out = new FileOutputStream( getDestFile() );
541-
zOut = new ZipArchiveOutputStream( new BufferedOutputStream( out, 65536) );
541+
zOut = new ZipArchiveOutputStream( bufferedOutputStream( fileOutputStream( getDestFile(), "jar" ) ));
542542

543543
zOut.setEncoding( getEncoding() );
544544
if ( isCompress() )
@@ -572,6 +572,7 @@ protected boolean createEmptyZip( File zipFile )
572572
* @see ZipArchiver#cleanUp
573573
*/
574574
protected void cleanUp()
575+
throws IOException
575576
{
576577
super.cleanUp();
577578

0 commit comments

Comments
 (0)