From ba1c194dc0d57b49ffb2220b7571e68caf09f18b Mon Sep 17 00:00:00 2001 From: Christian Schulte Date: Sat, 7 May 2016 19:27:19 +0200 Subject: [PATCH] o Updated class 'PropertyUtils' to stop silently dropping exceptions. o Updated to stop suppressing exceptions incorrectly. --- .../java/org/codehaus/plexus/util/Expand.java | 62 +++++-------------- .../org/codehaus/plexus/util/FileUtils.java | 33 +++++----- .../codehaus/plexus/util/PropertyUtils.java | 54 +++++----------- .../org/codehaus/plexus/util/xml/XmlUtil.java | 25 +++++--- .../plexus/util/xml/Xpp3DomBuilder.java | 26 +++++--- 5 files changed, 85 insertions(+), 115 deletions(-) diff --git a/src/main/java/org/codehaus/plexus/util/Expand.java b/src/main/java/org/codehaus/plexus/util/Expand.java index 7f2534d9..0f1449c1 100644 --- a/src/main/java/org/codehaus/plexus/util/Expand.java +++ b/src/main/java/org/codehaus/plexus/util/Expand.java @@ -76,6 +76,7 @@ */ public class Expand { + private File dest;//req private File source;// req @@ -99,7 +100,7 @@ public void execute() /** * Description of the Method */ - protected void expandFile( File srcF, File dir ) + protected void expandFile( final File srcF, final File dir ) throws Exception { ZipInputStream zis = null; @@ -107,55 +108,37 @@ protected void expandFile( File srcF, File dir ) { // code from WarExpand zis = new ZipInputStream( new FileInputStream( srcF ) ); - ZipEntry ze = null; - while ( ( ze = zis.getNextEntry() ) != null ) + for ( ZipEntry ze = zis.getNextEntry(); ze != null; ze = zis.getNextEntry() ) { - extractFile( srcF, - dir, zis, - ze.getName(), - new Date( ze.getTime() ), - ze.isDirectory() ); + extractFile( srcF, dir, zis, ze.getName(), new Date( ze.getTime() ), ze.isDirectory() ); } //log("expand complete", Project.MSG_VERBOSE); + zis.close(); + zis = null; } catch ( IOException ioe ) { - throw new Exception("Error while expanding " + srcF.getPath(), ioe); + throw new Exception( "Error while expanding " + srcF.getPath(), ioe ); } finally { - if ( zis != null ) - { - try - { - zis.close(); - } - catch ( IOException e ) - { - } - } + IOUtil.close( zis ); } } /** * Description of the Method */ - protected void extractFile( File srcF, - File dir, - InputStream compressedInputStream, - String entryName, - Date entryDate, - boolean isDirectory ) + protected void extractFile( File srcF, File dir, InputStream compressedInputStream, String entryName, + Date entryDate, boolean isDirectory ) throws Exception { File f = FileUtils.resolveFile( dir, entryName ); try { - if ( !overwrite && f.exists() - && - f.lastModified() >= entryDate.getTime() ) + if ( !overwrite && f.exists() && f.lastModified() >= entryDate.getTime() ) { return; } @@ -170,34 +153,22 @@ protected void extractFile( File srcF, } else { - byte[] buffer = new byte[1024]; - int length = 0; + byte[] buffer = new byte[ 65536 ]; FileOutputStream fos = null; try { fos = new FileOutputStream( f ); - while ( ( length = - compressedInputStream.read( buffer ) ) >= 0 ) - { - fos.write( buffer, 0, length ); - } + for ( int length = compressedInputStream.read( buffer ); + length >= 0; + fos.write( buffer, 0, length ), length = compressedInputStream.read( buffer ) ); fos.close(); fos = null; } finally { - if ( fos != null ) - { - try - { - fos.close(); - } - catch ( IOException e ) - { - } - } + IOUtil.close( fos ); } } @@ -239,4 +210,5 @@ public void setOverwrite( boolean b ) { overwrite = b; } + } diff --git a/src/main/java/org/codehaus/plexus/util/FileUtils.java b/src/main/java/org/codehaus/plexus/util/FileUtils.java index cdd2c1d7..40c063a9 100644 --- a/src/main/java/org/codehaus/plexus/util/FileUtils.java +++ b/src/main/java/org/codehaus/plexus/util/FileUtils.java @@ -2385,26 +2385,31 @@ public static void copyFile( File from, File to, String encoding, FilterWrapper[ public static List loadFile( File file ) throws IOException { - List lines = new ArrayList(); - - if ( file.exists() ) + final List lines = new ArrayList(); + BufferedReader reader = null; + try { - BufferedReader reader = new BufferedReader( new FileReader( file ) ); - - String line = reader.readLine(); - - while ( line != null ) + if ( file.exists() ) { - line = line.trim(); + reader = new BufferedReader( new FileReader( file ) ); - if ( !line.startsWith( "#" ) && line.length() != 0 ) + for ( String line = reader.readLine(); line != null; line = reader.readLine() ) { - lines.add( line ); + line = line.trim(); + + if ( !line.startsWith( "#" ) && line.length() != 0 ) + { + lines.add( line ); + } } - line = reader.readLine(); - } - reader.close(); + reader.close(); + reader = null; + } + } + finally + { + IOUtil.close( reader ); } return lines; diff --git a/src/main/java/org/codehaus/plexus/util/PropertyUtils.java b/src/main/java/org/codehaus/plexus/util/PropertyUtils.java index 0ed1aa48..f369a965 100644 --- a/src/main/java/org/codehaus/plexus/util/PropertyUtils.java +++ b/src/main/java/org/codehaus/plexus/util/PropertyUtils.java @@ -34,67 +34,47 @@ public class PropertyUtils { - public static Properties loadProperties( URL url ) + public static Properties loadProperties( final URL url ) throws IOException { - try - { - return loadProperties( url.openStream() ); - } - catch ( Exception e ) + if ( url == null ) { - // ignore + throw new NullPointerException( "url" ); } - return null; + return loadProperties( url.openStream() ); } - public static Properties loadProperties( File file ) + public static Properties loadProperties( final File file ) throws IOException { - try + if ( file == null ) { - return loadProperties( new FileInputStream( file ) ); - } - catch ( Exception e ) - { - // ignore + throw new NullPointerException( "file" ); } - return null; + return loadProperties( new FileInputStream( file ) ); } - public static Properties loadProperties( InputStream is ) + public static Properties loadProperties( final InputStream is ) throws IOException { + InputStream in = is; try { - Properties properties = new Properties(); + final Properties properties = new Properties(); // Make sure the properties stream is valid - if ( is != null ) + if ( in != null ) { - properties.load( is ); + properties.load( in ); + in.close(); + in = null; } return properties; } - catch ( IOException e ) - { - // ignore - } finally { - try - { - if ( is != null ) - { - is.close(); - } - } - catch ( IOException e ) - { - // ignore - } + IOUtil.close( in ); } - - return null; } + } diff --git a/src/main/java/org/codehaus/plexus/util/xml/XmlUtil.java b/src/main/java/org/codehaus/plexus/util/xml/XmlUtil.java index 1973e340..722d3ea5 100644 --- a/src/main/java/org/codehaus/plexus/util/xml/XmlUtil.java +++ b/src/main/java/org/codehaus/plexus/util/xml/XmlUtil.java @@ -72,7 +72,8 @@ public static boolean isXml( File f ) XmlPullParser parser = new MXParser(); parser.setInput( reader ); parser.nextToken(); - + reader.close(); + reader = null; return true; } catch ( Exception e ) @@ -221,20 +222,26 @@ public static void prettyFormat( InputStream is, OutputStream os, int indentSize } Reader reader = null; - - Writer out = new OutputStreamWriter( os ); - PrettyPrintXMLWriter xmlWriter = new PrettyPrintXMLWriter( out ); - xmlWriter.setLineIndenter( StringUtils.repeat( " ", indentSize ) ); - xmlWriter.setLineSeparator( lineSeparator ); - - XmlPullParser parser = new MXParser(); + Writer writer = null; try { reader = ReaderFactory.newXmlReader( is ); + writer = new OutputStreamWriter( os ); + + final PrettyPrintXMLWriter xmlWriter = new PrettyPrintXMLWriter( writer ); + xmlWriter.setLineIndenter( StringUtils.repeat( " ", indentSize ) ); + xmlWriter.setLineSeparator( lineSeparator ); + final XmlPullParser parser = new MXParser(); parser.setInput( reader ); prettyFormatInternal( parser, xmlWriter ); + + writer.close(); + writer = null; + + reader.close(); + reader = null; } catch ( XmlPullParserException e ) { @@ -242,8 +249,8 @@ public static void prettyFormat( InputStream is, OutputStream os, int indentSize } finally { + IOUtil.close( writer ); IOUtil.close( reader ); - IOUtil.close( out ); } } diff --git a/src/main/java/org/codehaus/plexus/util/xml/Xpp3DomBuilder.java b/src/main/java/org/codehaus/plexus/util/xml/Xpp3DomBuilder.java index bfbe7240..3f8ce989 100644 --- a/src/main/java/org/codehaus/plexus/util/xml/Xpp3DomBuilder.java +++ b/src/main/java/org/codehaus/plexus/util/xml/Xpp3DomBuilder.java @@ -49,13 +49,16 @@ public static Xpp3Dom build( InputStream is, String encoding ) public static Xpp3Dom build( InputStream is, String encoding, boolean trim ) throws XmlPullParserException, IOException { - XmlPullParser parser = new MXParser(); - - parser.setInput( is, encoding ); - try { - return build( parser, trim ); + final XmlPullParser parser = new MXParser(); + parser.setInput( is, encoding ); + + final Xpp3Dom xpp3Dom = build( parser, trim ); + is.close(); + is = null; + + return xpp3Dom; } finally { @@ -66,13 +69,16 @@ public static Xpp3Dom build( InputStream is, String encoding, boolean trim ) public static Xpp3Dom build( Reader reader, boolean trim ) throws XmlPullParserException, IOException { - XmlPullParser parser = new MXParser(); - - parser.setInput( reader ); - try { - return build( parser, trim ); + final XmlPullParser parser = new MXParser(); + parser.setInput( reader ); + + final Xpp3Dom xpp3Dom = build( parser, trim ); + reader.close(); + reader = null; + + return xpp3Dom; } finally {