Skip to content

Commit bf4a3d2

Browse files
authored
Switch to modern I/O (#91)
1 parent 9022926 commit bf4a3d2

File tree

4 files changed

+62
-111
lines changed

4 files changed

+62
-111
lines changed

src/main/java/org/apache/maven/report/projectinfo/DependenciesReport.java

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.io.InputStreamReader;
3030
import java.io.LineNumberReader;
3131
import java.io.OutputStream;
32+
import java.nio.charset.StandardCharsets;
3233
import java.util.Locale;
3334

3435
import org.apache.maven.artifact.Artifact;
@@ -54,7 +55,6 @@
5455
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
5556
import org.codehaus.plexus.i18n.I18N;
5657
import org.codehaus.plexus.util.IOUtil;
57-
import org.codehaus.plexus.util.ReaderFactory;
5858

5959
/**
6060
* Generates the Project Dependencies report.
@@ -223,45 +223,27 @@ private DependencyNode resolveProject() {
223223
* @throws IOException if any
224224
*/
225225
private void copyResources(File outputDirectory) throws IOException {
226-
InputStream resourceList = null;
227-
InputStream in = null;
228-
BufferedReader reader = null;
229-
OutputStream out = null;
230-
try {
231-
resourceList = getClass().getClassLoader().getResourceAsStream(RESOURCES_DIR + "/resources.txt");
232-
233-
if (resourceList != null) {
234-
reader = new LineNumberReader(new InputStreamReader(resourceList, ReaderFactory.US_ASCII));
235-
226+
InputStream resourceList = getClass().getClassLoader().getResourceAsStream(RESOURCES_DIR + "/resources.txt");
227+
if (resourceList != null) {
228+
try (BufferedReader reader =
229+
new LineNumberReader(new InputStreamReader(resourceList, StandardCharsets.US_ASCII))) {
236230
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
237-
in = getClass().getClassLoader().getResourceAsStream(RESOURCES_DIR + "/" + line);
238-
239-
if (in == null) {
240-
throw new IOException("The resource " + line + " doesn't exist.");
231+
try (InputStream in = getClass().getClassLoader().getResourceAsStream(RESOURCES_DIR + "/" + line)) {
232+
if (in == null) {
233+
throw new IOException("The resource " + line + " doesn't exist.");
234+
}
235+
236+
File outputFile = new File(outputDirectory, line);
237+
if (!outputFile.getParentFile().exists()) {
238+
outputFile.getParentFile().mkdirs();
239+
}
240+
241+
try (OutputStream out = new FileOutputStream(outputFile)) {
242+
IOUtil.copy(in, out);
243+
}
241244
}
242-
243-
File outputFile = new File(outputDirectory, line);
244-
245-
if (!outputFile.getParentFile().exists()) {
246-
outputFile.getParentFile().mkdirs();
247-
}
248-
249-
out = new FileOutputStream(outputFile);
250-
IOUtil.copy(in, out);
251-
out.close();
252-
out = null;
253-
in.close();
254-
in = null;
255245
}
256-
257-
reader.close();
258-
reader = null;
259246
}
260-
} finally {
261-
IOUtil.close(out);
262-
IOUtil.close(reader);
263-
IOUtil.close(in);
264-
IOUtil.close(resourceList);
265247
}
266248
}
267249
}

src/main/java/org/apache/maven/report/projectinfo/DependencyConvergenceReport.java

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -131,56 +131,54 @@ protected String getI18Nsection() {
131131

132132
@Override
133133
protected void executeReport(Locale locale) throws MavenReportException {
134-
Sink sink = getSink();
134+
try (Sink sink = getSink()) {
135+
sink.head();
136+
sink.title();
135137

136-
sink.head();
137-
sink.title();
138+
if (isReactorBuild()) {
139+
sink.text(getI18nString(locale, "reactor.title"));
140+
} else {
141+
sink.text(getI18nString(locale, "title"));
142+
}
138143

139-
if (isReactorBuild()) {
140-
sink.text(getI18nString(locale, "reactor.title"));
141-
} else {
142-
sink.text(getI18nString(locale, "title"));
143-
}
144+
sink.title_();
145+
sink.head_();
144146

145-
sink.title_();
146-
sink.head_();
147+
sink.body();
147148

148-
sink.body();
149+
sink.section1();
149150

150-
sink.section1();
151+
sink.sectionTitle1();
151152

152-
sink.sectionTitle1();
153+
if (isReactorBuild()) {
154+
sink.text(getI18nString(locale, "reactor.title"));
155+
} else {
156+
sink.text(getI18nString(locale, "title"));
157+
}
153158

154-
if (isReactorBuild()) {
155-
sink.text(getI18nString(locale, "reactor.title"));
156-
} else {
157-
sink.text(getI18nString(locale, "title"));
158-
}
159+
sink.sectionTitle1_();
159160

160-
sink.sectionTitle1_();
161+
DependencyAnalyzeResult dependencyResult = analyzeDependencyTree();
162+
int convergence = calculateConvergence(dependencyResult);
161163

162-
DependencyAnalyzeResult dependencyResult = analyzeDependencyTree();
163-
int convergence = calculateConvergence(dependencyResult);
164+
if (convergence < FULL_CONVERGENCE) {
165+
// legend
166+
generateLegend(locale, sink);
167+
sink.lineBreak();
168+
}
164169

165-
if (convergence < FULL_CONVERGENCE) {
166-
// legend
167-
generateLegend(locale, sink);
168-
sink.lineBreak();
169-
}
170+
// stats
171+
generateStats(locale, sink, dependencyResult);
170172

171-
// stats
172-
generateStats(locale, sink, dependencyResult);
173+
sink.section1_();
173174

174-
sink.section1_();
175+
if (convergence < FULL_CONVERGENCE) {
176+
// convergence
177+
generateConvergence(locale, sink, dependencyResult);
178+
}
175179

176-
if (convergence < FULL_CONVERGENCE) {
177-
// convergence
178-
generateConvergence(locale, sink, dependencyResult);
180+
sink.body_();
179181
}
180-
181-
sink.body_();
182-
sink.flush();
183-
sink.close();
184182
}
185183

186184
// ----------------------------------------------------------------------

src/main/java/org/apache/maven/report/projectinfo/ProjectInfoReportUtils.java

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@ public class ProjectInfoReportUtils {
7070
/** The timeout when getting the url input stream */
7171
private static final int TIMEOUT = 1000 * 5;
7272

73-
/** The default encoding used to transform bytes to characters */
74-
private static final String DEFAULT_ENCODING = "UTF-8";
75-
7673
/**
7774
* Get the input stream using UTF-8 as character encoding from a URL.
7875
*
@@ -83,7 +80,7 @@ public class ProjectInfoReportUtils {
8380
* @see #getContent(URL, Settings, String)
8481
*/
8582
public static String getContent(URL url, Settings settings) throws IOException {
86-
return getContent(url, settings, DEFAULT_ENCODING);
83+
return getContent(url, settings, "UTF-8");
8784
}
8885

8986
/**
@@ -115,23 +112,13 @@ public static String getContent(URL url, MavenProject project, Settings settings
115112
String scheme = url.getProtocol();
116113

117114
if (encoding == null || encoding.isEmpty()) {
118-
encoding = DEFAULT_ENCODING;
115+
encoding = "UTF-8";
119116
}
120117

121118
if ("file".equals(scheme)) {
122-
InputStream in = null;
123-
try {
124-
URLConnection conn = url.openConnection();
125-
in = conn.getInputStream();
126-
119+
try (InputStream in = url.openConnection().getInputStream()) {
127120
final String content = IOUtil.toString(in, encoding);
128-
129-
in.close();
130-
in = null;
131-
132121
return content;
133-
} finally {
134-
IOUtil.close(in);
135122
}
136123
}
137124

@@ -167,19 +154,8 @@ protected PasswordAuthentication getPasswordAuthentication() {
167154
}
168155
}
169156

170-
InputStream in = null;
171-
try {
172-
URLConnection conn = getURLConnection(url, project, settings);
173-
in = conn.getInputStream();
174-
175-
final String string = IOUtil.toString(in, encoding);
176-
177-
in.close();
178-
in = null;
179-
180-
return string;
181-
} finally {
182-
IOUtil.close(in);
157+
try (InputStream in = getURLConnection(url, project, settings).getInputStream()) {
158+
return IOUtil.toString(in, encoding);
183159
}
184160
}
185161

src/test/java/org/apache/maven/report/projectinfo/stubs/ProjectInfoProjectStub.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
package org.apache.maven.report.projectinfo.stubs;
2020

2121
import java.io.File;
22+
import java.io.IOException;
23+
import java.io.Reader;
2224
import java.util.ArrayList;
2325
import java.util.Collections;
2426
import java.util.List;
@@ -40,9 +42,8 @@
4042
import org.apache.maven.model.PluginManagement;
4143
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
4244
import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
43-
import org.apache.maven.shared.utils.io.IOUtil;
4445
import org.codehaus.plexus.util.ReaderFactory;
45-
import org.codehaus.plexus.util.xml.XmlStreamReader;
46+
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
4647
import org.eclipse.aether.repository.RemoteRepository;
4748

4849
/**
@@ -61,17 +62,11 @@ public abstract class ProjectInfoProjectStub extends MavenProjectStub {
6162
*/
6263
public ProjectInfoProjectStub() {
6364
MavenXpp3Reader pomReader = new MavenXpp3Reader();
64-
XmlStreamReader reader = null;
65-
try {
66-
reader = ReaderFactory.newXmlReader(new File(getBasedir(), getPOM()));
65+
try (Reader reader = ReaderFactory.newXmlReader(new File(getBasedir(), getPOM()))) {
6766
model = pomReader.read(reader);
68-
reader.close();
69-
reader = null;
7067
setModel(model);
71-
} catch (Exception e) {
68+
} catch (IOException | XmlPullParserException e) {
7269
throw new RuntimeException(e);
73-
} finally {
74-
IOUtil.close(reader);
7570
}
7671

7772
setGroupId(model.getGroupId());

0 commit comments

Comments
 (0)