Skip to content

Refactor: use try with resources pattern #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -365,18 +365,9 @@ else if ( isDirectory )
}
else
{
OutputStream out = null;
try
try ( OutputStream out = new FileOutputStream( f ) )
{
out = new FileOutputStream( f );

IOUtil.copy( compressedInputStream, out );
out.close();
out = null;
}
finally
{
IOUtil.close( out );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.Collections;
import java.util.List;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.StringUtils;

/**
Expand Down Expand Up @@ -43,56 +42,50 @@ public DotDirectiveArchiveFinalizer( File dotFileDirectory, String dotFilePrefix
public void finalizeArchiveCreation( Archiver archiver )
throws ArchiverException
{
BufferedReader in = null;
try
{
List<File> dotFiles = FileUtils.getFiles( dotFileDirectory, dotFilePrefix + "*", null );

for ( File dotFile : dotFiles )
{
in = new BufferedReader( new FileReader( dotFile ) );

for ( String line = in.readLine(); line != null; line = in.readLine() )
try ( BufferedReader in = new BufferedReader( new FileReader( dotFile ) ) )
{
String[] s = StringUtils.split( line, ":" );

if ( s.length == 1 )
for ( String line = in.readLine(); line != null; line = in.readLine() )
{
File directory = new File( dotFileDirectory, s[0] );

System.out.println( "adding directory = " + directory );
String[] s = StringUtils.split( line, ":" );

archiver.addDirectory( directory );
}
else
{
File directory = new File( dotFileDirectory, s[0] );
if ( s.length == 1 )
{
File directory = new File( dotFileDirectory, s[0] );

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

if ( s[1].endsWith( "/" ) )
{
archiver.addDirectory( directory, s[1] );
archiver.addDirectory( directory );
}
else
{
archiver.addDirectory( directory, s[1] + "/" );
File directory = new File( dotFileDirectory, s[0] );

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

if ( s[1].endsWith( "/" ) )
{
archiver.addDirectory( directory, s[1] );
}
else
{
archiver.addDirectory( directory, s[1] + "/" );
}
}
}
}

in.close();
in = null;
}
}
catch ( IOException e )
{
throw new ArchiverException( "Error processing dot files.", e );
}
finally
{
IOUtil.close( in );
}
}

@Override
Expand Down
26 changes: 4 additions & 22 deletions src/main/java/org/codehaus/plexus/archiver/jar/JarArchiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
import java.util.Vector;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.commons.compress.parallel.InputStreamSupplier;
import org.codehaus.plexus.archiver.ArchiverException;
import org.codehaus.plexus.archiver.zip.ConcurrentJarCreator;
import org.codehaus.plexus.archiver.zip.ZipArchiver;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.logging.console.ConsoleLogger;
import org.codehaus.plexus.util.IOUtil;
import static org.codehaus.plexus.archiver.util.Streams.bufferedOutputStream;
import static org.codehaus.plexus.archiver.util.Streams.fileOutputStream;

Expand Down Expand Up @@ -238,24 +238,15 @@ public void setManifest( File manifestFile )
private Manifest getManifest( File manifestFile )
throws ArchiverException
{
InputStream in = null;
try
try ( InputStream in = new FileInputStream( manifestFile ))
{
in = new FileInputStream( manifestFile );
final Manifest mf = getManifest( in );
in.close();
in = null;
return mf;
return getManifest( in );
}
catch ( IOException e )
{
throw new ArchiverException( "Unable to read manifest file: " + manifestFile + " (" + e.getMessage() + ")",
e );
}
finally
{
IOUtil.close( in );
}
}

private Manifest getManifest( InputStream is )
Expand Down Expand Up @@ -799,10 +790,8 @@ else if ( zipFile.isDirectory() )
}
else
{
org.apache.commons.compress.archivers.zip.ZipFile zf = null;
try
try ( ZipFile zf = new ZipFile( file, "utf-8" ) )
{
zf = new org.apache.commons.compress.archivers.zip.ZipFile( file, "utf-8" );
Enumeration<ZipArchiveEntry> entries = zf.getEntries();
HashSet<String> dirSet = new HashSet<String>();
while ( entries.hasMoreElements() )
Expand Down Expand Up @@ -833,13 +822,6 @@ else if ( !name.contains( "/" ) )
}
dirs.addAll( dirSet );
}
finally
{
if ( zf != null )
{
zf.close();
}
}
}
}

Expand Down
31 changes: 13 additions & 18 deletions src/main/java/org/codehaus/plexus/archiver/tar/TarUnArchiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,37 +97,32 @@ protected void execute( String path, File outputDirectory )
protected void execute( File sourceFile, File destDirectory, FileMapper[] fileMappers )
throws ArchiverException
{
TarArchiveInputStream tis = null;
try
{
getLogger().info( "Expanding: " + sourceFile + " into " + destDirectory );
TarFile tarFile = new TarFile( sourceFile );
tis = new TarArchiveInputStream(
decompress( compression, sourceFile, new BufferedInputStream( new FileInputStream( sourceFile ) ) ) );
TarArchiveEntry te;
while ( ( te = tis.getNextTarEntry() ) != null )
try ( TarArchiveInputStream tis = new TarArchiveInputStream(
decompress( compression, sourceFile, new BufferedInputStream( new FileInputStream( sourceFile ) ) ) ) )
{
TarResource fileInfo = new TarResource( tarFile, te );
if ( isSelected( te.getName(), fileInfo ) )
TarArchiveEntry te;
while ( ( te = tis.getNextTarEntry() ) != null )
{
final String symlinkDestination = te.isSymbolicLink() ? te.getLinkName() : null;
extractFile( sourceFile, destDirectory, tis, te.getName(), te.getModTime(), te.isDirectory(),
te.getMode() != 0 ? te.getMode() : null, symlinkDestination, fileMappers );

TarResource fileInfo = new TarResource( tarFile, te );
if ( isSelected( te.getName(), fileInfo ) )
{
final String symlinkDestination = te.isSymbolicLink() ? te.getLinkName() : null;
extractFile( sourceFile, destDirectory, tis, te.getName(), te.getModTime(), te.isDirectory(),
te.getMode() != 0 ? te.getMode() : null, symlinkDestination, fileMappers );

}
}
getLogger().debug( "expand complete" );
}
getLogger().debug( "expand complete" );
tis.close();
tis = null;
}
catch ( IOException ioe )
{
throw new ArchiverException( "Error while expanding " + sourceFile.getAbsolutePath(), ioe );
}
finally
{
IOUtil.close( tis );
}
}

/**
Expand Down
11 changes: 1 addition & 10 deletions src/main/java/org/codehaus/plexus/archiver/util/Compressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.codehaus.plexus.archiver.ArchiverException;
import org.codehaus.plexus.components.io.resources.PlexusIoResource;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.util.IOUtil;

public abstract class Compressor
extends AbstractLogEnabled
Expand Down Expand Up @@ -91,17 +90,9 @@ private void compressFile( InputStream in, OutputStream zOut )
protected void compress( PlexusIoResource resource, OutputStream zOut )
throws IOException
{
InputStream in = null;
try
try ( InputStream in = Streams.bufferedInputStream( resource.getContents() ) )
{
in = Streams.bufferedInputStream( resource.getContents() );
compressFile( in, zOut );
in.close();
in = null;
}
finally
{
IOUtil.close( in );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,9 @@ public static boolean isUptodate( long sourceDate, long destinationDate )
public static void copyFile( PlexusIoResource in, File outFile )
throws IOException
{
InputStream input = null;
OutputStream output = null;
try
try ( InputStream input = in.getContents(); OutputStream output = new FileOutputStream( outFile ) )
{
input = in.getContents();
output = new FileOutputStream( outFile );
IOUtil.copy( input, output );
output.close();
output = null;
input.close();
input = null;
}
finally
{
IOUtil.close( input );
IOUtil.close( output );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import org.codehaus.plexus.components.io.functions.SymlinkDestinationSupplier;
import org.codehaus.plexus.components.io.resources.PlexusIoResource;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import static org.codehaus.plexus.archiver.util.Streams.bufferedOutputStream;
import static org.codehaus.plexus.archiver.util.Streams.fileOutputStream;

Expand Down Expand Up @@ -664,10 +663,9 @@ protected boolean createEmptyZip( File zipFile )
// because it does not permit a zero-entry archive.
// Must create it manually.
getLogger().info( "Note: creating empty " + archiveType + " archive " + zipFile );
OutputStream os = null;
try

try ( OutputStream os = new FileOutputStream( zipFile ) )
{
os = new FileOutputStream( zipFile );
// Cf. PKZIP specification.
byte[] empty = new byte[ 22 ];
empty[0] = 80; // P
Expand All @@ -676,17 +674,11 @@ protected boolean createEmptyZip( File zipFile )
empty[3] = 6;
// remainder zeros
os.write( empty );
os.close();
os = null;
}
catch ( IOException ioe )
{
throw new ArchiverException( "Could not create empty ZIP archive " + "(" + ioe.getMessage() + ")", ioe );
}
finally
{
IOUtil.close( os );
}
return true;
}

Expand Down
Loading