Skip to content

Commit ba1c194

Browse files
o Updated class 'PropertyUtils' to stop silently dropping exceptions.
o Updated to stop suppressing exceptions incorrectly.
1 parent 8399a2e commit ba1c194

File tree

5 files changed

+85
-115
lines changed

5 files changed

+85
-115
lines changed

src/main/java/org/codehaus/plexus/util/Expand.java

+17-45
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
*/
7777
public class Expand
7878
{
79+
7980
private File dest;//req
8081

8182
private File source;// req
@@ -99,63 +100,45 @@ public void execute()
99100
/**
100101
* Description of the Method
101102
*/
102-
protected void expandFile( File srcF, File dir )
103+
protected void expandFile( final File srcF, final File dir )
103104
throws Exception
104105
{
105106
ZipInputStream zis = null;
106107
try
107108
{
108109
// code from WarExpand
109110
zis = new ZipInputStream( new FileInputStream( srcF ) );
110-
ZipEntry ze = null;
111111

112-
while ( ( ze = zis.getNextEntry() ) != null )
112+
for ( ZipEntry ze = zis.getNextEntry(); ze != null; ze = zis.getNextEntry() )
113113
{
114-
extractFile( srcF,
115-
dir, zis,
116-
ze.getName(),
117-
new Date( ze.getTime() ),
118-
ze.isDirectory() );
114+
extractFile( srcF, dir, zis, ze.getName(), new Date( ze.getTime() ), ze.isDirectory() );
119115
}
120116

121117
//log("expand complete", Project.MSG_VERBOSE);
118+
zis.close();
119+
zis = null;
122120
}
123121
catch ( IOException ioe )
124122
{
125-
throw new Exception("Error while expanding " + srcF.getPath(), ioe);
123+
throw new Exception( "Error while expanding " + srcF.getPath(), ioe );
126124
}
127125
finally
128126
{
129-
if ( zis != null )
130-
{
131-
try
132-
{
133-
zis.close();
134-
}
135-
catch ( IOException e )
136-
{
137-
}
138-
}
127+
IOUtil.close( zis );
139128
}
140129
}
141130

142131
/**
143132
* Description of the Method
144133
*/
145-
protected void extractFile( File srcF,
146-
File dir,
147-
InputStream compressedInputStream,
148-
String entryName,
149-
Date entryDate,
150-
boolean isDirectory )
134+
protected void extractFile( File srcF, File dir, InputStream compressedInputStream, String entryName,
135+
Date entryDate, boolean isDirectory )
151136
throws Exception
152137
{
153138
File f = FileUtils.resolveFile( dir, entryName );
154139
try
155140
{
156-
if ( !overwrite && f.exists()
157-
&&
158-
f.lastModified() >= entryDate.getTime() )
141+
if ( !overwrite && f.exists() && f.lastModified() >= entryDate.getTime() )
159142
{
160143
return;
161144
}
@@ -170,34 +153,22 @@ protected void extractFile( File srcF,
170153
}
171154
else
172155
{
173-
byte[] buffer = new byte[1024];
174-
int length = 0;
156+
byte[] buffer = new byte[ 65536 ];
175157
FileOutputStream fos = null;
176158
try
177159
{
178160
fos = new FileOutputStream( f );
179161

180-
while ( ( length =
181-
compressedInputStream.read( buffer ) ) >= 0 )
182-
{
183-
fos.write( buffer, 0, length );
184-
}
162+
for ( int length = compressedInputStream.read( buffer );
163+
length >= 0;
164+
fos.write( buffer, 0, length ), length = compressedInputStream.read( buffer ) );
185165

186166
fos.close();
187167
fos = null;
188168
}
189169
finally
190170
{
191-
if ( fos != null )
192-
{
193-
try
194-
{
195-
fos.close();
196-
}
197-
catch ( IOException e )
198-
{
199-
}
200-
}
171+
IOUtil.close( fos );
201172
}
202173
}
203174

@@ -239,4 +210,5 @@ public void setOverwrite( boolean b )
239210
{
240211
overwrite = b;
241212
}
213+
242214
}

src/main/java/org/codehaus/plexus/util/FileUtils.java

+19-14
Original file line numberDiff line numberDiff line change
@@ -2385,26 +2385,31 @@ public static void copyFile( File from, File to, String encoding, FilterWrapper[
23852385
public static List<String> loadFile( File file )
23862386
throws IOException
23872387
{
2388-
List<String> lines = new ArrayList<String>();
2389-
2390-
if ( file.exists() )
2388+
final List<String> lines = new ArrayList<String>();
2389+
BufferedReader reader = null;
2390+
try
23912391
{
2392-
BufferedReader reader = new BufferedReader( new FileReader( file ) );
2393-
2394-
String line = reader.readLine();
2395-
2396-
while ( line != null )
2392+
if ( file.exists() )
23972393
{
2398-
line = line.trim();
2394+
reader = new BufferedReader( new FileReader( file ) );
23992395

2400-
if ( !line.startsWith( "#" ) && line.length() != 0 )
2396+
for ( String line = reader.readLine(); line != null; line = reader.readLine() )
24012397
{
2402-
lines.add( line );
2398+
line = line.trim();
2399+
2400+
if ( !line.startsWith( "#" ) && line.length() != 0 )
2401+
{
2402+
lines.add( line );
2403+
}
24032404
}
2404-
line = reader.readLine();
2405-
}
24062405

2407-
reader.close();
2406+
reader.close();
2407+
reader = null;
2408+
}
2409+
}
2410+
finally
2411+
{
2412+
IOUtil.close( reader );
24082413
}
24092414

24102415
return lines;

src/main/java/org/codehaus/plexus/util/PropertyUtils.java

+17-37
Original file line numberDiff line numberDiff line change
@@ -34,67 +34,47 @@
3434
public class PropertyUtils
3535
{
3636

37-
public static Properties loadProperties( URL url )
37+
public static Properties loadProperties( final URL url ) throws IOException
3838
{
39-
try
40-
{
41-
return loadProperties( url.openStream() );
42-
}
43-
catch ( Exception e )
39+
if ( url == null )
4440
{
45-
// ignore
41+
throw new NullPointerException( "url" );
4642
}
4743

48-
return null;
44+
return loadProperties( url.openStream() );
4945
}
5046

51-
public static Properties loadProperties( File file )
47+
public static Properties loadProperties( final File file ) throws IOException
5248
{
53-
try
49+
if ( file == null )
5450
{
55-
return loadProperties( new FileInputStream( file ) );
56-
}
57-
catch ( Exception e )
58-
{
59-
// ignore
51+
throw new NullPointerException( "file" );
6052
}
6153

62-
return null;
54+
return loadProperties( new FileInputStream( file ) );
6355
}
6456

65-
public static Properties loadProperties( InputStream is )
57+
public static Properties loadProperties( final InputStream is ) throws IOException
6658
{
59+
InputStream in = is;
6760
try
6861
{
69-
Properties properties = new Properties();
62+
final Properties properties = new Properties();
7063

7164
// Make sure the properties stream is valid
72-
if ( is != null )
65+
if ( in != null )
7366
{
74-
properties.load( is );
67+
properties.load( in );
68+
in.close();
69+
in = null;
7570
}
7671

7772
return properties;
7873
}
79-
catch ( IOException e )
80-
{
81-
// ignore
82-
}
8374
finally
8475
{
85-
try
86-
{
87-
if ( is != null )
88-
{
89-
is.close();
90-
}
91-
}
92-
catch ( IOException e )
93-
{
94-
// ignore
95-
}
76+
IOUtil.close( in );
9677
}
97-
98-
return null;
9978
}
79+
10080
}

src/main/java/org/codehaus/plexus/util/xml/XmlUtil.java

+16-9
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ public static boolean isXml( File f )
7272
XmlPullParser parser = new MXParser();
7373
parser.setInput( reader );
7474
parser.nextToken();
75-
75+
reader.close();
76+
reader = null;
7677
return true;
7778
}
7879
catch ( Exception e )
@@ -221,29 +222,35 @@ public static void prettyFormat( InputStream is, OutputStream os, int indentSize
221222
}
222223

223224
Reader reader = null;
224-
225-
Writer out = new OutputStreamWriter( os );
226-
PrettyPrintXMLWriter xmlWriter = new PrettyPrintXMLWriter( out );
227-
xmlWriter.setLineIndenter( StringUtils.repeat( " ", indentSize ) );
228-
xmlWriter.setLineSeparator( lineSeparator );
229-
230-
XmlPullParser parser = new MXParser();
225+
Writer writer = null;
231226
try
232227
{
233228
reader = ReaderFactory.newXmlReader( is );
229+
writer = new OutputStreamWriter( os );
230+
231+
final PrettyPrintXMLWriter xmlWriter = new PrettyPrintXMLWriter( writer );
232+
xmlWriter.setLineIndenter( StringUtils.repeat( " ", indentSize ) );
233+
xmlWriter.setLineSeparator( lineSeparator );
234234

235+
final XmlPullParser parser = new MXParser();
235236
parser.setInput( reader );
236237

237238
prettyFormatInternal( parser, xmlWriter );
239+
240+
writer.close();
241+
writer = null;
242+
243+
reader.close();
244+
reader = null;
238245
}
239246
catch ( XmlPullParserException e )
240247
{
241248
throw new IOException( "Unable to parse the XML: " + e.getMessage() );
242249
}
243250
finally
244251
{
252+
IOUtil.close( writer );
245253
IOUtil.close( reader );
246-
IOUtil.close( out );
247254
}
248255
}
249256

src/main/java/org/codehaus/plexus/util/xml/Xpp3DomBuilder.java

+16-10
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,16 @@ public static Xpp3Dom build( InputStream is, String encoding )
4949
public static Xpp3Dom build( InputStream is, String encoding, boolean trim )
5050
throws XmlPullParserException, IOException
5151
{
52-
XmlPullParser parser = new MXParser();
53-
54-
parser.setInput( is, encoding );
55-
5652
try
5753
{
58-
return build( parser, trim );
54+
final XmlPullParser parser = new MXParser();
55+
parser.setInput( is, encoding );
56+
57+
final Xpp3Dom xpp3Dom = build( parser, trim );
58+
is.close();
59+
is = null;
60+
61+
return xpp3Dom;
5962
}
6063
finally
6164
{
@@ -66,13 +69,16 @@ public static Xpp3Dom build( InputStream is, String encoding, boolean trim )
6669
public static Xpp3Dom build( Reader reader, boolean trim )
6770
throws XmlPullParserException, IOException
6871
{
69-
XmlPullParser parser = new MXParser();
70-
71-
parser.setInput( reader );
72-
7372
try
7473
{
75-
return build( parser, trim );
74+
final XmlPullParser parser = new MXParser();
75+
parser.setInput( reader );
76+
77+
final Xpp3Dom xpp3Dom = build( parser, trim );
78+
reader.close();
79+
reader = null;
80+
81+
return xpp3Dom;
7682
}
7783
finally
7884
{

0 commit comments

Comments
 (0)