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/30 23:00:49 UTC

[maven-shared-io] 01/01: close streams

This is an automated email from the ASF dual-hosted git repository.

elharo pushed a commit to branch close
in repository https://gitbox.apache.org/repos/asf/maven-shared-io.git

commit 2966d9cd7ceaff178bc9a5bbc46f275f060cea65
Author: Elliotte Rusty Harold <el...@ibiblio.org>
AuthorDate: Tue Jun 30 19:00:33 2020 -0400

    close streams
---
 .../java/org/apache/maven/shared/io/Utils.java     | 25 +++++++++++-----------
 .../io/location/ArtifactLocatorStrategyTest.java   |  1 -
 .../maven/shared/io/location/FileLocationTest.java | 11 +++++-----
 3 files changed, 18 insertions(+), 19 deletions(-)

diff --git a/src/test/java/org/apache/maven/shared/io/Utils.java b/src/test/java/org/apache/maven/shared/io/Utils.java
index cf204b0..9aaa0bb 100644
--- a/src/test/java/org/apache/maven/shared/io/Utils.java
+++ b/src/test/java/org/apache/maven/shared/io/Utils.java
@@ -82,7 +82,8 @@ public final class Utils
     }
 
     /**
-     * reads content from a file and normalize EOLs to simple line feed (\\n), using platform encoding.
+     * Reads content from a file and normalizes EOLs to simple line feed (\\n), using platform encoding.
+     * 
      * @deprecated this API isn't explicit about encoding nor EOL normalization, use readPlatformFile() or
      * readXmlFile() depending on your need, in conjunction with normalizeEndOfLine()
      */
@@ -95,22 +96,20 @@ public final class Utils
     {
         StringWriter buffer = new StringWriter();
 
-        Reader reader = ReaderFactory.newPlatformReader( file );
-
-        IOUtil.copy( reader, buffer );
-
-        return buffer.toString();
+        try ( Reader reader = ReaderFactory.newPlatformReader( file ) ) {
+            IOUtil.copy( reader, buffer );  
+            return buffer.toString();
+        }
     }
 
     public static String readXmlFile( File file ) throws IOException
     {
         StringWriter buffer = new StringWriter();
 
-        Reader reader = ReaderFactory.newXmlReader( file );
-
-        IOUtil.copy( reader, buffer );
-
-        return buffer.toString();
+        try ( Reader reader = ReaderFactory.newXmlReader( file ) ) {
+            IOUtil.copy( reader, buffer );  
+            return buffer.toString();
+        }
     }
 
     /**
@@ -120,11 +119,11 @@ public final class Utils
     {
         StringBuffer buffer = new StringBuffer();
 
-        BufferedReader reader = new BufferedReader( new StringReader( content ) );
 
         String line = null;
 
-        try {
+        try ( BufferedReader reader = new BufferedReader( new StringReader( content ) ) )
+        {
             while( ( line = reader.readLine() ) != null )
             {
                 if ( buffer.length() > 0 )
diff --git a/src/test/java/org/apache/maven/shared/io/location/ArtifactLocatorStrategyTest.java b/src/test/java/org/apache/maven/shared/io/location/ArtifactLocatorStrategyTest.java
index 13b8bc3..5407977 100644
--- a/src/test/java/org/apache/maven/shared/io/location/ArtifactLocatorStrategyTest.java
+++ b/src/test/java/org/apache/maven/shared/io/location/ArtifactLocatorStrategyTest.java
@@ -22,7 +22,6 @@ package org.apache.maven.shared.io.location;
 import java.io.File;
 import java.io.IOException;
 import java.util.Collections;
-import java.util.List;
 
 import junit.framework.TestCase;
 
diff --git a/src/test/java/org/apache/maven/shared/io/location/FileLocationTest.java b/src/test/java/org/apache/maven/shared/io/location/FileLocationTest.java
index 126cb97..467833a 100644
--- a/src/test/java/org/apache/maven/shared/io/location/FileLocationTest.java
+++ b/src/test/java/org/apache/maven/shared/io/location/FileLocationTest.java
@@ -77,11 +77,12 @@ public class FileLocationTest
 
         location.open();
 
-        InputStream stream = location.getInputStream();
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-        IOUtil.copy( stream, out );
-
-        assertEquals( testStr, new String(out.toByteArray(), "US-ASCII" ) );
+        try ( InputStream stream = location.getInputStream() ) {
+            ByteArrayOutputStream out = new ByteArrayOutputStream();
+            IOUtil.copy( stream, out );
+    
+            assertEquals( testStr, new String(out.toByteArray(), "US-ASCII" ) );
+        }
     }
 
     public void testShouldReadFileContentsUsingByteArray() throws IOException