Skip to content

Commit ebbc240

Browse files
author
Gintas Grigelionis
committed
Use try with resources
1 parent bf3e9dc commit ebbc240

10 files changed

+13
-111
lines changed

src/java/org/apache/ivy/ant/IvyReport.java

+1-11
Original file line numberDiff line numberDiff line change
@@ -308,10 +308,8 @@ private void genStyled(String[] confs, File style, String ext) throws IOExceptio
308308
out = getProject().getBaseDir();
309309
}
310310

311-
InputStream xsltStream = null;
312-
try {
311+
try (InputStream xsltStream = new BufferedInputStream(new FileInputStream(style))) {
313312
// create stream to stylesheet
314-
xsltStream = new BufferedInputStream(new FileInputStream(style));
315313
Source xsltSource = new StreamSource(xsltStream, JAXPUtils.getSystemId(style));
316314

317315
// create transformer
@@ -372,14 +370,6 @@ private void genStyled(String[] confs, File style, String ext) throws IOExceptio
372370
}
373371
} catch (TransformerConfigurationException e) {
374372
throw new BuildException(e);
375-
} finally {
376-
if (xsltStream != null) {
377-
try {
378-
xsltStream.close();
379-
} catch (IOException e) {
380-
// ignore
381-
}
382-
}
383373
}
384374
}
385375

src/java/org/apache/ivy/core/pack/ZipPacking.java

+1-8
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,8 @@ public void unpack(InputStream packed, File dest) throws IOException {
7373
}
7474

