Skip to content

Commit bd5cc61

Browse files
satamasplamentotev
authored andcommitted
Refactor: use try with resources pattern
Closes #116
1 parent 708733c commit bd5cc61

13 files changed

+74
-233
lines changed

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

+1-10
Original file line numberDiff line numberDiff line change
@@ -365,18 +365,9 @@ else if ( isDirectory )
365365
}
366366
else
367367
{
368-
OutputStream out = null;
369-
try
368+
try ( OutputStream out = new FileOutputStream( f ) )
370369
{
371-
out = new FileOutputStream( f );
372-
373370
IOUtil.copy( compressedInputStream, out );
374-
out.close();
375-
out = null;
376-
}
377-
finally
378-
{
379-
IOUtil.close( out );
380371
}
381372
}
382373

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

+20-27
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import java.util.Collections;
88
import java.util.List;
99
import org.codehaus.plexus.util.FileUtils;
10-
import org.codehaus.plexus.util.IOUtil;
1110
import org.codehaus.plexus.util.StringUtils;
1211

1312
/**
@@ -43,56 +42,50 @@ public DotDirectiveArchiveFinalizer( File dotFileDirectory, String dotFilePrefix
4342
public void finalizeArchiveCreation( Archiver archiver )
4443
throws ArchiverException
4544
{
46-
BufferedReader in = null;
4745
try
4846
{
4947
List<File> dotFiles = FileUtils.getFiles( dotFileDirectory, dotFilePrefix + "*", null );
5048

5149
for ( File dotFile : dotFiles )
5250
{
53-
in = new BufferedReader( new FileReader( dotFile ) );
54-
55-
for ( String line = in.readLine(); line != null; line = in.readLine() )
51+
try ( BufferedReader in = new BufferedReader( new FileReader( dotFile ) ) )
5652
{
57-
String[] s = StringUtils.split( line, ":" );
5853

59-
if ( s.length == 1 )
54+
for ( String line = in.readLine(); line != null; line = in.readLine() )
6055
{
61-
File directory = new File( dotFileDirectory, s[0] );
62-
63-
System.out.println( "adding directory = " + directory );
56+
String[] s = StringUtils.split( line, ":" );
6457

65-
archiver.addDirectory( directory );
66-
}
67-
else
68-
{
69-
File directory = new File( dotFileDirectory, s[0] );
58+
if ( s.length == 1 )
59+
{
60+
File directory = new File( dotFileDirectory, s[0] );
7061

71-
System.out.println( "adding directory = " + directory + " to: " + s[1] );
62+
System.out.println( "adding directory = " + directory );
7263

73-
if ( s[1].endsWith( "/" ) )
74-
{
75-
archiver.addDirectory( directory, s[1] );
64+
archiver.addDirectory( directory );
7665
}
7766
else
7867
{
79-
archiver.addDirectory( directory, s[1] + "/" );
68+
File directory = new File( dotFileDirectory, s[0] );
69+
70+
System.out.println( "adding directory = " + directory + " to: " + s[1] );
71+
72+
if ( s[1].endsWith( "/" ) )
73+
{
74+
archiver.addDirectory( directory, s[1] );
75+
}
76+
else
77+
{
78+
archiver.addDirectory( directory, s[1] + "/" );
79+
}
8080
}
8181
}
8282
}
83-
84-
in.close();
85-
in = null;
8683
}
8784
}
8885
catch ( IOException e )
8986
{
9087
throw new ArchiverException( "Error processing dot files.", e );
9188
}
92-
finally
93-
{
94-
IOUtil.close( in );
95-
}
9689
}
9790

9891
@Override

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

+4-22
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@
3737
import java.util.Vector;
3838
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
3939
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
40+
import org.apache.commons.compress.archivers.zip.ZipFile;
4041
import org.apache.commons.compress.parallel.InputStreamSupplier;
4142
import org.codehaus.plexus.archiver.ArchiverException;
4243
import org.codehaus.plexus.archiver.zip.ConcurrentJarCreator;
4344
import org.codehaus.plexus.archiver.zip.ZipArchiver;
4445
import org.codehaus.plexus.logging.Logger;
4546
import org.codehaus.plexus.logging.console.ConsoleLogger;
46-
import org.codehaus.plexus.util.IOUtil;
4747
import static org.codehaus.plexus.archiver.util.Streams.bufferedOutputStream;
4848
import static org.codehaus.plexus.archiver.util.Streams.fileOutputStream;
4949

@@ -238,24 +238,15 @@ public void setManifest( File manifestFile )
238238
private Manifest getManifest( File manifestFile )
239239
throws ArchiverException
240240
{
241-
InputStream in = null;
242-
try
241+
try ( InputStream in = new FileInputStream( manifestFile ) )
243242
{
244-
in = new FileInputStream( manifestFile );
245-
final Manifest mf = getManifest( in );
246-
in.close();
247-
in = null;
248-
return mf;
243+
return getManifest( in );
249244
}
250245
catch ( IOException e )
251246
{
252247
throw new ArchiverException( "Unable to read manifest file: " + manifestFile + " (" + e.getMessage() + ")",
253248
e );
254249
}
255-
finally
256-
{
257-
IOUtil.close( in );
258-
}
259250
}
260251

261252
private Manifest getManifest( InputStream is )
@@ -799,10 +790,8 @@ else if ( zipFile.isDirectory() )
799790
}
800791
else
801792
{
802-
org.apache.commons.compress.archivers.zip.ZipFile zf = null;
803-
try
793+
try ( ZipFile zf = new ZipFile( file, "utf-8" ) )
804794
{
805-
zf = new org.apache.commons.compress.archivers.zip.ZipFile( file, "utf-8" );
806795
Enumeration<ZipArchiveEntry> entries = zf.getEntries();
807796
HashSet<String> dirSet = new HashSet<String>();
808797
while ( entries.hasMoreElements() )
@@ -833,13 +822,6 @@ else if ( !name.contains( "/" ) )
833822
}
834823
dirs.addAll( dirSet );
835824
}
836-
finally
837-
{
838-
if ( zf != null )
839-
{
840-
zf.close();
841-
}
842-
}
843825
}
844826
}
845827

src/main/java/org/codehaus/plexus/archiver/tar/TarUnArchiver.java

+13-18
Original file line numberDiff line numberDiff line change
@@ -97,37 +97,32 @@ protected void execute( String path, File outputDirectory )
9797
protected void execute( File sourceFile, File destDirectory, FileMapper[] fileMappers )
9898
throws ArchiverException
9999
{
100-
TarArchiveInputStream tis = null;
101100
try
102101
{
103102
getLogger().info( "Expanding: " + sourceFile + " into " + destDirectory );
104103
TarFile tarFile = new TarFile( sourceFile );
105-
tis = new TarArchiveInputStream(
106-
decompress( compression, sourceFile, new BufferedInputStream( new FileInputStream( sourceFile ) ) ) );
107-
TarArchiveEntry te;
108-
while ( ( te = tis.getNextTarEntry() ) != null )
104+
try ( TarArchiveInputStream tis = new TarArchiveInputStream(
105+
decompress( compression, sourceFile, new BufferedInputStream( new FileInputStream( sourceFile ) ) ) ) )
109106
{
110-
TarResource fileInfo = new TarResource( tarFile, te );
111-
if ( isSelected( te.getName(), fileInfo ) )
107+
TarArchiveEntry te;
108+
while ( ( te = tis.getNextTarEntry() ) != null )
112109
{
113-
final String symlinkDestination = te.isSymbolicLink() ? te.getLinkName() : null;
114-
extractFile( sourceFile, destDirectory, tis, te.getName(), te.getModTime(), te.isDirectory(),
115-
te.getMode() != 0 ? te.getMode() : null, symlinkDestination, fileMappers );
116-
110+
TarResource fileInfo = new TarResource( tarFile, te );
111+
if ( isSelected( te.getName(), fileInfo ) )
112+
{
113+
final String symlinkDestination = te.isSymbolicLink() ? te.getLinkName() : null;
114+
extractFile( sourceFile, destDirectory, tis, te.getName(), te.getModTime(), te.isDirectory(),
115+
te.getMode() != 0 ? te.getMode() : null, symlinkDestination, fileMappers );
116+
117+
}
117118
}
119+
getLogger().debug( "expand complete" );
118120
}
119-
getLogger().debug( "expand complete" );
120-
tis.close();
121-
tis = null;
122121
}
123122
catch ( IOException ioe )
124123
{
125124
throw new ArchiverException( "Error while expanding " + sourceFile.getAbsolutePath(), ioe );
126125
}
127-
finally
128-
{
129-
IOUtil.close( tis );
130-
}
131126
}
132127

133128
/**

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

+1-10
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.codehaus.plexus.archiver.ArchiverException;
2424
import org.codehaus.plexus.components.io.resources.PlexusIoResource;
2525
import org.codehaus.plexus.logging.AbstractLogEnabled;
26-
import org.codehaus.plexus.util.IOUtil;
2726

2827
public abstract class Compressor
2928
extends AbstractLogEnabled
@@ -91,17 +90,9 @@ private void compressFile( InputStream in, OutputStream zOut )
9190
protected void compress( PlexusIoResource resource, OutputStream zOut )
9291
throws IOException
9392
{
94-
InputStream in = null;
95-
try
93+
try ( InputStream in = Streams.bufferedInputStream( resource.getContents() ) )
9694
{
97-
in = Streams.bufferedInputStream( resource.getContents() );
9895
compressFile( in, zOut );
99-
in.close();
100-
in = null;
101-
}
102-
finally
103-
{
104-
IOUtil.close( in );
10596
}
10697
}
10798

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

+1-14
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,9 @@ public static boolean isUptodate( long sourceDate, long destinationDate )
7777
public static void copyFile( PlexusIoResource in, File outFile )
7878
throws IOException
7979
{
80-
InputStream input = null;
81-
OutputStream output = null;
82-
try
80+
try ( InputStream input = in.getContents(); OutputStream output = new FileOutputStream( outFile ) )
8381
{
84-
input = in.getContents();
85-
output = new FileOutputStream( outFile );
8682
IOUtil.copy( input, output );
87-
output.close();
88-
output = null;
89-
input.close();
90-
input = null;
91-
}
92-
finally
93-
{
94-
IOUtil.close( input );
95-
IOUtil.close( output );
9683
}
9784
}
9885

src/main/java/org/codehaus/plexus/archiver/zip/AbstractZipArchiver.java

+2-10
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import org.codehaus.plexus.components.io.functions.SymlinkDestinationSupplier;
4646
import org.codehaus.plexus.components.io.resources.PlexusIoResource;
4747
import org.codehaus.plexus.util.FileUtils;
48-
import org.codehaus.plexus.util.IOUtil;
4948
import static org.codehaus.plexus.archiver.util.Streams.bufferedOutputStream;
5049
import static org.codehaus.plexus.archiver.util.Streams.fileOutputStream;
5150

@@ -664,10 +663,9 @@ protected boolean createEmptyZip( File zipFile )
664663
// because it does not permit a zero-entry archive.
665664
// Must create it manually.
666665
getLogger().info( "Note: creating empty " + archiveType + " archive " + zipFile );
667-
OutputStream os = null;
668-
try
666+
667+
try ( OutputStream os = new FileOutputStream( zipFile ) )
669668
{
670-
os = new FileOutputStream( zipFile );
671669
// Cf. PKZIP specification.
672670
byte[] empty = new byte[ 22 ];
673671
empty[0] = 80; // P
@@ -676,17 +674,11 @@ protected boolean createEmptyZip( File zipFile )
676674
empty[3] = 6;
677675
// remainder zeros
678676
os.write( empty );
679-
os.close();
680-
os = null;
681677
}
682678
catch ( IOException ioe )
683679
{
684680
throw new ArchiverException( "Could not create empty ZIP archive " + "(" + ioe.getMessage() + ")", ioe );
685681
}
686-
finally
687-
{
688-
IOUtil.close( os );
689-
}
690682
return true;
691683
}
692684

0 commit comments

Comments
 (0)