Skip to content

Commit

Permalink
o Updated class 'PropertyUtils' to stop silently dropping exceptions.
Browse files Browse the repository at this point in the history
o Updated to stop suppressing exceptions incorrectly.
  • Loading branch information
ChristianSchulte committed May 7, 2016
1 parent 8399a2e commit ba1c194
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 115 deletions.
62 changes: 17 additions & 45 deletions src/main/java/org/codehaus/plexus/util/Expand.java
Expand Up @@ -76,6 +76,7 @@
*/
public class Expand
{

private File dest;//req

private File source;// req
Expand All @@ -99,63 +100,45 @@ 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;
try
{
// 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;
}
Expand All @@ -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 );
}
}

Expand Down Expand Up @@ -239,4 +210,5 @@ public void setOverwrite( boolean b )
{
overwrite = b;
}

}
33 changes: 19 additions & 14 deletions src/main/java/org/codehaus/plexus/util/FileUtils.java
Expand Up @@ -2385,26 +2385,31 @@ public static void copyFile( File from, File to, String encoding, FilterWrapper[
public static List<String> loadFile( File file )
throws IOException
{
List<String> lines = new ArrayList<String>();

if ( file.exists() )
final List<String> lines = new ArrayList<String>();
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;
Expand Down
54 changes: 17 additions & 37 deletions src/main/java/org/codehaus/plexus/util/PropertyUtils.java
Expand Up @@ -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;
}

}
25 changes: 16 additions & 9 deletions src/main/java/org/codehaus/plexus/util/xml/XmlUtil.java
Expand Up @@ -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 )
Expand Down Expand Up @@ -221,29 +222,35 @@ 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 )
{
throw new IOException( "Unable to parse the XML: " + e.getMessage() );
}
finally
{
IOUtil.close( writer );
IOUtil.close( reader );
IOUtil.close( out );
}
}

Expand Down
26 changes: 16 additions & 10 deletions src/main/java/org/codehaus/plexus/util/xml/Xpp3DomBuilder.java
Expand Up @@ -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
{
Expand All @@ -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
{
Expand Down

0 comments on commit ba1c194

Please sign in to comment.