7575
protected void writeFile(InputStream zip, File f) throws IOException {
76-
FileOutputStream out = new FileOutputStream(f);
77-
try {
76+
try (FileOutputStream out = new FileOutputStream(f)) {
7877
FileUtil.copy(zip, out, null, false);
79-
} finally {
80-
try {
81-
out.close();
82-
} catch (IOException e) {
83-
// ignore
84-
}
8578
}
8679
}
8780

src/java/org/apache/ivy/core/settings/XmlSettingsParser.java

+1-11
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,7 @@ public File resolveFile(String path, String filename) {
151151
@SuppressWarnings("deprecation")
152152
private void doParse(URL settingsUrl) throws IOException, ParseException {
153153
this.settings = settingsUrl;
154-
InputStream stream = null;
155-
try {
156-
stream = URLHandlerRegistry.getDefault().openStream(settingsUrl);
154+
try (InputStream stream = URLHandlerRegistry.getDefault().openStream(settingsUrl)) {
157155
InputSource inSrc = new InputSource(stream);
158156
inSrc.setSystemId(settingsUrl.toExternalForm());
159157
SAXParserFactory.newInstance().newSAXParser().parse(settingsUrl.toExternalForm(), this);
@@ -165,14 +163,6 @@ private void doParse(URL settingsUrl) throws IOException, ParseException {
165163
+ ": " + e.getMessage(), 0);
166164
pe.initCause(e);
167165
throw pe;
168-
} finally {
169-
if (stream != null) {
170-
try {
171-
stream.close();
172-
} catch (IOException e) {
173-
// ignored
174-
}
175-
}
176166
}
177167
}
178168

src/java/org/apache/ivy/osgi/core/ManifestParser.java

+1-8
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,8 @@ public static BundleInfo parseJarManifest(InputStream jarStream) throws IOExcept
8787
}
8888

8989
public static BundleInfo parseManifest(File manifestFile) throws IOException, ParseException {
90-
FileInputStream fis = new FileInputStream(manifestFile);
91-
try {
90+
try (FileInputStream fis = new FileInputStream(manifestFile)) {
9291
return parseManifest(fis);
93-
} finally {
94-
try {
95-
fis.close();
96-
} catch (IOException e) {
97-
// ignore
98-
}
9992
}
10093
}
10194

src/java/org/apache/ivy/osgi/core/OSGiManifestParser.java

+1-8
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,9 @@ public boolean accept(Resource res) {
6161

6262
public ModuleDescriptor parseDescriptor(ParserSettings ivySettings, URL descriptorURL,
6363
Resource res, boolean validate) throws ParseException, IOException {
64-
final InputStream resourceStream = res.openStream();
6564
final Manifest manifest;
66-
try {
65+
try (InputStream resourceStream = res.openStream()) {
6766
manifest = new Manifest(resourceStream);
68-
} finally {
69-
try {
70-
resourceStream.close();
71-
} catch (Exception e) {
72-
// ignore
73-
}
7467
}
7568
BundleInfo bundleInfo = ManifestParser.parseManifest(manifest);
7669
try {

src/java/org/apache/ivy/osgi/repo/AbstractFSManifestIterable.java

+1-11
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ public boolean hasNext() {
101101
}
102102
} else if (bundleCandidates.hasNext()) {
103103
T bundleCandidate = bundleCandidates.next();
104-
JarInputStream in = null;
105-
try {
106-
in = new JarInputStream(getInputStream(bundleCandidate));
104+
try (JarInputStream in = new JarInputStream(getInputStream(bundleCandidate))) {
107105
Manifest manifest = in.getManifest();
108106
if (manifest != null) {
109107
next = new ManifestAndLocation(manifest,
@@ -115,14 +113,6 @@ public boolean hasNext() {
115113
Message.debug("Jar file just removed: " + bundleCandidate, e);
116114
} catch (IOException e) {
117115
Message.warn("Unreadable jar: " + bundleCandidate, e);
118-
} finally {
119-
if (in != null) {
120-
try {
121-
in.close();
122-
} catch (IOException e) {
123-
// Don't care
124-
}
125-
}
126116
}
127117
} else {
128118
// no more candidate on the current directory

src/java/org/apache/ivy/osgi/repo/ArtifactReportManifestIterable.java

+3-13
Original file line numberDiff line numberDiff line change
@@ -94,27 +94,17 @@ public boolean hasNext() {
9494
}
9595
}
9696
if (jar.getUnpackedLocalFile() != null && jar.getUnpackedLocalFile().isDirectory()) {
97-
FileInputStream in = null;
98-
try {
99-
in = new FileInputStream(new File(jar.getUnpackedLocalFile(),
100-
"META-INF/MANIFEST.MF"));
97+
try (FileInputStream in = new FileInputStream(new File(jar.getUnpackedLocalFile(),
98+
"META-INF/MANIFEST.MF"))) {
10199
next = new ManifestAndLocation(new Manifest(in), jar.getUnpackedLocalFile()
102100
.toURI(), sourceURI);
103101
return true;
104102
} catch (FileNotFoundException e) {
105103
Message.debug(
106-
"Bundle directory file just removed: " + jar.getUnpackedLocalFile(), e);
104+
"Bundle directory file just removed: " + jar.getUnpackedLocalFile(), e);
107105
} catch (IOException e) {
108106
Message.debug("The Manifest in the bundle directory could not be read: "
109107
+ jar.getUnpackedLocalFile(), e);
110-
} finally {
111-
if (in != null) {
112-
try {
113-
in.close();
114-
} catch (IOException e) {
115-
// ignore
116-
}
117-
}
118108
}
119109
} else {
120110
File artifact;

src/java/org/apache/ivy/util/CredentialsUtil.java

+2-22
Original file line numberDiff line numberDiff line change
@@ -111,20 +111,10 @@ public static Credentials promptCredentials(Credentials c, File passfile) {
111111
Properties props = new EncryptedProperties();
112112
props.setProperty("username", username);
113113
props.setProperty("passwd", passwd);
114-
FileOutputStream fos = null;
115-
try {
116-
fos = new FileOutputStream(passfile);
114+
try (FileOutputStream fos = new FileOutputStream(passfile)) {
117115
props.store(fos, "");
118116
} catch (Exception e) {
119117
Message.warn("error occurred while saving password file " + passfile, e);
120-
} finally {
121-
if (fos != null) {
122-
try {
123-
fos.close();
124-
} catch (Exception e) {
125-
// ignored
126-
}
127-
}
128118
}
129119
}
130120
c = new Credentials(c.getRealm(), c.getHost(), username, passwd);
@@ -135,9 +125,7 @@ public static Credentials promptCredentials(Credentials c, File passfile) {
135125
public static Credentials loadPassfile(Credentials c, File passfile) {
136126
if (passfile != null && passfile.exists()) {
137127
Properties props = new EncryptedProperties();
138-
FileInputStream fis = null;
139-
try {
140-
fis = new FileInputStream(passfile);
128+
try (FileInputStream fis = new FileInputStream(passfile)) {
141129
props.load(fis);
142130
String username = c.getUserName();
143131
String passwd = c.getPasswd();
@@ -150,14 +138,6 @@ public static Credentials loadPassfile(Credentials c, File passfile) {
150138
return new Credentials(c.getRealm(), c.getHost(), username, passwd);
151139
} catch (IOException e) {
152140
Message.warn("error occurred while loading password file " + passfile, e);
153-
} finally {
154-
if (fis != null) {
155-
try {
156-
fis.close();
157-
} catch (IOException e) {
158-
// ignored
159-
}
160-
}
161141
}
162142
}
163143
return c;

src/java/org/apache/ivy/util/XMLHelper.java

+1-8
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,10 @@ public static void parse(URL xmlURL, URL schema, DefaultHandler handler) throws
119119
@SuppressWarnings("deprecation")
120120
public static void parse(URL xmlURL, URL schema, DefaultHandler handler, LexicalHandler lHandler)
121121
throws SAXException, IOException, ParserConfigurationException {
122-
InputStream xmlStream = URLHandlerRegistry.getDefault().openStream(xmlURL);
123-
try {
122+
try (InputStream xmlStream = URLHandlerRegistry.getDefault().openStream(xmlURL)) {
124123
InputSource inSrc = new InputSource(xmlStream);
125124
inSrc.setSystemId(toSystemId(xmlURL));
126125
parse(inSrc, schema, handler, lHandler);
127-
} finally {
128-
try {
129-
xmlStream.close();
130-
} catch (IOException e) {
131-
// ignored
132-
}
133126
}
134127
}
135128

src/java/org/apache/ivy/util/url/BasicURLHandler.java

+1-11
Original file line numberDiff line numberDiff line change
@@ -347,22 +347,12 @@ private void disconnect(URLConnection con) {
347347
private void readResponseBody(HttpURLConnection conn) {
348348
byte[] buffer = new byte[BUFFER_SIZE];
349349

350-
InputStream inStream = null;
351-
try {
352-
inStream = conn.getInputStream();
350+
try (InputStream inStream = conn.getInputStream()) {
353351
while (inStream.read(buffer) > 0) {
354352
// Skip content
355353
}
356354
} catch (IOException e) {
357355
// ignore
358-
} finally {
359-
if (inStream != null) {
360-
try {
361-
inStream.close();
362-
} catch (IOException e) {
363-
// ignore
364-
}
365-
}
366356
}
367357

368358
InputStream errStream = conn.getErrorStream();

0 commit comments

Comments
 (0)