This is an automated email from the ASF dual-hosted git repository.
hboutemy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-wrapper.git
The following commit(s) were added to refs/heads/master by this push:
new 57a336b use try with resources construct to simplify code
57a336b is described below
commit 57a336b662f87b9335c763645c78db539b39f37a
Author: Hervé Boutemy <[email protected]>
AuthorDate: Sat Dec 11 10:21:57 2021 +0100
use try with resources construct to simplify code
---
.../apache/maven/wrapper/DefaultDownloader.java | 33 ++++----------
.../java/org/apache/maven/wrapper/Installer.java | 53 +++++++++-------------
.../org/apache/maven/wrapper/MavenWrapperMain.java | 27 ++++-------
.../org/apache/maven/wrapper/WrapperExecutor.java | 7 +--
4 files changed, 43 insertions(+), 77 deletions(-)
diff --git
a/maven-wrapper/src/main/java/org/apache/maven/wrapper/DefaultDownloader.java
b/maven-wrapper/src/main/java/org/apache/maven/wrapper/DefaultDownloader.java
index 91ed2b1..aa0c463 100644
---
a/maven-wrapper/src/main/java/org/apache/maven/wrapper/DefaultDownloader.java
+++
b/maven-wrapper/src/main/java/org/apache/maven/wrapper/DefaultDownloader.java
@@ -96,18 +96,15 @@ public class DefaultDownloader
private void downloadInternal( URI address, File destination )
throws Exception
{
- OutputStream out = null;
- URLConnection conn;
- InputStream in = null;
- try
+ URL url = address.toURL();
+ URLConnection conn = url.openConnection();
+ addBasicAuthentication( address, conn );
+ final String userAgentValue = calculateUserAgent();
+ conn.setRequestProperty( "User-Agent", userAgentValue );
+
+ try ( OutputStream out = new BufferedOutputStream( new
FileOutputStream( destination ) );
+ InputStream in = conn.getInputStream() )
{
- URL url = address.toURL();
- out = new BufferedOutputStream( new FileOutputStream( destination
) );
- conn = url.openConnection();
- addBasicAuthentication( address, conn );
- final String userAgentValue = calculateUserAgent();
- conn.setRequestProperty( "User-Agent", userAgentValue );
- in = conn.getInputStream();
byte[] buffer = new byte[BUFFER_SIZE];
int numRead;
long progressCounter = 0;
@@ -122,18 +119,8 @@ public class DefaultDownloader
out.write( buffer, 0, numRead );
}
}
- finally
- {
- Logger.info( "" );
- if ( in != null )
- {
- in.close();
- }
- if ( out != null )
- {
- out.close();
- }
- }
+
+ Logger.info( "" );
}
private void addBasicAuthentication( URI address, URLConnection connection
)
diff --git
a/maven-wrapper/src/main/java/org/apache/maven/wrapper/Installer.java
b/maven-wrapper/src/main/java/org/apache/maven/wrapper/Installer.java
index bf0359f..babafd8 100644
--- a/maven-wrapper/src/main/java/org/apache/maven/wrapper/Installer.java
+++ b/maven-wrapper/src/main/java/org/apache/maven/wrapper/Installer.java
@@ -150,21 +150,19 @@ public class Installer
}
else
{
- BufferedReader is = new BufferedReader( new InputStreamReader(
p.getInputStream() ) );
- Formatter stdout = new Formatter();
- String line;
- while ( ( line = is.readLine() ) != null )
+ try ( BufferedReader is = new BufferedReader( new
InputStreamReader( p.getInputStream() ) );
+ Formatter stdout = new Formatter() )
{
- stdout.format( "%s%n", line );
+ String line;
+ while ( ( line = is.readLine() ) != null )
+ {
+ stdout.format( "%s%n", line );
+ }
+ errorMessage = stdout.toString();
}
- errorMessage = stdout.toString();
}
}
- catch ( IOException e )
- {
- errorMessage = e.getMessage();
- }
- catch ( InterruptedException e )
+ catch ( IOException | InterruptedException e )
{
errorMessage = e.getMessage();
}
@@ -208,9 +206,7 @@ public class Installer
throws IOException
{
Enumeration entries;
- ZipFile zipFile;
-
- zipFile = new ZipFile( zip );
+ ZipFile zipFile = new ZipFile( zip );
entries = zipFile.entries();
@@ -225,25 +221,20 @@ public class Installer
}
new File( dest, entry.getName() ).getParentFile().mkdirs();
- copyInputStream( zipFile.getInputStream( entry ),
- new BufferedOutputStream( new FileOutputStream(
new File( dest, entry.getName() ) ) ) );
- }
- zipFile.close();
- }
- public void copyInputStream( InputStream in, OutputStream out )
- throws IOException
- {
- byte[] buffer = new byte[1024];
- int len;
+ try ( InputStream in = zipFile.getInputStream( entry );
+ OutputStream out =
+ new BufferedOutputStream( new
FileOutputStream( new File( dest, entry.getName() ) ) ) )
+ {
+ byte[] buffer = new byte[1024];
+ int len;
- while ( ( len = in.read( buffer ) ) >= 0 )
- {
- out.write( buffer, 0, len );
+ while ( ( len = in.read( buffer ) ) >= 0 )
+ {
+ out.write( buffer, 0, len );
+ }
+ }
}
-
- in.close();
- out.close();
+ zipFile.close();
}
-
}
diff --git
a/maven-wrapper/src/main/java/org/apache/maven/wrapper/MavenWrapperMain.java
b/maven-wrapper/src/main/java/org/apache/maven/wrapper/MavenWrapperMain.java
index b5a9c3c..ea5a1d7 100644
--- a/maven-wrapper/src/main/java/org/apache/maven/wrapper/MavenWrapperMain.java
+++ b/maven-wrapper/src/main/java/org/apache/maven/wrapper/MavenWrapperMain.java
@@ -36,6 +36,9 @@ import
org.apache.maven.wrapper.cli.SystemPropertiesCommandLineConverter;
*/
public class MavenWrapperMain
{
+ private static final String POM_PROPERTIES =
"/META-INF/maven/org.apache.maven.wrapper/"
+ + "maven-wrapper/pom.properties";
+
public static final String DEFAULT_MAVEN_USER_HOME = System.getProperty(
"user.home" ) + "/.m2";
public static final String MAVEN_USER_HOME_PROPERTY_KEY =
"maven.user.home";
@@ -119,30 +122,20 @@ public class MavenWrapperMain
static String wrapperVersion()
{
- try
+ try ( InputStream resourceAsStream =
MavenWrapperMain.class.getResourceAsStream( POM_PROPERTIES ) )
{
- InputStream resourceAsStream =
- MavenWrapperMain.class.getResourceAsStream(
"/META-INF/maven/org.apache.maven.wrapper/"
- + "maven-wrapper/pom.properties" );
if ( resourceAsStream == null )
{
- throw new RuntimeException( "No maven properties found." );
+ throw new IllegalStateException( POM_PROPERTIES + " not
found." );
}
Properties mavenProperties = new Properties();
- try
- {
- mavenProperties.load( resourceAsStream );
- String version = mavenProperties.getProperty( "version" );
- if ( version == null )
- {
- throw new RuntimeException( "No version number specified
in build receipt resource." );
- }
- return version;
- }
- finally
+ mavenProperties.load( resourceAsStream );
+ String version = mavenProperties.getProperty( "version" );
+ if ( version == null )
{
- resourceAsStream.close();
+ throw new NullPointerException( "No version specified in " +
POM_PROPERTIES );
}
+ return version;
}
catch ( Exception e )
{
diff --git
a/maven-wrapper/src/main/java/org/apache/maven/wrapper/WrapperExecutor.java
b/maven-wrapper/src/main/java/org/apache/maven/wrapper/WrapperExecutor.java
index c2f5266..f4a195e 100644
--- a/maven-wrapper/src/main/java/org/apache/maven/wrapper/WrapperExecutor.java
+++ b/maven-wrapper/src/main/java/org/apache/maven/wrapper/WrapperExecutor.java
@@ -124,15 +124,10 @@ public class WrapperExecutor
private static void loadProperties( File propertiesFile, Properties
properties )
throws IOException
{
- InputStream inStream = new FileInputStream( propertiesFile );
- try
+ try ( InputStream inStream = new FileInputStream( propertiesFile ) )
{
properties.load( inStream );
}
- finally
- {
- inStream.close();
- }
}
/**