You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by el...@apache.org on 2020/06/15 13:27:10 UTC
[maven-resolver] 01/01: fix some warnings around unclosed resources
This is an automated email from the ASF dual-hosted git repository.
elharo pushed a commit to branch warn
in repository https://gitbox.apache.org/repos/asf/maven-resolver.git
commit 4ea39e5f501dafeedf0b1cfb27b0f8e8dce4e9e7
Author: Elliotte Rusty Harold <el...@ibiblio.org>
AuthorDate: Mon Jun 15 09:26:54 2020 -0400
fix some warnings around unclosed resources
---
.../aether/connector/basic/PartialFile.java | 31 ++--------------
.../connector/transport/AbstractTransporter.java | 43 +++++-----------------
.../aether/transport/file/FileTransporter.java | 1 +
.../org/eclipse/aether/util/ChecksumUtils.java | 32 ++++++++--------
.../util/filter/ExclusionDependencyFilterTest.java | 1 -
.../util/repository/JreProxySelectorTest.java | 8 ++--
6 files changed, 36 insertions(+), 80 deletions(-)
diff --git a/maven-resolver-connector-basic/src/main/java/org/eclipse/aether/connector/basic/PartialFile.java b/maven-resolver-connector-basic/src/main/java/org/eclipse/aether/connector/basic/PartialFile.java
index c7a74f7..9fe7494 100644
--- a/maven-resolver-connector-basic/src/main/java/org/eclipse/aether/connector/basic/PartialFile.java
+++ b/maven-resolver-connector-basic/src/main/java/org/eclipse/aether/connector/basic/PartialFile.java
@@ -71,8 +71,7 @@ final class PartialFile
}
private static FileLock lock( File lockFile, File partFile, int requestTimeout, RemoteAccessChecker checker,
- AtomicBoolean concurrent )
- throws Exception
+ AtomicBoolean concurrent ) throws Exception
{
boolean interrupted = false;
try
@@ -141,19 +140,15 @@ final class PartialFile
if ( lock == null )
{
raf.close();
- raf = null;
}
}
catch ( OverlappingFileLockException e )
{
close( raf );
- raf = null;
- lock = null;
}
catch ( RuntimeException | IOException e )
{
close( raf );
- raf = null;
if ( !lockFile.delete() )
{
lockFile.deleteOnExit();
@@ -200,33 +195,15 @@ final class PartialFile
public void close() throws IOException
{
- Channel channel = null;
- try
+ try ( Channel channel = lock.channel() )
{
- channel = lock.channel();
lock.release();
- channel.close();
- channel = null;
}
finally
{
- try
- {
- if ( channel != null )
- {
- channel.close();
- }
- }
- catch ( final IOException e )
- {
- // Suppressed due to an exception already thrown in the try block.
- }
- finally
+ if ( !lockFile.delete() )
{
- if ( !lockFile.delete() )
- {
- lockFile.deleteOnExit();
- }
+ lockFile.deleteOnExit();
}
}
}
diff --git a/maven-resolver-spi/src/main/java/org/eclipse/aether/spi/connector/transport/AbstractTransporter.java b/maven-resolver-spi/src/main/java/org/eclipse/aether/spi/connector/transport/AbstractTransporter.java
index 21a0da3..0c14b91 100644
--- a/maven-resolver-spi/src/main/java/org/eclipse/aether/spi/connector/transport/AbstractTransporter.java
+++ b/maven-resolver-spi/src/main/java/org/eclipse/aether/spi/connector/transport/AbstractTransporter.java
@@ -81,62 +81,39 @@ public abstract class AbstractTransporter
* Subclasses might want to invoke this utility method from within their {@link #implGet(GetTask)} to avoid
* boilerplate I/O code.
*
- * @param task The download to perform, must not be {@code null}.
- * @param is The input stream to download the data from, must not be {@code null}.
+ * @param task the download to perform, must not be {@code null}.
+ * @param is the input stream to download the data from, must not be {@code null}.
* @param close {@code true} if the supplied input stream should be automatically closed, {@code false} to leave the
* stream open.
- * @param length The size in bytes of the downloaded resource or {@code -1} if unknown, not to be confused with the
+ * @param length the size in bytes of the downloaded resource or {@code -1} if unknown, not to be confused with the
* length of the supplied input stream which might be smaller if the download is resumed.
* @param resume {@code true} if the download resumes from {@link GetTask#getResumeOffset()}, {@code false} if the
* download starts at the first byte of the resource.
- * @throws IOException If the transfer encountered an I/O error.
- * @throws TransferCancelledException If the transfer was cancelled.
+ * @throws IOException if the transfer encountered an I/O error
+ * @throws TransferCancelledException if the transfer was cancelled
*/
protected void utilGet( GetTask task, InputStream is, boolean close, long length, boolean resume )
throws IOException, TransferCancelledException
{
- OutputStream os = null;
- try
+
+ try ( OutputStream os = task.newOutputStream( resume ) )
{
- os = task.newOutputStream( resume );
task.getListener().transportStarted( resume ? task.getResumeOffset() : 0L, length );
copy( os, is, task.getListener() );
- os.close();
- os = null;
-
- if ( close )
- {
- is.close();
- is = null;
- }
}
finally
- {
+ {
try
{
- if ( os != null )
+ if ( close && is != null )
{
- os.close();
+ is.close();
}
}
catch ( final IOException e )
{
// Suppressed due to an exception already thrown in the try block.
}
- finally
- {
- try
- {
- if ( close && is != null )
- {
- is.close();
- }
- }
- catch ( final IOException e )
- {
- // Suppressed due to an exception already thrown in the try block.
- }
- }
}
}
diff --git a/maven-resolver-transport-file/src/main/java/org/eclipse/aether/transport/file/FileTransporter.java b/maven-resolver-transport-file/src/main/java/org/eclipse/aether/transport/file/FileTransporter.java
index 7da42f2..ed651a1 100644
--- a/maven-resolver-transport-file/src/main/java/org/eclipse/aether/transport/file/FileTransporter.java
+++ b/maven-resolver-transport-file/src/main/java/org/eclipse/aether/transport/file/FileTransporter.java
@@ -22,6 +22,7 @@ package org.eclipse.aether.transport.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
+import java.io.IOException;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.spi.connector.transport.AbstractTransporter;
diff --git a/maven-resolver-util/src/main/java/org/eclipse/aether/util/ChecksumUtils.java b/maven-resolver-util/src/main/java/org/eclipse/aether/util/ChecksumUtils.java
index 0ac354f..a6ec325 100644
--- a/maven-resolver-util/src/main/java/org/eclipse/aether/util/ChecksumUtils.java
+++ b/maven-resolver-util/src/main/java/org/eclipse/aether/util/ChecksumUtils.java
@@ -95,17 +95,20 @@ public final class ChecksumUtils
/**
* Calculates checksums for the specified file.
*
- * @param dataFile The file for which to calculate checksums, must not be {@code null}.
- * @param algos The names of checksum algorithms (cf. {@link MessageDigest#getInstance(String)} to use, must not be
+ * @param dataFile the file for which to calculate checksums, must not be {@code null}.
+ * @param algos the names of checksum algorithms (cf. {@link MessageDigest#getInstance(String)} to use, must not be
* {@code null}.
- * @return The calculated checksums, indexed by algorithm name, or the exception that occurred while trying to
+ * @return the calculated checksums, indexed by algorithm name, or the exception that occurred while trying to
* calculate it, never {@code null}.
- * @throws IOException If the data file could not be read.
+ * @throws IOException if the data file could not be read
*/
public static Map<String, Object> calc( File dataFile, Collection<String> algos )
throws IOException
{
- return calc( new FileInputStream( dataFile ), algos );
+ try ( FileInputStream in = new FileInputStream( dataFile ) )
+ {
+ return calc( in, algos );
+ }
}
@@ -134,19 +137,16 @@ public final class ChecksumUtils
}
}
- try ( InputStream in = data )
+ for ( byte[] buffer = new byte[ 32 * 1024 ];; )
{
- for ( byte[] buffer = new byte[ 32 * 1024 ];; )
+ int read = data.read( buffer );
+ if ( read < 0 )
{
- int read = in.read( buffer );
- if ( read < 0 )
- {
- break;
- }
- for ( MessageDigest digest : digests.values() )
- {
- digest.update( buffer, 0, read );
- }
+ break;
+ }
+ for ( MessageDigest digest : digests.values() )
+ {
+ digest.update( buffer, 0, read );
}
}
diff --git a/maven-resolver-util/src/test/java/org/eclipse/aether/util/filter/ExclusionDependencyFilterTest.java b/maven-resolver-util/src/test/java/org/eclipse/aether/util/filter/ExclusionDependencyFilterTest.java
index 1da95bb..37fca10 100644
--- a/maven-resolver-util/src/test/java/org/eclipse/aether/util/filter/ExclusionDependencyFilterTest.java
+++ b/maven-resolver-util/src/test/java/org/eclipse/aether/util/filter/ExclusionDependencyFilterTest.java
@@ -22,7 +22,6 @@ package org.eclipse.aether.util.filter;
import static org.junit.Assert.*;
import java.util.Arrays;
-import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
diff --git a/maven-resolver-util/src/test/java/org/eclipse/aether/util/repository/JreProxySelectorTest.java b/maven-resolver-util/src/test/java/org/eclipse/aether/util/repository/JreProxySelectorTest.java
index 5e8d274..5752fc9 100644
--- a/maven-resolver-util/src/test/java/org/eclipse/aether/util/repository/JreProxySelectorTest.java
+++ b/maven-resolver-util/src/test/java/org/eclipse/aether/util/repository/JreProxySelectorTest.java
@@ -171,9 +171,11 @@ public class JreProxySelectorTest
RemoteRepository repo2 = new RemoteRepository.Builder( repo ).setProxy( proxy ).build();
Authentication auth = proxy.getAuthentication();
assertNotNull( auth );
- AuthenticationContext authCtx = AuthenticationContext.forProxy( new DefaultRepositorySystemSession(), repo2 );
- assertEquals( "proxyuser", authCtx.get( AuthenticationContext.USERNAME ) );
- assertEquals( "proxypass", authCtx.get( AuthenticationContext.PASSWORD ) );
+ try ( AuthenticationContext authCtx = AuthenticationContext.forProxy( new DefaultRepositorySystemSession(), repo2 ) )
+ {
+ assertEquals( "proxyuser", authCtx.get( AuthenticationContext.USERNAME ) );
+ assertEquals( "proxypass", authCtx.get( AuthenticationContext.PASSWORD ) );
+ }
}
}