From 05df14eb6bc5304f1370baebe6d46368bafa9f9d Mon Sep 17 00:00:00 2001 From: Christian Schulte Date: Wed, 4 May 2016 01:16:38 +0200 Subject: [PATCH] o Updated to stop suppressing exception's thrown when closing streams. o Updated to correctly test for end of file conditions. o Catching up with the following 'commons-io' bug fixes: https://issues.apache.org/jira/browse/IO-298 https://issues.apache.org/jira/browse/IO-502 --- .../org/codehaus/plexus/util/FileUtils.java | 37 ++++++++++++++++--- .../java/org/codehaus/plexus/util/IOUtil.java | 8 ++-- .../org/codehaus/plexus/util/IOUtilTest.java | 2 +- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/codehaus/plexus/util/FileUtils.java b/src/main/java/org/codehaus/plexus/util/FileUtils.java index 80c22729..cdd2c1d7 100644 --- a/src/main/java/org/codehaus/plexus/util/FileUtils.java +++ b/src/main/java/org/codehaus/plexus/util/FileUtils.java @@ -389,10 +389,12 @@ public static String fileRead( File file, String encoding ) } int count; char[] b = new char[512]; - while ( ( count = reader.read( b ) ) > 0 ) // blocking read + while ( ( count = reader.read( b ) ) >= 0 ) // blocking read { buf.append( b, 0, count ); } + reader.close(); + reader = null; } finally { @@ -439,6 +441,8 @@ public static void fileAppend( String fileName, String encoding, String data ) { out.write( data.getBytes() ); } + out.close(); + out = null; } finally { @@ -515,6 +519,8 @@ public static void fileWrite( File file, String encoding, String data ) writer = new OutputStreamWriter( out ); } writer.write( data ); + writer.close(); + writer = null; } finally { @@ -761,18 +767,23 @@ public static boolean contentEquals( final File file1, final File file2 ) InputStream input1 = null; InputStream input2 = null; + boolean equals = false; try { input1 = new FileInputStream( file1 ); input2 = new FileInputStream( file2 ); - return IOUtil.contentEquals( input1, input2 ); - + equals = IOUtil.contentEquals( input1, input2 ); + input1.close(); + input1 = null; + input2.close(); + input2 = null; } finally { IOUtil.close( input1 ); IOUtil.close( input2 ); } + return equals; } /** @@ -813,11 +824,11 @@ public static File toFile( final URL url ) public static URL[] toURLs( final File[] files ) throws IOException { - final URL[] urls = new URL[files.length]; + final URL[] urls = new URL[ files.length ]; for ( int i = 0; i < urls.length; i++ ) { - urls[i] = files[i].toURL(); + urls[i] = files[i].toURI().toURL(); } return urls; @@ -1112,6 +1123,14 @@ private static void doCopyFile( File source, File destination ) count = size - pos > FILE_COPY_BUFFER_SIZE ? FILE_COPY_BUFFER_SIZE : size - pos; pos += output.transferFrom( input, pos, count ); } + output.close(); + output = null; + fos.close(); + fos = null; + input.close(); + input = null; + fis.close(); + fis = null; } finally { @@ -1197,6 +1216,10 @@ public static void copyStreamToFile( final InputStreamFacade source, final File input = source.getInputStream(); output = new FileOutputStream( destination ); IOUtil.copy( input, output ); + output.close(); + output = null; + input.close(); + input = null; } finally { @@ -2332,6 +2355,10 @@ public static void copyFile( File from, File to, String encoding, FilterWrapper[ } IOUtil.copy( reader, fileWriter ); + fileWriter.close(); + fileWriter = null; + fileReader.close(); + fileReader = null; } finally { diff --git a/src/main/java/org/codehaus/plexus/util/IOUtil.java b/src/main/java/org/codehaus/plexus/util/IOUtil.java index 4045a7f4..ec9b6ba3 100644 --- a/src/main/java/org/codehaus/plexus/util/IOUtil.java +++ b/src/main/java/org/codehaus/plexus/util/IOUtil.java @@ -186,7 +186,7 @@ public static void copy( final InputStream input, { final byte[] buffer = new byte[bufferSize]; int n = 0; - while ( -1 != ( n = input.read( buffer ) ) ) + while ( 0 <= ( n = input.read( buffer ) ) ) { output.write( buffer, 0, n ); } @@ -210,7 +210,7 @@ public static void copy( final Reader input, final Writer output, final int buff { final char[] buffer = new char[bufferSize]; int n = 0; - while ( -1 != ( n = input.read( buffer ) ) ) + while ( 0 <= ( n = input.read( buffer ) ) ) { output.write( buffer, 0, n ); } @@ -699,7 +699,7 @@ public static boolean contentEquals( final InputStream input1, final InputStream bufferedInput2 = new BufferedInputStream( input2 ); int ch = bufferedInput1.read(); - while ( -1 != ch ) + while ( 0 <= ch ) { final int ch2 = bufferedInput2.read(); if ( ch != ch2 ) @@ -710,7 +710,7 @@ public static boolean contentEquals( final InputStream input1, } final int ch2 = bufferedInput2.read(); - if ( -1 != ch2 ) + if ( 0 <= ch2 ) { return false; } diff --git a/src/test/java/org/codehaus/plexus/util/IOUtilTest.java b/src/test/java/org/codehaus/plexus/util/IOUtilTest.java index dc0ae630..e126cd24 100644 --- a/src/test/java/org/codehaus/plexus/util/IOUtilTest.java +++ b/src/test/java/org/codehaus/plexus/util/IOUtilTest.java @@ -127,7 +127,7 @@ private void assertEqualContent( File f0, File f1 ) try { - while ( -1 != n0 ) + while ( 0 <= n0 ) { n0 = is0.read( buf0 ); n1 = is1.read( buf1 );